Browse Source

Refactor to use factories

master
Trevor Richards 3 years ago
parent
commit
d8909ed7d7
  1. 29
      app.py
  2. 16
      config.py
  3. 38
      factory.py
  4. 7
      launcher.py
  5. 4
      passenger_wsgi.py
  6. 2
      routes/get.py
  7. 11
      routes/post.py
  8. 1
      site.cfg

29
app.py

@ -1,29 +0,0 @@
from flask import Flask
from flask_assets import Environment, Bundle
from routes.get import get
# Instantiate Flask
app = Flask(
__name__,
static_folder='static',
static_path='/static',
template_folder='views'
)
# Apply configurations
app.config.from_pyfile('site.cfg')
assets = Environment(app)
#JS/CSS bundlers for minification
js = Bundle('js/site.js', filters='jsmin', output='js/bundle.min.js')
css = Bundle('css/styles.css', filters='cssmin', output='css/bundle.min.css')
assets.register('js_modules', js)
assets.register('css_modules', css)
# Apply GET method routes
app.register_blueprint(get)
# Run application
if __name__ == '__main__':
app.run()

16
config.py

@ -0,0 +1,16 @@
import os
class BaseConfig:
DEBUG = os.getenv('FLASK_DEBUG', False)
STATIC_PATH = '/static'
STATIC_FOLDER = 'static'
TEMPLATE_FOLDER = 'views'
class DevConfig(BaseConfig):
DEBUG = os.getenv('FLASK_DEBUG', True)
class ProdConfig(BaseConfig):
DEBUG = os.getenv('FLASK_DEBUG', True)

38
factory.py

@ -0,0 +1,38 @@
from config import DevConfig, ProdConfig
from flask import Flask
from flask_assets import Environment, Bundle
from routes.get import get
# from routes.post import post
CONFIGS = {'dev': DevConfig, 'prod': ProdConfig}
def create_app(env):
config = CONFIGS[env]
# Instantiate Flask
app = Flask(
__name__,
template_folder=config.TEMPLATE_FOLDER,
static_path=config.STATIC_PATH,
static_folder=config.STATIC_FOLDER
)
app.config.from_object(config)
assets = Environment(app)
# JS/CSS bundlers for minification
js = Bundle('js/site.js', filters='jsmin', output='js/bundle.min.js')
css = Bundle(
'css/styles.css',
filters='cssmin',
output='css/bundle.min.css'
)
assets.register('js_modules', js)
assets.register('css_modules', css)
# Apply GET method routes
app.register_blueprint(get)
# app.register_blueprint(post)
return app

7
launcher.py

@ -0,0 +1,7 @@
import os
from factory import create_app
app = create_app(os.getenv('FLASK_ENV', 'dev'))
if __name__ == 'main':
app.run()

4
passenger_wsgi.py

@ -1,8 +1,8 @@
import sys, os # noqa
INTERP = '/home/user/<website.com>/env/bin/python'
INTERP = '/home/<username>/<websitefolder>/env/bin/python'
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
sys.path.append(os.getcwd())
from app import app as application # noqa
from launcher import app as application # noqa

2
routes/get.py

@ -5,4 +5,4 @@ get = Blueprint('get', __name__)
@get.route('/')
def home():
return render_template('home.html')
return render_template('home.html')

11
routes/post.py

@ -1 +1,10 @@
# Routes for POST methods.
# Routes for POST methods.
# from flask import Blueprint, request
# post = Blueprint('post', __name__)
# @post.route('/some-route', methods=['POST'])
# def example():
# # Do stuff
# pass

1
site.cfg

@ -1 +0,0 @@
DEBUG = True
Loading…
Cancel
Save