-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (52 loc) · 1.98 KB
/
app.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
import json
from urllib.parse import parse_qs
from app.libs.logger import LoggerHandler
from app.libs.variable import ROUTE, request
from app.libs.response import show_reponse, Status
from app.setting import PORT
# https://stackoverflow.com/questions/43393764/python-3-6-project-structure-leads-to-runtimewarning
# registy router first (can't import in app/__init__.py)
from app.api.web import *
from app.api.admin import *
from app.api.weixin import *
logger = LoggerHandler("app")
def application(environ, start_response):
url = environ.get("PATH_INFO")
method = environ.get("REQUEST_METHOD")
qs = environ.get("QUERY_STRING")
length = environ.get("CONTENT_LENGTH", "0")
length = 0 if length == "" else int(length)
data = environ["wsgi.input"].read(length)
headers = {"method": method, "X-Token": environ.get("HTTP_X_TOKEN")}
global request
request["headers"] = headers
request["qs"] = parse_qs(qs)
if method.lower() == "post" and data:
try:
request["data"] = json.loads(data.decode())
except Exception as e:
logger.error(e)
# xml,form
request["data"] = data.decode()
status = "200 OK"
headers = [
("Content-type", "application/json; charset=utf-8"),
("Access-Control-Allow-Headers", "*"),
("Access-Control-Allow-Origin", "*"),
("Access-Control-Max-Age", "600"), # cache preflight request 10min
]
start_response(status, headers)
try:
data = ROUTE[url]()
if type(data) is str:
return [data.encode("utf-8")]
return [json.dumps(data).encode("utf-8")]
except Exception as e:
err = show_reponse(code=Status.other, message=f"{e}")
logger.error(e)
return [json.dumps(err).encode("utf-8")]
if __name__ == "__main__":
from wsgiref.simple_server import make_server
with make_server("", PORT, application) as httpd:
logger.info(f"Serving on port {PORT}...")
httpd.serve_forever()