]>
Commit | Line | Data |
---|---|---|
1 | #/usr/bin/python | |
2 | ||
3 | import logging | |
4 | import multiprocessing | |
5 | import os.path | |
6 | import simplejson | |
7 | import tornado.httpserver | |
8 | import tornado.locale | |
9 | import tornado.web | |
10 | ||
11 | from tornado.options import options | |
12 | ||
13 | import backend | |
14 | ||
15 | from handlers import * | |
16 | from ui_modules import * | |
17 | ||
18 | BASEDIR = os.path.join(os.path.dirname(__file__), "..") | |
19 | ||
20 | # Load translations. | |
21 | tornado.locale.load_gettext_translations(os.path.join(BASEDIR, "translations"), "webapp") | |
22 | ||
23 | class Application(tornado.web.Application): | |
24 | def __init__(self, **kwargs): | |
25 | self.__backend = None | |
26 | ||
27 | settings = dict( | |
28 | cookie_secret = "aXBmaXJlY29va2llc2VjcmV0Cg==", | |
29 | debug = options.debug, | |
30 | gzip = True, | |
31 | login_url = "/login", | |
32 | template_path = os.path.join(BASEDIR, "templates"), | |
33 | ui_methods = { | |
34 | "format_month_name" : self.format_month_name, | |
35 | }, | |
36 | ui_modules = { | |
37 | "Advertisement" : AdvertisementModule, | |
38 | "DonationBox" : DonationBoxModule, | |
39 | "DownloadButton" : DownloadButtonModule, | |
40 | "Map" : MapModule, | |
41 | "Menu" : MenuModule, | |
42 | "MirrorItem" : MirrorItemModule, | |
43 | "MirrorsTable" : MirrorsTableModule, | |
44 | "NetBootMenuConfig" : NetBootMenuConfigModule, | |
45 | "NetBootMenuHeader" : NetBootMenuHeaderModule, | |
46 | "NetBootMenuSeparator" : NetBootMenuSeparatorModule, | |
47 | "NewsItem" : NewsItemModule, | |
48 | "NewsLine" : NewsLineModule, | |
49 | "NewsTable" : NewsTableModule, | |
50 | "NewsYearNavigation" : NewsYearNavigationModule, | |
51 | "PlanetEntry" : PlanetEntryModule, | |
52 | "ReleaseItem" : ReleaseItemModule, | |
53 | "SidebarBanner" : SidebarBannerModule, | |
54 | "SidebarRelease" : SidebarReleaseModule, | |
55 | "StasyTable" : StasyTableModule, | |
56 | "StasyCPUCoreTable" : StasyCPUCoreTableModule, | |
57 | "StasyDeviceTable" : StasyDeviceTableModule, | |
58 | "StasyGeoTable" : StasyGeoTableModule, | |
59 | "TrackerPeerList" : TrackerPeerListModule, | |
60 | "Wish" : WishModule, | |
61 | "Wishlist" : WishlistModule, | |
62 | }, | |
63 | xsrf_cookies = True, | |
64 | ) | |
65 | settings.update(kwargs) | |
66 | ||
67 | tornado.web.Application.__init__(self, **settings) | |
68 | ||
69 | self.settings["static_path"] = static_path = os.path.join(BASEDIR, "static") | |
70 | static_handlers = [ | |
71 | (r"/static/(.*)", tornado.web.StaticFileHandler, dict(path = static_path)), | |
72 | (r"/(favicon\.ico)", tornado.web.StaticFileHandler, dict(path = static_path)), | |
73 | (r"/(robots\.txt)", tornado.web.StaticFileHandler, dict(path = static_path)), | |
74 | ] | |
75 | ||
76 | self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [ | |
77 | # Entry site that lead the user to index | |
78 | (r"/", IndexHandler), | |
79 | (r"/index\.?(s?html?)?", RootHandler), | |
80 | ||
81 | # Handle news items | |
82 | (r"/news", NewsIndexHandler), | |
83 | (r"/news/year/([0-9]*)", NewsYearHandler), | |
84 | (r"/news/(.*)", NewsItemHandler), | |
85 | (r"/author/(.*)", NewsAuthorHandler), | |
86 | ||
87 | # Download sites | |
88 | (r"/download", DownloadHandler), | |
89 | (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }), | |
90 | ||
91 | # Handle old pages that have moved elsewhere | |
92 | (r"/screenshots", tornado.web.RedirectHandler, { "url" : "/about" }), | |
93 | (r"/about", tornado.web.RedirectHandler, { "url" : "/features" }), | |
94 | (r"/support", tornado.web.RedirectHandler, { "url" : "/getinvolved" }), | |
95 | (r"/features/.*", tornado.web.RedirectHandler, { "url" : "/features" }), | |
96 | ||
97 | (r"/donate", tornado.web.RedirectHandler, { "url" : "/donation" }), | |
98 | ||
99 | # RSS feed | |
100 | (r"/news.rss", RSSNewsHandler), | |
101 | ||
102 | # Redirection for bookmarks, etc. | |
103 | (r"/(de|en)/(.*)", LangCompatHandler) | |
104 | ||
105 | ] + static_handlers + [ | |
106 | # Always the last rule | |
107 | (r"/(.*)", StaticHandler), | |
108 | ]) | |
109 | ||
110 | # downloads.ipfire.org | |
111 | self.add_handlers(r"downloads?\.ipfire\.org", [ | |
112 | (r"/", DownloadsIndexHandler), | |
113 | (r"/latest", DownloadsLatestHandler), | |
114 | (r"/release/(\d)", DownloadsReleaseHandler), | |
115 | (r"/release/([\w\.\-]*)", DownloadsReleaseHandler), | |
116 | (r"/older", DownloadsOlderHandler), | |
117 | (r"/development", DownloadsDevelopmentHandler), | |
118 | (r"/mirrors", tornado.web.RedirectHandler, { "url" : "http://mirrors.ipfire.org/" }), | |
119 | (r"/source", tornado.web.RedirectHandler, { "url" : "http://source.ipfire.org/" }), | |
120 | (r"/download-splash", DownloadSplashHandler), | |
121 | ] + static_handlers + [ | |
122 | (r"/(iso|torrent)/(.*)", DownloadCompatHandler), | |
123 | (r"/(.*)", DownloadFileHandler), | |
124 | ]) | |
125 | ||
126 | # mirrors.ipfire.org | |
127 | self.add_handlers(r"mirrors\.ipfire\.org", [ | |
128 | (r"/", MirrorIndexHandler), | |
129 | (r"/mirror/(.*)", MirrorItemHandler), | |
130 | (r"/lists/pakfire2", MirrorListPakfire2Handler), | |
131 | ] + static_handlers) | |
132 | ||
133 | # planet.ipfire.org | |
134 | self.add_handlers(r"planet\.ipfire\.org", [ | |
135 | (r"/", PlanetMainHandler), | |
136 | (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler), | |
137 | (r"/user/([a-z0-9_-]+)", PlanetUserHandler), | |
138 | (r"/search", PlanetSearchHandler), | |
139 | (r"/year/(\d+)", PlanetYearHandler), | |
140 | ||
141 | # API | |
142 | (r"/api/planet/search/autocomplete", PlanetAPISearchAutocomplete), | |
143 | ||
144 | # RSS | |
145 | (r"/rss", RSSPlanetAllHandler), | |
146 | (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler), | |
147 | (r"/news.rss", tornado.web.RedirectHandler, { "url" : "/rss" }), | |
148 | ] + static_handlers) | |
149 | ||
150 | # stasy.ipfire.org | |
151 | self.add_handlers(r"fireinfo\.ipfire\.org", [ | |
152 | (r"/", StasyIndexHandler), | |
153 | (r"/profile/([a-z0-9]{40})", StasyProfileDetailHandler), | |
154 | (r"/vendor/(pci|usb)/([0-9a-f]{4})", StasyStatsVendorDetail), | |
155 | (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", StasyStatsModelDetail), | |
156 | ||
157 | # Send profiles. | |
158 | (r"/send/([a-z0-9]+)", StasyProfileSendHandler), | |
159 | ||
160 | # Stats handlers | |
161 | (r"/stats", StasyStatsHandler), | |
162 | (r"/stats/cpus", StasyStatsCPUHandler), | |
163 | (r"/stats/cpuflags", StasyStatsCPUFlagsHandler), | |
164 | (r"/stats/geo", StasyStatsGeoHandler), | |
165 | (r"/stats/memory", StasyStatsMemoryHandler), | |
166 | (r"/stats/network", StasyStatsNetworkHandler), | |
167 | (r"/stats/oses", StasyStatsOSesHandler), | |
168 | (r"/stats/virtual", StasyStatsVirtualHandler), | |
169 | ] + static_handlers) | |
170 | ||
171 | # i-use.ipfire.org | |
172 | self.add_handlers(r"i-use\.ipfire\.org", [ | |
173 | (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage), | |
174 | ]) | |
175 | ||
176 | # tracker.ipfire.org | |
177 | self.add_handlers(r"(torrent|tracker)\.ipfire\.org", [ | |
178 | (r"/", TrackerIndexHandler), | |
179 | (r"/announce.*", TrackerAnnounceHandler), | |
180 | (r"/scrape", TrackerScrapeHandler), | |
181 | (r"/torrent/([0-9a-f]+)", TrackerDetailHandler), | |
182 | (r"/([0-9a-f]{40})", TrackerDetailHandler), | |
183 | (r"/([0-9a-f]{40})/download", TrackerDownloadHandler), | |
184 | ] + static_handlers) | |
185 | ||
186 | # boot.ipfire.org | |
187 | BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot") | |
188 | self.add_handlers(r"boot\.ipfire\.org", [ | |
189 | (r"/", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org/download" }), | |
190 | ||
191 | # Configurations | |
192 | (r"/menu.gpxe", MenuGPXEHandler), | |
193 | (r"/menu.cfg", MenuCfgHandler), | |
194 | ||
195 | # Static files | |
196 | (r"/(boot\.png|premenu\.cfg|pxelinux\.0|menu\.c32|vesamenu\.c32)", | |
197 | tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }), | |
198 | ]) | |
199 | ||
200 | # nopaste.ipfire.org | |
201 | self.add_handlers(r"nopaste\.ipfire\.org", [ | |
202 | (r"/", NopasteIndexHandler), | |
203 | (r"/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", NopasteEntryHandler), | |
204 | ] + static_handlers) | |
205 | ||
206 | # wishlist.ipfire.org | |
207 | self.add_handlers(r"wishlist\.ipfire\.org", [ | |
208 | (r"/", WishlistIndexHandler), | |
209 | (r"/closed", WishlistClosedHandler), | |
210 | (r"/wish/(.*)/donate", WishDonateHandler), | |
211 | (r"/wish/(.*)", WishHandler), | |
212 | (r"/terms", WishlistTermsHandler), | |
213 | ] + static_handlers) | |
214 | ||
215 | # geoip.ipfire.org | |
216 | self.add_handlers(r"geoip\.ipfire\.org", [ | |
217 | (r"/", GeoIPHandler), | |
218 | ] + static_handlers) | |
219 | ||
220 | # admin.ipfire.org | |
221 | self.add_handlers(r"admin\.ipfire\.org", [ | |
222 | (r"/", AdminIndexHandler), | |
223 | (r"/login", AdminLoginHandler), | |
224 | (r"/logout", AdminLogoutHandler), | |
225 | # Accounts | |
226 | (r"/accounts", AdminAccountsHandler), | |
227 | #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler), | |
228 | #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler), | |
229 | # Planet | |
230 | (r"/planet", AdminPlanetHandler), | |
231 | (r"/planet/compose", AdminPlanetComposeHandler), | |
232 | (r"/planet/edit/(.*)", AdminPlanetEditHandler), | |
233 | (r"/planet/publish/(.*)", AdminPlanetPublishHandler), | |
234 | # Mirrors | |
235 | (r"/mirrors", AdminMirrorsHandler), | |
236 | (r"/mirrors/create", AdminMirrorsCreateHandler), | |
237 | (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler), | |
238 | (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler), | |
239 | (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler), | |
240 | (r"/mirrors/update", AdminMirrorsUpdateHandler), | |
241 | # Fireinfo | |
242 | (r"/fireinfo/stats", AdminFireinfoStatsHandler), | |
243 | # Downloads | |
244 | (r"/downloads", AdminDownloadsHandler), | |
245 | (r"/downloads/mirrors", AdminDownloadsMirrorsHandler), | |
246 | # API | |
247 | (r"/api/planet/search/autocomplete", PlanetAPISearchAutocomplete), | |
248 | (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler) | |
249 | ] + static_handlers) | |
250 | ||
251 | # ipfire.org | |
252 | self.add_handlers(r".*", [ | |
253 | (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" }) | |
254 | ]) | |
255 | ||
256 | logging.info("Successfully initialied application") | |
257 | ||
258 | self.__running = True | |
259 | ||
260 | def __del__(self): | |
261 | logging.info("Shutting down application") | |
262 | ||
263 | @property | |
264 | def backend(self): | |
265 | if self.__backend is None: | |
266 | configfile = self.settings.get("configfile", None) | |
267 | if not configfile: | |
268 | raise RuntimeException("Could not find configuration file") | |
269 | ||
270 | self.__backend = backend.Backend(configfile=configfile) | |
271 | ||
272 | return self.__backend | |
273 | ||
274 | @property | |
275 | def ioloop(self): | |
276 | return tornado.ioloop.IOLoop.instance() | |
277 | ||
278 | def shutdown(self, *args): | |
279 | logging.debug("Caught shutdown signal") | |
280 | self.ioloop.stop() | |
281 | ||
282 | self.__running = False | |
283 | ||
284 | def run(self, port=8001): | |
285 | logging.debug("Going to background") | |
286 | ||
287 | http_server = tornado.httpserver.HTTPServer(self, xheaders=True) | |
288 | ||
289 | num_processes = multiprocessing.cpu_count() / 2 | |
290 | ||
291 | # If we are not running in debug mode, we can actually run multiple | |
292 | # frontends to get best performance out of our service. | |
293 | if not self.settings["debug"]: | |
294 | http_server.bind(port) | |
295 | http_server.start(num_processes=num_processes) | |
296 | else: | |
297 | http_server.listen(port) | |
298 | ||
299 | # All requests should be done after 30 seconds or they will be killed. | |
300 | self.ioloop.set_blocking_log_threshold(30) | |
301 | ||
302 | self.ioloop.start() | |
303 | ||
304 | def reload(self): | |
305 | logging.debug("Caught reload signal") | |
306 | ||
307 | def format_month_name(self, handler, month): | |
308 | _ = handler.locale.translate | |
309 | ||
310 | if month == 1: | |
311 | return _("January") | |
312 | elif month == 2: | |
313 | return _("February") | |
314 | elif month == 3: | |
315 | return _("March") | |
316 | elif month == 4: | |
317 | return _("April") | |
318 | elif month == 5: | |
319 | return _("May") | |
320 | elif month == 6: | |
321 | return _("June") | |
322 | elif month == 7: | |
323 | return _("July") | |
324 | elif month == 8: | |
325 | return _("August") | |
326 | elif month == 9: | |
327 | return _("September") | |
328 | elif month == 10: | |
329 | return _("October") | |
330 | elif month == 11: | |
331 | return _("November") | |
332 | elif month == 12: | |
333 | return _("December") | |
334 | ||
335 | return month |