-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
106 lines (85 loc) · 3.01 KB
/
api.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
#coding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os.path
import tornado.auth
import tornado.escape
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.autoreload
import tornado.web
from tornado.options import define, options
import config
#base defines
define(
"port",
default=config.default_port, help="run on the given port", type=int
)
class Application(tornado.web.Application):
def __init__(self):
settings = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
)
handlers = [
(r'/cocos2d/(.*)', tornado.web.StaticFileHandler, {'path': settings['static_path']+'/js/cocos2d/'}),
(r'/src/(.*)', tornado.web.StaticFileHandler, {'path': settings['static_path']+'/js/src/'}),
(r'/res/(.*)', tornado.web.StaticFileHandler, {'path': settings['static_path']+'/res/'}),
(r'/(.*?).js', tornado.web.StaticFileHandler, {'path': settings['static_path']+'/js/'}),
(r"/api_test", APITestHandler),
(r"/cocos2d_test", Cocos2dHandler),
(r"/box2d_test", Box2dHandler),
(r"/template/(\S+?)/", TemplateTestHandler),
(r"/async_test", AsyncTestHandler),
]
#you can define base connetions here, such as mongodb, redis.
self.test_connection = None
tornado.web.Application.__init__(self, handlers, **settings)
class BaseHandler(tornado.web.RequestHandler):
@property
def test_connection(self):
return self.application.test_connection
def testFunc(self, argv):
return argv
#template test
class TemplateTestHandler(BaseHandler):
def get(self, info):
self.render('test.html', info=info)
#cocos2d test
class Cocos2dHandler(BaseHandler):
def get(self):
self.render('index.html', info=None)
#box2d test
class Box2dHandler(BaseHandler):
def get(self):
self.render('box2d_test.html', info=None)
#api test
class APITestHandler(BaseHandler):
def get(self):
word = self.get_argument('kw', 'Hello World')
self.write(word)
#async http request test
class AsyncTestHandler(BaseHandler):
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self):
http_client = tornado.httpclient.AsyncHTTPClient()
response = yield http_client.fetch(
config.testurl,
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) \
AppleWebKit/537.36 (KHTML, like Gecko) \
Chrome/33.0.1750.58 Safari/537.36'})
self.write(response.body)
def main():
tornado.options.parse_command_line()
application = Application()
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
io_loop = tornado.ioloop.IOLoop.instance()
tornado.autoreload.start(io_loop)
io_loop.start()
if __name__ == "__main__":
main()