f72e5b95b9b8718b48f342ed1fe7bffb0e554508
[xonotic/xonstat.git] / xonstat / __init__.py
1 import sqlahelper
2 from pyramid_beaker import set_cache_regions_from_settings
3 from pyramid.config import Configurator
4 from pyramid.httpexceptions import HTTPNotFound
5 from pyramid.renderers import JSONP
6 from sqlalchemy import engine_from_config
7 from xonstat.models import initialize_db
8 from xonstat.views import *
9 from xonstat.security import *
10
11
12 def main(global_config, **settings):
13     """ This function returns a Pyramid WSGI application.
14     """
15     # setup the database engine
16     engine = engine_from_config(settings, 'sqlalchemy.', pool_size=5)
17     sqlahelper.add_engine(engine)
18
19     # initialize database structures
20     initialize_db(engine)
21
22     # set up beaker cache
23     set_cache_regions_from_settings(settings)
24
25     config = Configurator(settings=settings, root_factory=ACLFactory)
26
27     # mako for templating
28     config.include('pyramid_mako')
29
30     # for json-encoded responses
31     config.add_renderer('jsonp', JSONP(param_name='callback'))
32
33     # for static assets
34     config.add_static_view('static', 'xonstat:static')
35
36     # robots
37     config.add_route("robots", "robots.txt")
38     config.add_view(robots, route_name="robots")
39
40     # for 404s
41     config.add_view(notfound, context=HTTPNotFound, renderer="404.mako")
42
43     # ROOT ROUTE
44     config.add_route("main_index", "/")
45     config.add_view(main_index, route_name="main_index", renderer="main_index.mako")
46
47     # MAIN SUBMISSION ROUTE
48     config.add_route("submit_stats", "stats/submit")
49     config.add_view(submit_stats, route_name="submit_stats", renderer="submit_stats.mako")
50
51     # PLAYER ROUTES
52     config.add_route("player_game_index",      "/player/{player_id:\d+}/games")
53     config.add_view(player_game_index,      route_name="player_game_index",      renderer="player_game_index.mako")
54
55     config.add_route("player_game_index_json", "/player/{player_id:\d+}/games.json")
56     config.add_view(player_game_index_json, route_name="player_game_index_json", renderer="jsonp")
57
58     config.add_route("player_info",      "/player/{id:\d+}")
59     config.add_view(player_info,      route_name="player_info",      renderer="player_info.mako")
60
61     config.add_route("player_info_json", "/player/{id:\d+}.json")
62     config.add_view(player_info_json, route_name="player_info_json", renderer="jsonp")
63
64     config.add_route("player_hashkey_info_text", "/player/me")
65     config.add_view(player_hashkey_info_text, route_name="player_hashkey_info_text", renderer="player_hashkey_info_text.mako")
66
67     config.add_route("player_hashkey_info_json", "/player/me.json")
68     config.add_view(player_hashkey_info_json, route_name="player_hashkey_info_json", renderer="jsonp")
69
70     config.add_route("player_elo_info_text", "/player/{hashkey}/elo.txt")
71     config.add_view(player_elo_info_text, route_name="player_elo_info_text", renderer="player_elo_info_text.mako")
72
73     # FIXME - needs an additional method to convert to JSON
74     config.add_route("player_elo_info_json", "/player/{hashkey}/elo.json")
75     config.add_view(player_elo_info_json, route_name="player_elo_info_json", renderer="jsonp")
76
77     config.add_route("player_accuracy",      "/player/{id:\d+}/accuracy")
78     config.add_view(player_accuracy_json, route_name="player_accuracy",      renderer="jsonp")
79
80     config.add_route("player_index",      "/players")
81     config.add_view(player_index,      route_name="player_index",      renderer="player_index.mako")
82
83     config.add_route("player_index_json", "/players.json")
84     config.add_view(player_index_json, route_name="player_index_json", renderer="jsonp")
85
86     config.add_route("player_captimes",      "/player/{player_id:\d+}/captimes")
87     config.add_view(player_captimes,      route_name="player_captimes",      renderer="player_captimes.mako")
88
89     config.add_route("player_captimes_json", "/player/{player_id:\d+}/captimes.json")
90     config.add_view(player_captimes_json, route_name="player_captimes_json", renderer="jsonp")
91
92     config.add_route("player_weaponstats_data_json", "/player/{id:\d+}/weaponstats.json")
93     config.add_view(player_weaponstats_data_json, route_name="player_weaponstats_data_json", renderer="jsonp")
94
95     config.add_route("top_players_index", "/topactive")
96     config.add_view(top_players_index, route_name="top_players_index", renderer="top_players_index.mako")
97
98     config.add_route("top_servers_index", "/topservers")
99     config.add_view(top_servers_index, route_name="top_servers_index", renderer="top_servers_index.mako")
100
101     config.add_route("top_maps_index", "/topmaps")
102     config.add_view(top_maps_index, route_name="top_maps_index", renderer="top_maps_index.mako")
103
104     config.add_route("player_versus", "/versus")
105     config.add_view(player_versus, route_name="player_versus", renderer="player_versus.mako")
106
107     # GAME ROUTES
108     config.add_route("game_info",      "/game/{id:\d+}")
109     config.add_view(game_info,      route_name="game_info",      renderer="game_info.mako")
110
111     config.add_route("game_info_json", "/game/{id:\d+}.json")
112     config.add_view(game_info_json, route_name="game_info_json", renderer="jsonp")
113
114     config.add_route("rank_index", "/ranks/{game_type_cd}")
115     config.add_view(rank_index,      route_name="rank_index",      renderer="rank_index.mako")
116
117     config.add_route("rank_index_json", "/ranks/{game_type_cd}.json")
118     config.add_view(rank_index_json, route_name="rank_index_json", renderer="jsonp")
119
120     config.add_route("game_index", "/games")
121     config.add_view(game_finder, route_name="game_index", renderer="game_finder.mako")
122
123     config.add_route("game_index_json", "/games.json")
124     config.add_view(game_finder_json, route_name="game_index_json", renderer="jsonp")
125
126     # SERVER ROUTES
127     config.add_route("server_index", "/servers")
128     config.add_view(view=ServerIndex, route_name="server_index", attr="html",
129                     renderer="server_index.mako", accept="text/html")
130     config.add_view(view=ServerIndex, route_name="server_index", attr="json", renderer="json",
131                     accept="application/json")
132
133     config.add_route("server_top_maps", "/server/{id:\d+}/topmaps")
134     config.add_view(view=ServerTopMaps, route_name="server_top_maps", attr="html",
135                     renderer="server_top_maps.mako", accept="text/html")
136     config.add_view(view=ServerTopMaps, route_name="server_top_maps", attr="json", renderer="json",
137                     accept="application/json")
138
139     config.add_route("server_top_active", "/server/{id:\d+}/topactive")
140     config.add_view(view=ServerTopPlayers, route_name="server_top_active", attr="html",
141                     renderer="server_top_active.mako", accept="text/html")
142     config.add_view(view=ServerTopPlayers, route_name="server_top_active", attr="json",
143                     renderer="json", accept="application/json")
144
145     config.add_route("server_top_scorers", "/server/{id:\d+}/topscorers")
146     config.add_view(view=ServerTopScorers, route_name="server_top_scorers", attr="html",
147                     renderer="server_top_scorers.mako", accept="text/html")
148     config.add_view(view=ServerTopScorers, route_name="server_top_scorers", attr="json",
149                     renderer="json", accept="application/json")
150
151     config.add_route("server_info", "/server/{id:\d+}")
152     config.add_view(view=ServerInfo, route_name="server_info", attr="html",
153                     renderer="server_info.mako", accept="text/html")
154     config.add_view(view=ServerInfo, route_name="server_info", attr="json", renderer="json",
155                     accept="application/json")
156
157     # MAP ROUTES
158     config.add_route("map_index", "/maps")
159     config.add_view(view=MapIndex, route_name="map_index", attr="html",
160                     renderer="map_index.mako", accept="text/html")
161     config.add_view(view=MapIndex, route_name="map_index", attr="json", renderer="json",
162                     accept="application/json")
163
164     config.add_route("map_top_scorers", "/map/{id:\d+}/topscorers")
165     config.add_view(view=MapTopScorers, route_name="map_top_scorers", attr="html",
166                     renderer="map_top_scorers.mako", accept="text/html")
167     config.add_view(view=MapTopScorers, route_name="map_top_scorers", attr="json",
168                     renderer="json", accept="application/json")
169
170     config.add_route("map_info",      "/map/{id:\d+}")
171     config.add_view(map_info,      route_name="map_info",      renderer="map_info.mako")
172
173     config.add_route("map_info_json", "/map/{id:\d+}.json")
174     config.add_view(map_info_json, route_name="map_info_json", renderer="jsonp")
175
176     config.add_route("map_captimes",      "/map/{id:\d+}/captimes")
177     config.add_view(map_captimes,      route_name="map_captimes",      renderer="map_captimes.mako")
178
179     config.add_route("map_captimes_json", "/map/{id:\d+}/captimes.json")
180     config.add_view(map_captimes_json, route_name="map_captimes_json", renderer="jsonp")
181
182     # SEARCH ROUTES
183     config.add_route("search",      "search")
184     config.add_view(search,      route_name="search",      renderer="search.mako")
185
186     config.add_route("search_json", "search.json")
187     config.add_view(search_json, route_name="search_json", renderer="jsonp")
188
189     # ADMIN ROUTES
190     config.add_forbidden_view(forbidden, renderer="forbidden.mako")
191
192     config.add_route("login", "/login")
193     config.add_view(login, route_name="login", check_csrf=True, renderer="json")
194
195     config.add_route("merge", "/admin/merge")
196     config.add_view(merge, route_name="merge", renderer="merge.mako", permission="merge")
197
198     return config.make_wsgi_app()