Add dev/prod configuration

Environment can be switched via the MYFLASKAPP_ENV system variable.
master
Steven Loria 11 years ago
parent 48a6bd0c99
commit 7d37cb6254
  1. 1
      README.rst
  2. 14
      {{cookiecutter.repo_name}}/README.rst
  3. 8
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py
  4. 19
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/settings.py

@ -14,6 +14,7 @@ Features
- Procfile for deploying to a PaaS (e.g. Heroku) - Procfile for deploying to a PaaS (e.g. Heroku)
- nose for testing - nose for testing
- A simple ``manage.py`` script. - A simple ``manage.py`` script.
- Easily switch between development and production environments through the APPNAME_ENV system variable.
Screenshots Screenshots
----------- -----------

@ -13,6 +13,7 @@ Quickstart
git clone https://github.com/{{cookiecutter.github_username}}/{{ cookiecutter.repo_name }} git clone https://github.com/{{cookiecutter.github_username}}/{{ cookiecutter.repo_name }}
cd {{cookiecutter.repo_name}} cd {{cookiecutter.repo_name}}
pip install -r requirements/dev.txt pip install -r requirements/dev.txt
export {{cookiecutter.repo_name|upper}}_ENV='dev'
python manage.py createdb python manage.py createdb
python manage.py runserver python manage.py runserver
@ -25,3 +26,16 @@ To open the interactive shell, run ::
python manage.py shell python manage.py shell
By default, you will have access to ``app``, ``models``, and ``db``. 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"

@ -1,7 +1,13 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os
from flask import Flask from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__) 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) db = SQLAlchemy(app)

@ -1,10 +1,23 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
class Config(object):
SECRET_KEY = 'shhhh'
APP_DIR = os.path.abspath(os.path.dirname(__file__)) # This directory APP_DIR = os.path.abspath(os.path.dirname(__file__)) # This directory
PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir)) 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" DB_NAME = "test.db"
DB_PATH = os.path.join(PROJECT_ROOT, DB_NAME) # 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_DATABASE_URI = "sqlite:///{0}".format(DB_PATH)
DEBUG = True SQLALCHEMY_ECHO = True
SECRET_KEY = 'shhhh'

Loading…
Cancel
Save