def create_app(configfile=None): # We are using the "Application Factory"-pattern here, which is described # in detail inside the Flask docs: # http://flask.pocoo.org/docs/patterns/appfactories/ app = Flask(__name__) # We use Flask-Appconfig here, but this is not a requirement AppConfig(app, configfile) # Install our Bootstrap extension Bootstrap(app) # Our application uses blueprints as well; these go well with the # application factory. We already imported the blueprint, now we just need # to register it: app.register_blueprint(frontend) # Because we're security-conscious developers, we also hard-code disabling # the CDN support (this might become a default in later versions): app.config['BOOTSTRAP_SERVE_LOCAL'] = True proxied = FlaskReverseProxied(app) # We initialize the navigation as well nav.init_app(app) from models import db # MySQL configurations db.init_app(app) return app
def register_plugins(app, plugins, *, proxied=True): if proxied: proxied = FlaskReverseProxied() plugins = plugins + [proxied] for plugin in plugins: plugin.init_app(app)
def setup_app_routes(app): # Adding CORS middleware app.cors = cors.CORS(app, resources=r'/*', allow_headers='*') # Adding Reverse proxy middleware app.reverse_proxied = FlaskReverseProxied(app) # Temporary disabled until we can confirm if it s useful or not # # # REST API extended to render exceptions as json # # https://gist.github.com/grampajoe/6529609 # # http://www.wiredmonk.me/error-handling-and-logging-in-flask-restful.html # class Api(restful.Api): # def handle_error(self, e): # # Attach the exception to itself so Flask-Restful's error handler # # tries to render it. # if not hasattr(e, 'data'): # TODO : fix/improve this # e.data = e # return super(Api, self).handle_error(e) # # api = restful.Api(app) # # RESTful # app.logger.info('Registering api Blueprint on {0}'.format( app.config.get('BASEPATH', '/ros'))) app.register_blueprint(api_1_blueprint, url_prefix=app.config.get('BASEPATH', '/ros')) # Building BWcompat routes with explicit redirects # TODO : double check, maybe not such a good idea ? #app.add_url_rule(app.config.get('BASEPATH', '/ros'), view_func=generate_redirect('api_0_1.backend', new_endpoint="_redirect_.ros._to_.api_0_1.backend")) # Next API : currently in development #app.logger.info('Registering api Blueprint on /api/v0.2_dev') #app.register_blueprint(api_2_blueprint, url_prefix='/api/v0.2_dev') # Usual Flask : This is not REST # Front Pages # self.app.add_url_rule('/favicon.ico', redirect_to=url_for('static', filename='favicon.ico')) # Follow pluggable views design : http://flask.pocoo.org/docs/0.10/views/ # TODO : maybe add documentation/test pages from OPENAPI ? app.logger.info('Registering main Blueprint') app.register_blueprint(frontend_blueprint, url_prefix='/frontend') app.add_url_rule('/', view_func=generate_redirect( 'main.ros_list', new_endpoint="_redirect_._to_.ros_list")) app.logger.debug('Rostful Rules : {0}'.format(app.url_map)) # Registering app context cleanup function register_teardown(app)
app.config.from_object(config) # Config Workflow # 1. load hardcoded (in source) defaults, minimum expected to not break / crash. # 2. load config from default location (overriding hardcoded defaults). # if not there, create it (at runtime, with the hardcoded default values) # 3. load user provided config from envvar if any, otherwise silently ignore. # 4. load user provided config from command arg if any, if missing file then except (user provided arg is wrong). # if intent is to use file from default location, then no config arg should be provided, # and optional override is available via envvar. # Adding CORS middleware app.cors = cors.CORS(app, resources=r'/*', allow_headers='*') # Adding Reverse proxy middleware app.reverse_proxied = FlaskReverseProxied(app) # Temporary disabled until we can confirm if it s useful or not # # # REST API extended to render exceptions as json # # https://gist.github.com/grampajoe/6529609 # # http://www.wiredmonk.me/error-handling-and-logging-in-flask-restful.html # class Api(restful.Api): # def handle_error(self, e): # # Attach the exception to itself so Flask-Restful's error handler # # tries to render it. # if not hasattr(e, 'data'): # TODO : fix/improve this # e.data = e # return super(Api, self).handle_error(e) # # api = restful.Api(app)
from flask_login import LoginManager, UserMixin from flask_login import login_required, login_user, logout_user from flask_reverse_proxy import FlaskReverseProxied from easysettings import EasySettings from flashforge import FlashForge, FlashForgeError ''' Setup the app and all subsytems ''' #some default values DEFAULT_PASSWORD = '******' #create the app app = Flask('flamo') app.config['SECRET_KEY'] = os.environ.get('FLAMO_SECRET_KEY', 'flamo') proxied = FlaskReverseProxied(app) #login manager login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = 'login' #socket io server socketio = SocketIO(app) #settings settings = EasySettings('flamo.conf') ''' Implementation '''
from importlib import import_module from pkgutil import iter_modules from flask import Flask from .config import DefaultConfig from .models import db, ma from . import blueprints from flask_reverse_proxy import FlaskReverseProxied proxy = FlaskReverseProxied() def create_application(with_blueprints=True): application = Flask(__name__, static_folder=None) application.config.from_object(DefaultConfig()) proxy.init_app(application) db.init_app(application) ma.init_app(application) if with_blueprints: register_blueprints(application) return application def register_blueprints(application): modules = iter_modules(blueprints.__path__, blueprints.__name__ + '.') for _, m, _ in modules: module = import_module(m) application.register_blueprint(module.blueprint)
import os from flask import render_template, Flask, request, redirect, jsonify, url_for from flask_bcrypt import Bcrypt from flask_cors import CORS from flask_reverse_proxy import FlaskReverseProxied from config import Configuration from models import db, ma, Item, History, Account, loggedin, DetailedUserSchema from models import * import click from flask.cli import FlaskGroup proxy = FlaskReverseProxied() app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = Configuration.SQLALCHEMY_DATABASE_URI proxy.init_app(app) db.init_app(app) ma.init_app(app) CORS(app) bcrypt = Bcrypt(app) @click.group(cls=FlaskGroup, create_app=lambda: app) def cli(): """Management script for the flask application."""