-
Notifications
You must be signed in to change notification settings - Fork 194
/
public_app.py
65 lines (51 loc) · 1.94 KB
/
public_app.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import app_config
import datetime
import json
import logging
import static
from flask import Flask, make_response, render_template
from render_utils import make_context, smarty_filter, urlencode_filter
from werkzeug.debug import DebuggedApplication
app = Flask(__name__)
app.debug = app_config.DEBUG
logging.basicConfig(format=app_config.LOG_FORMAT)
logger = logging.getLogger(__name__)
logger.setLevel(app_config.LOG_LEVEL)
try:
file_handler = logging.FileHandler('%s/public_app.log' % app_config.SERVER_LOG_PATH)
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
except IOError:
logger.warn('Could not open %s/public_app.log, skipping file-based logging' % app_config.SERVER_LOG_PATH)
app.logger.setLevel(logging.INFO)
app.register_blueprint(static.static, url_prefix='/%s' % app_config.PROJECT_SLUG)
app.add_template_filter(smarty_filter, name='smarty')
app.add_template_filter(urlencode_filter, name='urlencode')
# Example application views
@app.route('/%s/test/' % app_config.PROJECT_SLUG, methods=['GET'])
def _test_app():
"""
Test route for verifying the application is running.
"""
app.logger.info('Test URL requested.')
return make_response(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
# Example of rendering index.html with public_app
@app.route ('/%s/' % app_config.PROJECT_SLUG, methods=['GET'])
def index():
"""
Example view rendering a simple page.
"""
context = make_context(asset_depth=1)
with open('data/featured.json') as f:
context['featured'] = json.load(f)
return make_response(render_template('index.html', **context))
# Enable Werkzeug debug pages
if app_config.DEBUG:
wsgi_app = DebuggedApplication(app, evalex=False)
else:
wsgi_app = app
# Catch attempts to run the app directly
if __name__ == '__main__':
logger.error('This command has been removed! Please run "fab public_app" instead!')