-
Notifications
You must be signed in to change notification settings - Fork 1
/
static.py
58 lines (44 loc) · 1.51 KB
/
static.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
#!/usr/bin/env python
import json
from mimetypes import guess_type
from flask import abort
import app_config
import copytext
import envoy
from flask import Blueprint
from render_utils import flatten_app_config
static = Blueprint('static', __name__)
# Render JST templates on-demand
@static.route('/js/templates.js')
def _templates_js():
r = envoy.run('node_modules/universal-jst/bin/jst.js --template underscore jst')
return r.std_out, 200, { 'Content-Type': 'application/javascript' }
# Render LESS files on-demand
@static.route('/less/<string:filename>')
def _less(filename):
try:
with open('less/%s' % filename) as f:
less = f.read()
except IOError:
abort(404)
r = envoy.run('node_modules/less/bin/lessc -', data=less)
return r.std_out, 200, { 'Content-Type': 'text/css' }
# Render application configuration
@static.route('/js/app_config.js')
def _app_config_js():
config = flatten_app_config()
js = 'window.APP_CONFIG = ' + json.dumps(config)
return js, 200, { 'Content-Type': 'application/javascript' }
# Render copytext
@static.route('/js/copy.js')
def _copy_js():
copy = 'window.COPY = ' + copytext.Copy(app_config.COPY_PATH).json()
return copy, 200, { 'Content-Type': 'application/javascript' }
# Server arbitrary static files on-demand
@static.route('/<path:path>')
def _static(path):
try:
with open('www/%s' % path) as f:
return f.read(), 200, { 'Content-Type': guess_type(path)[0] }
except IOError:
abort(404)