-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
134 lines (113 loc) · 4.38 KB
/
app.py
File metadata and controls
134 lines (113 loc) · 4.38 KB
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
#!/usr/bin/env python
#-*-coding:u8
import tornado.ioloop
import tornado.web
import time
from tornado.options import options
from tornado import gen
import os
import torndb
import json
import decimal
import sys
import importlib
from config import dbs
def decimal_default(obj):
if isinstance(obj, decimal.Decimal):
return float(obj)
def jd(data):
return json.dumps(data, ensure_ascii=False, default=decimal_default)
def date_format(timestamp):
_format = "%Y-%m-%d %H:%M:%S.%s"
ltime=time.localtime(timestamp)
return time.strftime(_format, ltime)
def _log(handler):
"""
GET show/search?search=haha&name=11
query search=haha&name=11
query_arguments {'search': ['haha'], 'name': ['11']}
POST show/search
Content-Type application/json
body {"gsv":"4.4.4","uid":"3314310","os":"android","ts":"2016-04-20 08:06:15","events":[{"category":"裙子","pv_num":"566","position":"-1","tab_2":"品类","ts":"2016-04-20 03:28:10","tab_1":"category","tab_3":"裙子","eventname":"tab3_c"}
],"push_token":"GT;;d550c23a579ea97332038ea10629ad2a","isp":"tel","appkey":"mxyc","gc":"tengxun","gf":"android","net":"wifi","ip":"","duration":"154","gi":"b2b91ca6-f52e-43af-a5e5-d86467235f84","gv":"6.6.0","gt":"HUAWEI C199s by huanglei","gs":"720x1184","access_token":"jtbYfjVA9A4Ype5tK2rN4C8Kge7lXvY7kePxO0q5gCQ"}
"""
r = handler.request
request_time = 1000.0 * r.request_time()
print r.remote_ip, date_format(r._start_time), handler.get_status(), r.path, request_time, r.headers.get("User-Agent"), r.query, r.query_arguments, r.body, {k:v for (k, v) in r.headers.get_all()}
settings = dict(
#cookie_secret = "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
#login_url= "/login",
#xsrf_cookies= True,
debug = True,
log_function = _log
)
"""
查询方式分为两个方式, 一个是get 一个post
"""
class MainHandler(tornado.web.RequestHandler):
"""
首页
"""
@gen.coroutine
def get(self):
inf = {
"show": "http://127.0.0.1:8888/show/shop/brand_info?__ecs_business_info__business_id=1",
"show_bulk": "http://127.0.0.1:8888/show/shop/brand_info?__ecs_business_info__business_id__in=1,2,3",
"info": "http://127.0.0.1:8888/inf/shop/brand_info?__ecs_business_info__business_id__neq=1",
"info_bulk": "http://127.0.0.1:8888/inf/shop/brand_info?__ecs_business_info__business_id__not_in=1,2,3",
"struct": "http://127.0.0.1:8888/struct/shop/?search=common.categories_v2.cid",
"struct2": "http://127.0.0.1:8888/struct/shop/all?search=common.categories_v2.cid&search=common.categories_v2.name"
}
i = []
for key in inf:
i.append("<a href='%s'>%s : %s</a>" % (inf[key], key, inf[key]))
self.write("<hr>".join(i))
def get_engine(db_name):
info = dbs.get(db_name)
engine_type = info.get("type", "mysql")
module = importlib.import_module("plugin."+ engine_type + "_plugin")
return module.Engine(db_name, **info)
class ShowHandler(tornado.web.RequestHandler):
"""
显示接口查询语句
"""
@gen.coroutine
def get(self, db_name, inf_name):
self.write(get_engine(db_name).show(inf_name, self.request))
class InfHandler(tornado.web.RequestHandler):
"""
查询数据
"""
@gen.coroutine
def get(self, db_name, inf_name):
engine = get_engine(db_name)
res = engine.search(inf_name, self.request)
self.write(jd(res))
class StructHandler(tornado.web.RequestHandler):
"""
获得某个数据库某个表中字段的类型
"""
@gen.coroutine
def get(self, db_name, show_type=None):
"""
show FULL COLUMNS from shop.ecs_business_info where Field="business_id"
"""
engine = get_engine(db_name)
res = engine.struct(show_type, self.request)
self.write(jd(res))
class DebugHandler(tornado.web.RequestHandler):
"""
"""
def get(self, db_name, inf_name):
self.write(get_engine(db_name).debug(inf_name, self.request))
application = tornado.web.Application([
(r"/", MainHandler),
(r"/show/(.*)/(.*)", ShowHandler),
(r"/inf/(.*)/(.*)", InfHandler),
(r"/struct/(.*)/(.*)", StructHandler),
(r"/debug/(.*)/(.*)", DebugHandler),
], **settings)
if __name__ == "__main__":
#print get_conn("shop")
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()