Skip to content

Instantly share code, notes, and snippets.

View bjinwright's full-sized avatar

Brian Jinwright bjinwright

View GitHub Profile
@bjinwright
bjinwright / settings.py
Created February 15, 2013 16:36
So part of being a 12-factor app is storing your config in your environment (http://12factor.net/config). This is how I do that using Django settings files. This is the code companion for a blog post (http://blog.ipoots.com/post/43154090079/how-to-achieve-12-factor-app-config-glory-with-django). A similar blog post is http://www.wellfireinteract…
# Django settings for dnsly project.
from unipath import FSPath as Path
import os
def env(key, default=None):
"""Retrieves env vars and makes Python boolean replacements"""
val = os.getenv(key, default)
if val == 'True':
val = True
@bjinwright
bjinwright / enable_mod_pagespeed.conf
Created May 10, 2013 15:01
Example mod_pagespeed conf for AWS Elastic Beanstalk.
# mod_deflate configuration
<IfModule mod_deflate.c>
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xml+rss
AddOutputFilterByType DEFLATE application/x-javascript
@bjinwright
bjinwright / .config
Created May 10, 2013 15:17
AWS Elastic Beanstalk container commands for installing mod_pagespeed. This example uses a Django application but just strip out commands 1 and 2 and it usable by any application. This is the follow up to the https://gist.github.com/bjinwright/5554958 Gist that shows how to setup the Apache conf file that makes this work.
container_commands:
01_collectstatic:
command: "django-admin.py collectstatic --noinput"
02_lesscss:
command: "lesscpy -x static/css/base.less > static/css/base.css"
03_setup_apache:
command: "cp enable_mod_pagespeed.conf /etc/httpd/conf.d"
04_rm_pagespeed:
command: "rm -rf /pagespeed/ebextensions"
05_mkdir_pagespeed:
@bjinwright
bjinwright / Cloudant Indexes
Last active August 29, 2015 14:06
Cloudant Indexes
Here are the three JSON documents that I used to create the indexes.
{
"index": {
"fields": ["price","name"]
},
"name" : "price-name-index",
"type" : "json"
}
@bjinwright
bjinwright / custom_storages.py
Created July 12, 2016 18:52
Django Storages S3 custom folder based on settings
from django.conf import settings
from storages.backends.s3boto import S3BotoStorage
class StaticStorage(S3BotoStorage):
location = settings.STATICFILES_LOCATION
class MediaStorage(S3BotoStorage):
location = settings.MEDIAFILES_LOCATION
Import by filename is not supported.: ImportError
Traceback (most recent call last):
File "/var/task/handler.py", line 302, in lambda_handler
return LambdaHandler.lambda_handler(event, context)
File "/var/task/handler.py", line 110, in lambda_handler
return cls().handler(event, context)
File "/var/task/handler.py", line 159, in handler
app_function = get_django_wsgi(self.settings.DJANGO_SETTINGS)
File "/var/task/django_zappa_app.py", line 17, in get_django_wsgi
django.setup()
@bjinwright
bjinwright / .env
Last active March 25, 2022 20:20
Django Settings using Environment Variables via envs (https://github.com/bjinwright/envs) project.
DATABASE_ENGINE=django.db.backends.sqlite3
DATABASE_NAME=db.sqlite3
DATABASE_USER=
DATABASE_PASSWORD=
DATABASE_HOST=
DATABASE_PORT=
SECRET_KEY="asdfdfadsflkjsdflkjadsflkasfakl;"
STATIC_URL=/static/
DEBUG=True
@bjinwright
bjinwright / custom_storages.py
Created December 7, 2016 22:50
Custon S3 Django-Storages backend made for use with CloudFront with multiple origins
from django.conf import settings
from django.utils.encoding import filepath_to_uri
from storages.backends.s3boto import S3BotoStorage
class StaticStorage(S3BotoStorage):
location = settings.STATICFILES_LOCATION
def url(self, name, headers=None, response_headers=None, expire=None):
return '{}{}'.format(settings.STATIC_URL,filepath_to_uri(name))
@bjinwright
bjinwright / zappa-iam.json
Created December 8, 2016 17:30
Zappa IAM policy - Replace all the references to yourblog (your app name), your-function-bucket (the bucket that Zappa stores your code), and your-static-and-media-files-bucket (where you store your static files)
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:DescribeSecurityGroups",
"ec2:DescribeSubnets",
"ec2:Describe*"
],
"Resource": [
@bjinwright
bjinwright / zappa_settings.json
Last active December 8, 2016 23:22
Zappa settings file with remote environment vars and VPC support
{
"dev": {
"aws_region": "us-east-1",
"s3_bucket": "yourcode-bucket",
"django_settings": "yourapp.settings",
"remote_env_file":"yourblog-dev.json",
"remote_env_bucket":"yourcode-bucket",
"use_precompiled_packages": true,
"domain": "yourblog-dev-apigw.yourdevdomain.com",
"lets_encrypt_key": "s3://yourcode-bucket/openssl.key",