/
app_falcon.py
41 lines (39 loc) · 1.23 KB
/
app_falcon.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
#!/usr/bin/env python
# -*- coding:utf8 -*-
import falcon
import json
from codeigniter import CI_Application
ciapp=CI_Application(r'./')
from wsgiref.simple_server import make_server
def dispatch(req,resp):
resp.status = falcon.HTTP_200
resp.body=None
paths=filter(lambda x: x!='',req.path.split('/'))
ctrl_name='index'
func_name='index'
if len(paths)>=2:
ctrl_name=paths[0]
func_name=paths[1]
elif len(paths)==1:
func_name=paths[0]
ctrl=ciapp.loader.ctrl(ctrl_name)
if ctrl==None or not hasattr(ctrl,func_name):
resp.status=falcon.HTTP_404
resp.body="Not Found"
else:
try:
content=getattr(ctrl,func_name)(req,resp)
if resp.body==None:
if isinstance(content,unicode):
resp.body=unicode.encode(content,'utf-8','ignore')
elif isinstance(content,str):
resp.body=content
else:
resp.body=json.dumps(content)
except Exception as er:
resp.status=falcon.HTTP_500
resp.body='Internal Error'
print(er)
app = falcon.API()
app.add_sink(dispatch,'/')
# make_server('0.0.0.0',8000,app).serve_forever()