-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·50 lines (44 loc) · 1.38 KB
/
main.py
File metadata and controls
executable file
·50 lines (44 loc) · 1.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
#!/usr/bin/env python
import json
import logging
import os
import tornado.httpserver
import tornado.ioloop
import tornado.web
import berlin
import ai
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("berlin API, send POST please")
def post(self):
body = self.request.body
logging.debug("received POST: " + body)
request = {
'action': self.get_argument('action'),
'infos': json.loads(self.get_argument('infos')),
'map': json.loads(self.get_argument('map')),
'state': json.loads(self.get_argument('state'))
}
logging.debug("decoded request: " + str(request))
g = berlin.parse_request(request)
if g is None:
self.set_status(500, 'could not parse request')
return
if g.action in ['ping', 'turn']:
response = g.generate_turn()
logging.debug("response: " + str(response))
self.write(str(response))
self.flush()
return
self.set_status(200)
return
def main():
application = tornado.web.Application([
(r"/", MainHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
port = int(os.environ.get("PORT", 5000))
http_server.listen(port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()