Use jsonp renderer for remote requests (or this API would be pretty moot)
[xonotic/xonstat.git] / xonstat / __init__.py
1 import sqlahelper
2 from pyramid.config import Configurator
3 from pyramid.renderers import JSONP
4 from sqlalchemy import engine_from_config
5 from xonstat.models import initialize_db
6 from xonstat.views import *
7
8 def main(global_config, **settings):
9     """ This function returns a Pyramid WSGI application.
10     """
11     # setup the database engine
12     engine = engine_from_config(settings, 'sqlalchemy.')
13     sqlahelper.add_engine(engine)
14
15     # initialize database structures
16     initialize_db(engine)
17
18     config = Configurator(settings=settings)
19
20     config.add_static_view('static', 'xonstat:static')
21
22     config.add_renderer('jsonp', JSONP(param_name='callback'))
23
24     # ROOT ROUTE
25     config.add_route("main_index", "/")
26     config.add_view(main_index, route_name="main_index", renderer="main_index.mako")
27
28     # MAIN SUBMISSION ROUTE
29     config.add_route("stats_submit", "stats/submit")
30     config.add_view(stats_submit, route_name="stats_submit")
31
32     # PLAYER ROUTES
33     config.add_route("player_game_index", "/player/{player_id:\d+}/games")
34     config.add_view(player_game_index, route_name="player_game_index", renderer="player_game_index.mako")
35
36     config.add_route("player_index", "/players")
37     config.add_view(player_index, route_name="player_index", renderer="player_index.mako")
38
39     config.add_route("player_info", "/player/{id:\d+}")
40     config.add_view(player_info, route_name="player_info", renderer="player_info.mako")
41
42     config.add_route("player_accuracy",      "/player/{id:\d+}/accuracy")
43     config.add_route("player_accuracy_json", "/player/{id:\d+}/accuracy.json")
44     config.add_view(player_accuracy_json, route_name="player_accuracy",      renderer="jsonp")
45     config.add_view(player_accuracy_json, route_name="player_accuracy_json", renderer="jsonp")
46
47     # GAME ROUTES
48     config.add_route("game_index", "/games")
49     config.add_view(game_index, route_name="game_index", renderer="game_index.mako")
50
51     config.add_route("game_info", "/game/{id:\d+}")
52     config.add_view(game_info, route_name="game_info", renderer="game_info.mako")
53
54     config.add_route("rank_index", "/ranks/{game_type_cd:ctf|dm|tdm|duel}")
55     config.add_view(rank_index, route_name="rank_index", renderer="rank_index.mako")
56
57     # SERVER ROUTES
58     config.add_route("server_index", "/servers")
59     config.add_view(server_index, route_name="server_index", renderer="server_index.mako")
60
61     config.add_route("server_game_index", "/server/{server_id:\d+}/games/page/{page:\d+}")
62     config.add_view(server_game_index, route_name="server_game_index", renderer="server_game_index.mako")
63
64     config.add_route("server_info", "/server/{id:\d+}")
65     config.add_view(server_info, route_name="server_info", renderer="server_info.mako")
66
67     # MAP ROUTES
68     config.add_route("map_index",      "/maps")
69     config.add_route("map_index_json", "/maps.json")
70     config.add_view(map_index,      route_name="map_index",      renderer="map_index.mako")
71     config.add_view(map_index_json, route_name="map_index_json", renderer="jsonp")
72
73     config.add_route("map_info", "/map/{id:\d+}")
74     config.add_view(map_info, route_name="map_info", renderer="map_info.mako")
75
76     # SEARCH ROUTES
77     config.add_route("search", "search")
78     config.add_view(search, route_name="search", renderer="search.mako")
79
80     return config.make_wsgi_app()