forked from ramiropolla/capim
/
dispatch.fcgi
67 lines (53 loc) · 1.43 KB
/
dispatch.fcgi
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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import capim
import datetime
import os
import time
import traceback
LOGS_PREFIX = '$BASE_PATH/logs/'
RESPONSE_404_BODY = '''
<!DOCTYPE html>
<html>
<head>
<title>404</title>
</head>
<body>
<center>404 - Arquivo não encontrado</center>
</body>
</html>
'''
def capim_mtime():
return time.ctime(os.path.getmtime('capim.py'))
last_mtime = capim_mtime()
def dispatch(environ, start_response):
global last_mtime
new_mtime = capim_mtime()
if last_mtime != new_mtime:
reload(capim) # Who the fuck commited a non-existent function call?
last_mtime = new_mtime
try:
return capim.run(environ, start_response)
except IOError:
now = datetime.datetime.now()
filename = f'{LOGS_PREFIX}{now.year}_{now.month}_{now.day}.log'
with open(filename, 'a') as fp:
fp.write(
f'{"="*60}\n'
f'{now}\n'
f'{environ}\n'
)
traceback.print_exc(file=fp)
start_response(
'404 Not Found',
[('Content-Type', 'text/html; charset=UTF-8')]
)
return [RESPONSE_404_BODY]
if __name__ == '__main__':
ext = __file__.split('.')[-1]
if ext == 'fcgi':
from flup.server.fcgi_fork import WSGIServer
WSGIServer(dispatch).run()
else:
from wsgiref.handlers import CGIHandler
CGIHandler().run(dispatch)