working on players
[r15k.git] / r15k / server / server.py
1 # -*- coding: utf-8 -*-
2
3 import os
4 import signal
5 import sys
6 import logging
7 import json
8
9 # root path
10 rootPath = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
11 sys.path.append(os.path.join(rootPath, ".."))
12
13 import tornado.ioloop as loop
14 import tornado.web as web
15
16 from app.api import GamesHandler, PlayersHandler, OrdersHandler, UsersHandler
17
18 # configuration
19 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(name)-25s %(message)s')
20 logger = logging.getLogger('r15k.server')
21 logger.info("Starting with rootPath='%s'" % rootPath)
22
23 class NoCacheStaticFileHandler(web.StaticFileHandler):
24 def set_extra_headers(self, path):
25 # Disable cache
26 self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
27
28 def make_app():
29 import app
30 routes = [
31 (UsersHandler.API_ROUTE_ITEMS, UsersHandler),
32 (UsersHandler.API_ROUTE_ITEM, UsersHandler),
33 (OrdersHandler.API_ROUTE_ITEMS, OrdersHandler),
34 (OrdersHandler.API_ROUTE_ITEM, OrdersHandler),
35 (PlayersHandler.API_ROUTE_ITEMS, PlayersHandler),
36 (PlayersHandler.API_ROUTE_ITEM, PlayersHandler),
37 (GamesHandler.API_ROUTE_ITEMS, GamesHandler),
38 (GamesHandler.API_ROUTE_ITEM, GamesHandler),
39 ]
40 print("Routes\n======\n\n" + json.dumps(
41 [(url, repr(rh)) for url, rh in routes],
42 indent=2)
43 )
44 routes.append((r"/(.*)", NoCacheStaticFileHandler, {"path": os.path.join(rootPath, "www/"), "default_filename": "index.html"}))
45 return web.Application(routes, debug=True)
46
47 routes = get_routes(app)
48 return application
49
50
51 def on_exit(sig, func=None):
52 print "exit handler triggered"
53 sys.exit(1)
54
55 def set_exit_handler(func):
56 signal.signal(signal.SIGTERM, func)
57
58
59 if __name__ == "__main__":
60 set_exit_handler(on_exit)
61
62 app = make_app()
63 app.listen(5000)
64 loop.IOLoop.current().start()
65