1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import os
import logging
import logging.handlers
logger = logging.getLogger("")
logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler("flask.log",
maxBytes=3000000, backupCount=2)
formatter = logging.Formatter(
'[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logging.getLogger().addHandler(logging.StreamHandler())
from flask import Flask, send_from_directory, request
from app.settings import app_cfg
def create_demo_app(script_info=None):
"""
functional pattern for creating the flask app
"""
logging.debug("Starting Swimmer demo server...")
app = Flask(__name__, static_folder=app_cfg.DIR_EXPORTS, static_url_path='/')
app.config['SERVER_NAME'] = app_cfg.DEMO_SERVER_NAME
app.url_map.strict_slashes = False
@app.errorhandler(404)
def not_found(error):
path, fn = os.path.split(request.path)
path = path[1:]
dir_path = os.path.join(app_cfg.DIR_EXPORTS, path)
if os.path.isfile(os.path.join(dir_path, fn)):
return send_from_directory(dir_path, fn)
if os.path.isfile(os.path.join(dir_path, fn, 'index.html')):
return send_from_directory(os.path.join(dir_path, fn), 'index.html')
return "404", 404
@app.route('/')
def serve_index():
return "Swimmer demo", 200
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app_cfg.DIR_STATIC, 'img'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
return app
|