-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserve.py
153 lines (134 loc) · 3.48 KB
/
serve.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import coffeescript, os
from flask import Flask, request
import handler
import handlers
from handlers import *
from model import Config, User
app = Flask(__name__)
app.debug = True
key = Config.getString('secret_key')
if key == None:
key = os.urandom(24)
Config.set('secret_key', ''.join('%02x' % ord(c) for c in key))
else:
key = ''.join(chr(int(key[i:i+2], 16)) for i in xrange(0, 48, 2))
app.secret_key = key
def reroute(noId, withId):
def sub(id=None, *args, **kwargs):
try:
if id == None:
return noId(*args, **kwargs)
else:
return withId(id, *args, **kwargs)
except:
import traceback
traceback.print_exc()
sub.func_name = '__reroute_' + noId.func_name
return sub
for module, sub in handler.all.items():
for name, (method, args, rpc, (noId, withId)) in sub.items():
if module == 'index':
route = '/'
trailing = True
else:
route = '/%s' % module
trailing = False
if name != 'index':
if not trailing:
route += '/'
route += '%s' % name
trailing = False
if noId != None and withId != None:
func = reroute(noId, withId)
elif noId != None:
func = noId
else:
func = withId
if withId != None:
iroute = route
if not trailing:
iroute += '/'
iroute += '<int:id>'
app.route(iroute, methods=[method])(func)
if noId != None:
app.route(route, methods=[method])(func)
@app.route('/favicon.ico')
def favicon():
return file('static/favicon.ico', 'rb').read()
rpcStubTemplate = '''%s: function(%s, callback) {
$.ajax(%r,
{
success: function(data) {
if(callback !== undefined)
callback(data)
},
error: function() {
if(callback !== undefined)
callback()
},
dataType: 'json',
data: {csrf: $csrf, %s},
type: 'POST'
}
)
}'''
cachedRpc = None
@app.route('/rpc.js')
def rpc():
global cachedRpc
if cachedRpc:
return cachedRpc
modules = []
for module, sub in handler.all.items():
module = [module]
for name, (method, args, rpc, funcs) in sub.items():
if not rpc:
continue
func = funcs[0] if funcs[0] else funcs[1]
name = name[4:]
method = rpcStubTemplate % (
name, ', '.join(args),
func.url(),
', '.join('%s: %s' % (arg, arg) for arg in args)
)
module.append(method)
if len(module) > 1:
modules.append(module)
cachedRpc = 'var $rpc = {%s};' % (', '.join('%s: {%s}' % (module[0], ', '.join(module[1:])) for module in modules))
return cachedRpc
@app.route('/scripts/<fn>')
def script(fn):
try:
if not fn.endswith('.js'):
return ''
fn = 'scripts/' + fn[:-3]
if os.path.exists(fn + '.js'):
return file(fn + '.js', 'rb').read()
try:
jstat = os.stat(fn + '.cjs').st_mtime
except:
jstat = None
try:
cstat = os.stat(fn + '.coffee').st_mtime
except:
cstat = None
if jstat == None and cstat == None:
return ''
elif jstat != None and cstat == None or jstat > cstat:
return file(fn + '.cjs', 'rb').read()
source = file(fn + '.coffee', 'rb').read()
try:
source = coffeescript.compile(source)
except Exception, e:
return 'window.location = "/_coffee_error?fn=" + encodeURIComponent(%r) + "&error=" + encodeURIComponent(%r);' % (str(fn + '.coffee'), str(e.message))
file(fn + '.cjs', 'wb').write(source)
return source
except:
import traceback
traceback.print_exc()
@app.route('/_coffee_error')
def coffee_error():
fn, error = request.args['fn'], request.args['error']
return '<h1>Compilation error in %s</h1>%s' % (fn.replace('<', '<'), error.replace('<', '<'))
if __name__=='__main__':
app.run(host='')