def create_app(): app = Flask(__name__, template_folder='templates', static_folder='static') app.config.from_object(config) mako.init_app(app) db.init_app(app) app.session_interface = RedisSessionInterface(cache) app.register_blueprint(backend.bp) app.wsgi_app = DispatcherMiddleware(app.wsgi_app, OrderedDict((('/j', json_api), ))) return app
def create_app(): app = Flask(__name__, template_folder='templates', static_folder='static') app.config.from_object(config) mako.init_app(app) db.init_app(app) app.session_interface = RedisSessionInterface(cache) app.register_blueprint(backend.bp) app.wsgi_app = DispatcherMiddleware(app.wsgi_app, OrderedDict(( ('/j', json_api), ))) return app
partials = config.partials class ErrorHandler(_ErrorHandler): def default(self, request, exception): exc = '\n'.join(traceback.format_tb(sys.exc_info()[-1])) if 'Connection refused' in str(exception) and 'memcache' in exc: exception = ServerError( f'Please confirm that memcached is running!\n{exc}') return super().default(request, exception) app = Sanic(__name__, request_class=Request) app.error_handler = ErrorHandler() app.config.from_object(config) mako.init_app(app, context_processors=()) if sentry is not None: sentry.init_app(app) register_blueprints('views', app) try: register_blueprints('custom', app) except ImportStringError: ... app.static('/static', './static') session = Session() client = None redis = None @app.exception(NotFound)
from werkzeug import SharedDataMiddleware from flask import abort, Flask, request, jsonify, redirect, send_file from ext import db, mako, render_template from models import PasteFile from utils import get_file_path, humanize_bytes ONE_MONTH = 60 * 60 * 24 * 30 app = Flask(__name__, template_folder='templates', static_folder='static') app.config.from_object('config') app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {'/i/': get_file_path()}) mako.init_app(app) db.init_app(app) @app.route('/r/<img_hash>') def rsize(img_hash): w = request.args['w'] h = request.args['h'] old_paste = PasteFile.get_by_filehash(img_hash) new_paste = PasteFile.rsize(old_paste, w, h) return new_paste.url_i @app.route('/d/<filehash>', methods=['GET'])
def create_app(object_name): app = Flask(__name__, template_folder='templates') # here = os.path.abspath(os.path.dirname(__file__)) if os.path.exists('dev'): app.config.from_object(DevConfig) else: app.config.from_object(ProdConfig) eventlet.monkey_patch() mako.init_app(app) db.init_app(app) hashing.init_app(app) admin.init_app(app) login_manager.init_app(app) # csrf.init_app(app) debug_toolbar.init_app(app) cache.init_app(app) SOCKETIO_REDIS_URL = app.config['CELERY_BACKEND_URL'] socketio.init_app(app, async_mode='eventlet', message_queue=SOCKETIO_REDIS_URL) # celery = Celery(app.name) # celery.conf.update(app.config) api.init_app(app) admin.add_view(CustomView(name='Custom')) show_models = [ YjStationInfo, YjPLCInfo, YjGroupInfo, YjVariableInfo, Value, TransferLog, User ] for model in show_models: admin.add_view(CustomModelView(model, db.session, category='models')) admin.add_view( CustomFileAdmin(os.path.join(os.path.dirname(__file__), 'static'), '/static/', name='Static File')) def get_current_user(): return session['username'] @app.errorhandler(500) def server_inner_error(error): return u"内部代码错误 by yakumo17s" def close_db_connection(sender, **extra): db.session.close() # sender.logger.debug('Database close.') request_tearing_down.connect(close_db_connection, app) @app.context_processor def template_extras(): return {'enumerate': enumerate, 'current_user': current_user} @app.template_filter('capitalize') def reverse_filter(s): return s.capitalize() @user_logged_in.connect_via(app) def _track_logins(sender, user, **extra): # 记录用户登录次数,登录IP user.login_count += 1 user.last_login_ip = request.remote_addr user.last_login_time = int(time.time()) db.session.add(user) db.session.commit() @login_manager.user_loader def user_loader(user_id): user = User.query.get(user_id) return user def _get_frame(date_string): db = MySQLdb.connect('localhost', 'web', 'web', 'pyplc') query = 'SELECT * FROM {}'.format(date_string) df = read_sql(query, db) df = df.head(100) return df @app.route( '/db/<any(yjstationinfo, yjplcinfo, yjgroupinfo, yjvariableinfo):date_string>/' ) @cache.cached(timeout=10) def show_tables(date_string=None): df = _get_frame(date_string) if isinstance(df, bool) and not df: return 'Bad data format!' return render_template('show_data.html', df=df.to_html(classes='frame'), date_string=date_string) app.register_blueprint(basic_blueprint) app.register_blueprint(api_blueprint) app.register_blueprint(client_blueprint) return app
from ext import db, mako, render_template from models import PasteFile from utils import get_file_path, humanize_bytes from client import create ONE_MONTH = 60 * 60 * 24 * 30 app = Flask(__name__, template_folder='../../templates/r', static_folder='../../static') app.config.from_object('config') app.wsgi_app = SharedDataMiddleware(app.wsgi_app, { '/i/': get_file_path() }) mako.init_app(app) db.init_app(app) @app.route('/r/<img_hash>') def rsize(img_hash): w = request.args['w'] h = request.args['h'] old_paste = PasteFile.get_by_filehash(img_hash) new_paste = PasteFile.rsize(old_paste, w, h) return new_paste.url_i @app.route('/d/<filehash>', methods=['GET'])
from sanic import Sanic from ext import mako, init_db from config import DEBUG from views import bp app = Sanic(__name__) mako.init_app(app) # 延迟执行 app.blueprint(bp) app.static('/static', './static') # 静态方式设置子路径 @app.listener('before_server_start') # 启动器 async def setup_ab(app, loop): await init_db() # 全局初始化一次,都可以用了 if __name__ == '__main__': app.run(host='0.0.0.0', port=0000, debug=DEBUG)