From 7d37cb6254c1c99b8edb19540f5453f740b96ee1 Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Sat, 21 Sep 2013 13:12:14 -0500 Subject: [PATCH] Add dev/prod configuration Environment can be switched via the MYFLASKAPP_ENV system variable. --- README.rst | 1 + {{cookiecutter.repo_name}}/README.rst | 14 ++++++++++ .../{{cookiecutter.repo_name}}/app.py | 8 +++++- .../{{cookiecutter.repo_name}}/settings.py | 27 ++++++++++++++----- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index f122e5f..5583b88 100644 --- a/README.rst +++ b/README.rst @@ -14,6 +14,7 @@ Features - Procfile for deploying to a PaaS (e.g. Heroku) - nose for testing - A simple ``manage.py`` script. +- Easily switch between development and production environments through the APPNAME_ENV system variable. Screenshots ----------- diff --git a/{{cookiecutter.repo_name}}/README.rst b/{{cookiecutter.repo_name}}/README.rst index 97eb455..098e00a 100644 --- a/{{cookiecutter.repo_name}}/README.rst +++ b/{{cookiecutter.repo_name}}/README.rst @@ -13,6 +13,7 @@ Quickstart git clone https://github.com/{{cookiecutter.github_username}}/{{ cookiecutter.repo_name }} cd {{cookiecutter.repo_name}} pip install -r requirements/dev.txt + export {{cookiecutter.repo_name|upper}}_ENV='dev' python manage.py createdb python manage.py runserver @@ -25,3 +26,16 @@ To open the interactive shell, run :: python manage.py shell By default, you will have access to ``app``, ``models``, and ``db``. + +Development / Production Environments +------------------------------------- + +Configuration environements are handled through the {{cookiecutter.repo_name|upper}}_ENV system environment variable. + +To switch to the development environment, set :: + + export {{cookiecutter.repo_name|upper}}_ENV="dev" + +To switch to the production environment, set :: + + export {{cookiecutter.repo_name|upper}}_ENV="prod" diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py index d01f77e..a2e833a 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py @@ -1,7 +1,13 @@ # -*- coding: utf-8 -*- +import os from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) -app.config.from_pyfile('settings.py') +# The environment variable, either 'prod' or 'dev' +env = os.environ.get("{{cookiecutter.repo_name | upper}}_ENV", "prod") +# Use the appropriate environment-specific settings +app.config.from_object('{{cookiecutter.repo_name}}.settings.{env}Config' + .format(env=env.capitalize())) +app.config['ENV'] = env db = SQLAlchemy(app) diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/settings.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/settings.py index 7006695..574df75 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/settings.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/settings.py @@ -1,10 +1,23 @@ # -*- coding: utf-8 -*- import os -APP_DIR = os.path.abspath(os.path.dirname(__file__)) # This directory -PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir)) -DB_NAME = "test.db" -DB_PATH = os.path.join(PROJECT_ROOT, DB_NAME) -SQLALCHEMY_DATABASE_URI = "sqlite:///{0}".format(DB_PATH) -DEBUG = True -SECRET_KEY = 'shhhh' +class Config(object): + SECRET_KEY = 'shhhh' + APP_DIR = os.path.abspath(os.path.dirname(__file__)) # This directory + PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir)) + +class ProdConfig(Config): + DEBUG = False + SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/example' + SQLALCHEMY_ECHO = False + +class DevConfig(Config): + DEBUG = True + DB_NAME = "test.db" + # Put the db file in project root + DB_PATH = os.path.join(Config.PROJECT_ROOT, DB_NAME) + SQLALCHEMY_DATABASE_URI = "sqlite:///{0}".format(DB_PATH) + SQLALCHEMY_ECHO = True + + +