]> git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/__init__.py
Verify logins with pyramid_persona.
[xonotic/xonstat.git] / xonstat / __init__.py
1 import sqlahelper
2 from pyramid_beaker import set_cache_regions_from_settings
3 from pyramid.authentication import AuthTktAuthenticationPolicy
4 from pyramid.authorization import ACLAuthorizationPolicy
5 from pyramid.config import Configurator
6 from pyramid.httpexceptions import HTTPNotFound
7 from pyramid.renderers import JSONP
8 from sqlalchemy import engine_from_config
9 from xonstat.models import initialize_db
10 from xonstat.views import *
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)
26
27     # mako for templating
28     config.include('pyramid_mako')
29
30     # Mozilla Persona as the login verifier. It defines default
31     # authentication and authorization policies.
32     config.include('pyramid_persona')
33
34     # for json-encoded responses
35     config.add_renderer('jsonp', JSONP(param_name='callback'))
36
37     # authentication and authorization policies
38     authn_policy = AuthTktAuthenticationPolicy('secret', hashalg='sha512')
39     authz_policy = ACLAuthorizationPolicy()
40     config.set_authentication_policy(authn_policy)
41     config.set_authorization_policy(authz_policy)
42
43     # for static assets
44     config.add_static_view('static', 'xonstat:static')
45
46     # for 404s
47     config.add_view(notfound, context=HTTPNotFound, renderer="404.mako")
48
49     # ROOT ROUTE
50     config.add_route("main_index", "/")
51     config.add_view(main_index, route_name="main_index", renderer="main_index.mako")
52
53     # MAIN SUBMISSION ROUTE
54     config.add_route("submit_stats", "stats/submit")
55     config.add_view(submit_stats, route_name="submit_stats")
56
57     # PLAYER ROUTES
58     config.add_route("player_game_index",      "/player/{player_id:\d+}/games")
59     config.add_view(player_game_index,      route_name="player_game_index",      renderer="player_game_index.mako")
60
61     config.add_route("player_game_index_json", "/player/{player_id:\d+}/games.json")
62     config.add_view(player_game_index_json, route_name="player_game_index_json", renderer="jsonp")
63
64     config.add_route("player_info",      "/player/{id:\d+}")
65     config.add_view(player_info,      route_name="player_info",      renderer="player_info.mako")
66
67     config.add_route("player_info_json", "/player/{id:\d+}.json")
68     config.add_view(player_info_json, route_name="player_info_json", renderer="jsonp")
69
70     config.add_route("player_hashkey_info_text", "/player/me")
71     config.add_view(player_hashkey_info_text, route_name="player_hashkey_info_text", renderer="player_hashkey_info_text.mako")
72
73     config.add_route("player_hashkey_info_json", "/player/me.json")
74     config.add_view(player_hashkey_info_json, route_name="player_hashkey_info_json", renderer="jsonp")
75
76     config.add_route("player_elo_info_text", "/player/{hashkey}/elo.txt")
77     config.add_view(player_elo_info_text, route_name="player_elo_info_text", renderer="player_elo_info_text.mako")
78
79     # FIXME - needs an additional method to convert to JSON
80     config.add_route("player_elo_info_json", "/player/{hashkey}/elo.json")
81     config.add_view(player_elo_info_json, route_name="player_elo_info_json", renderer="jsonp")
82
83     config.add_route("player_accuracy",      "/player/{id:\d+}/accuracy")
84     config.add_view(player_accuracy_json, route_name="player_accuracy",      renderer="jsonp")
85
86     config.add_route("player_index",      "/players")
87     config.add_view(player_index,      route_name="player_index",      renderer="player_index.mako")
88
89     config.add_route("player_index_json", "/players.json")
90     config.add_view(player_index_json, route_name="player_index_json", renderer="jsonp")
91
92     config.add_route("player_captimes",      "/player/{player_id:\d+}/captimes")
93     config.add_view(player_captimes,      route_name="player_captimes",      renderer="player_captimes.mako")
94
95     config.add_route("player_captimes_json", "/player/{player_id:\d+}/captimes.json")
96     config.add_view(player_captimes_json, route_name="player_captimes_json", renderer="jsonp")
97
98     config.add_route("player_weaponstats_data_json", "/player/{id:\d+}/weaponstats.json")
99     config.add_view(player_weaponstats_data_json, route_name="player_weaponstats_data_json", renderer="jsonp")
100
101     config.add_route("top_players_by_time", "/topactive")
102     config.add_view(top_players_by_time, route_name="top_players_by_time", renderer="top_players_by_time.mako")
103
104     config.add_route("top_servers_by_players", "/topservers")
105     config.add_view(top_servers_by_players, route_name="top_servers_by_players", renderer="top_servers_by_players.mako")
106
107     config.add_route("top_maps_by_times_played", "/topmaps")
108     config.add_view(top_maps_by_times_played, route_name="top_maps_by_times_played", renderer="top_maps_by_times_played.mako")
109
110     # GAME ROUTES
111     config.add_route("game_info",      "/game/{id:\d+}")
112     config.add_view(game_info,      route_name="game_info",      renderer="game_info.mako")
113
114     config.add_route("game_info_json", "/game/{id:\d+}.json")
115     config.add_view(game_info_json, route_name="game_info_json", renderer="jsonp")
116
117     config.add_route("rank_index",      "/ranks/{game_type_cd:ctf|dm|tdm|duel|ca|ft}")
118     config.add_view(rank_index,      route_name="rank_index",      renderer="rank_index.mako")
119
120     config.add_route("rank_index_json", "/ranks/{game_type_cd:ctf|dm|tdm|duel|ca|ft}.json")
121     config.add_view(rank_index_json, route_name="rank_index_json", renderer="jsonp")
122
123     config.add_route("game_index", "/games")
124     config.add_view(game_finder, route_name="game_index", renderer="game_finder.mako")
125
126     # SERVER ROUTES
127     config.add_route("server_index",      "/servers")
128     config.add_view(server_index,      route_name="server_index",      renderer="server_index.mako")
129
130     config.add_route("server_index_json", "/servers.json")
131     config.add_view(server_index_json, route_name="server_index_json", renderer="jsonp")
132
133     config.add_route("server_game_index",      "/server/{server_id:\d+}/games/page/{page:\d+}")
134     config.add_view(server_game_index,      route_name="server_game_index",      renderer="server_game_index.mako")
135
136     config.add_route("server_game_index_json", "/server/{server_id:\d+}/games.json")
137     config.add_view(server_game_index_json, route_name="server_game_index_json", renderer="jsonp")
138
139     config.add_route("server_info",      "/server/{id:\d+}")
140     config.add_view(server_info,      route_name="server_info",      renderer="server_info.mako")
141
142     config.add_route("server_info_json", "/server/{id:\d+}.json")
143     config.add_view(server_info_json, route_name="server_info_json", renderer="jsonp")
144
145     # MAP ROUTES
146     config.add_route("map_index",      "/maps")
147     config.add_view(map_index,      route_name="map_index",      renderer="map_index.mako")
148
149     config.add_route("map_index_json", "/maps.json")
150     config.add_view(map_index_json, route_name="map_index_json", renderer="jsonp")
151
152     config.add_route("map_info",      "/map/{id:\d+}")
153     config.add_view(map_info,      route_name="map_info",      renderer="map_info.mako")
154
155     config.add_route("map_info_json", "/map/{id:\d+}.json")
156     config.add_view(map_info_json, route_name="map_info_json", renderer="jsonp")
157
158     config.add_route("map_captimes",      "/map/{id:\d+}/captimes")
159     config.add_view(map_captimes,      route_name="map_captimes",      renderer="map_captimes.mako")
160
161     config.add_route("map_captimes_json", "/map/{id:\d+}/captimes.json")
162     config.add_view(map_captimes_json, route_name="map_captimes_json", renderer="jsonp")
163
164     # SEARCH ROUTES
165     config.add_route("search",      "search")
166     config.add_view(search,      route_name="search",      renderer="search.mako")
167
168     config.add_route("search_json", "search.json")
169     config.add_view(search_json, route_name="search_json", renderer="jsonp")
170
171     # ADMIN ROUTES
172     config.add_forbidden_view(forbidden, renderer="forbidden.mako")
173
174     config.add_route("login", "/login")
175     config.add_view(login, route_name="login", check_csrf=True, renderer="json")
176
177     config.add_route("merge",      "/merge")
178     config.add_view(route_name="merge", renderer="merge.mako", permission="admin")
179
180     return config.make_wsgi_app()