forked from TylerFisher/housing2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
static.py
56 lines (42 loc) · 1.63 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
#!/usr/bin/env python
import json
from mimetypes import guess_type
import os
import subprocess
from flask import abort, make_response
import app_config
import copytext
from flask import Blueprint
from render_utils import BetterJSONEncoder, flatten_app_config
static = Blueprint('static', __name__)
# Render JST templates on-demand
@static.route('/js/templates.js')
def _templates_js():
r = subprocess.check_output(["node_modules/universal-jst/bin/jst.js", "--template", "underscore", "jst"])
return make_response(r, 200, { 'Content-Type': 'application/javascript' })
# Render LESS files on-demand
@static.route('/less/<string:filename>')
def _less(filename):
if not os.path.exists('less/%s' % filename):
abort(404)
r = subprocess.check_output(["node_modules/less/bin/lessc", "less/%s" % filename])
return make_response(r, 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, cls=BetterJSONEncoder)
return make_response(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 make_response(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 make_response(f.read(), 200, { 'Content-Type': guess_type(path)[0] })
except IOError:
abort(404)