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. 27
      {{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
APP_DIR = os.path.abspath(os.path.dirname(__file__)) # This directory class Config(object):
PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir)) SECRET_KEY = 'shhhh'
DB_NAME = "test.db" APP_DIR = os.path.abspath(os.path.dirname(__file__)) # This directory
DB_PATH = os.path.join(PROJECT_ROOT, DB_NAME) PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir))
SQLALCHEMY_DATABASE_URI = "sqlite:///{0}".format(DB_PATH)
DEBUG = True class ProdConfig(Config):
SECRET_KEY = 'shhhh' 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

Loading…
Cancel
Save