]> git.ipfire.org Git - ipfire.org.git/blame - webapp/__init__.py
Major update of the webapp.
[ipfire.org.git] / webapp / __init__.py
CommitLineData
81675874 1#/usr/bin/python
2
feb02477 3import logging
e6d36e0a 4import multiprocessing
81675874 5import os.path
8ccd2ff0 6import simplejson
feb02477 7import tornado.httpserver
81675874 8import tornado.locale
81675874 9import tornado.web
10
18e60079
MT
11from tornado.options import options
12
940227cb 13import backend
feb02477 14
8ccd2ff0
MT
15from handlers import *
16from ui_modules import *
81675874 17
18BASEDIR = os.path.join(os.path.dirname(__file__), "..")
19
60024cc8
MT
20# Load translations.
21tornado.locale.load_gettext_translations(os.path.join(BASEDIR, "translations"), "webapp")
81675874 22
23class Application(tornado.web.Application):
9068dba1 24 def __init__(self, **kwargs):
a6dc0bad
MT
25 self.__backend = None
26
81675874 27 settings = dict(
28 cookie_secret = "aXBmaXJlY29va2llc2VjcmV0Cg==",
18e60079 29 debug = options.debug,
81675874 30 gzip = True,
d0d074e0 31 login_url = "/login",
81675874 32 template_path = os.path.join(BASEDIR, "templates"),
cc3b928d
MT
33 ui_methods = {
34 "format_month_name" : self.format_month_name,
35 },
81675874 36 ui_modules = {
9068dba1
MT
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,
81675874 62 },
63 xsrf_cookies = True,
64 )
9068dba1 65 settings.update(kwargs)
5cf160e0 66
ae0228e1
MT
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
d4dc517c 76 self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [
940227cb
MT
77 # Entry site that lead the user to index
78 (r"/", IndexHandler),
79 (r"/index\.?(s?html?)?", RootHandler),
80
81 # Handle news items
940227cb 82 (r"/news", NewsIndexHandler),
7771acea 83 (r"/news/year/([0-9]*)", NewsYearHandler),
940227cb
MT
84 (r"/news/(.*)", NewsItemHandler),
85 (r"/author/(.*)", NewsAuthorHandler),
86
81675874 87 # Download sites
60024cc8
MT
88 (r"/download", DownloadHandler),
89 (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }),
940227cb 90
8d48f4ef
MT
91 # Handle old pages that have moved elsewhere
92 (r"/screenshots", tornado.web.RedirectHandler, { "url" : "/about" }),
60024cc8
MT
93 (r"/about", tornado.web.RedirectHandler, { "url" : "/features" }),
94 (r"/support", tornado.web.RedirectHandler, { "url" : "/getinvolved" }),
c801ee56 95 (r"/features/.*", tornado.web.RedirectHandler, { "url" : "/features" }),
60024cc8
MT
96
97 (r"/donate", tornado.web.RedirectHandler, { "url" : "/donation" }),
8d48f4ef 98
de683d7c 99 # RSS feed
bcc3ed4d 100 (r"/news.rss", RSSNewsHandler),
de683d7c
MT
101
102 # Redirection for bookmarks, etc.
940227cb
MT
103 (r"/(de|en)/(.*)", LangCompatHandler)
104
105 ] + static_handlers + [
81675874 106 # Always the last rule
940227cb
MT
107 (r"/(.*)", StaticHandler),
108 ])
109
940227cb 110 # downloads.ipfire.org
2db02063 111 self.add_handlers(r"downloads?\.ipfire\.org", [
940227cb
MT
112 (r"/", DownloadsIndexHandler),
113 (r"/latest", DownloadsLatestHandler),
9068dba1
MT
114 (r"/release/(\d)", DownloadsReleaseHandler),
115 (r"/release/([\w\.\-]*)", DownloadsReleaseHandler),
940227cb
MT
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/" }),
60024cc8 120 (r"/download-splash", DownloadSplashHandler),
54af860e 121 ] + static_handlers + [
edd297c4 122 (r"/(iso|torrent)/(.*)", DownloadCompatHandler),
54af860e
MT
123 (r"/(.*)", DownloadFileHandler),
124 ])
940227cb
MT
125
126 # mirrors.ipfire.org
127 self.add_handlers(r"mirrors\.ipfire\.org", [
128 (r"/", MirrorIndexHandler),
9068dba1 129 (r"/mirror/(.*)", MirrorItemHandler),
0a003782 130 (r"/lists/pakfire2", MirrorListPakfire2Handler),
940227cb
MT
131 ] + static_handlers)
132
d0d074e0
MT
133 # planet.ipfire.org
134 self.add_handlers(r"planet\.ipfire\.org", [
940227cb 135 (r"/", PlanetMainHandler),
d0d074e0 136 (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler),
27066195 137 (r"/user/([a-z0-9_-]+)", PlanetUserHandler),
2bdd073f 138 (r"/search", PlanetSearchHandler),
cc3b928d 139 (r"/year/(\d+)", PlanetYearHandler),
bcc3ed4d 140
a3e3c96d 141 # API
8876f3df 142 (r"/api/planet/search/autocomplete", PlanetAPISearchAutocomplete),
bcc3ed4d
MT
143
144 # RSS
145 (r"/rss", RSSPlanetAllHandler),
146 (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler),
20ccd136 147 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "/rss" }),
d0d074e0
MT
148 ] + static_handlers)
149
940227cb 150 # stasy.ipfire.org
60024cc8 151 self.add_handlers(r"fireinfo\.ipfire\.org", [
940227cb 152 (r"/", StasyIndexHandler),
91a446f0
MT
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
30852a9e
MT
157 # Send profiles.
158 (r"/send/([a-z0-9]+)", StasyProfileSendHandler),
159
91a446f0
MT
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),
54af860e 166 (r"/stats/network", StasyStatsNetworkHandler),
91a446f0
MT
167 (r"/stats/oses", StasyStatsOSesHandler),
168 (r"/stats/virtual", StasyStatsVirtualHandler),
ae0228e1 169 ] + static_handlers)
5cf160e0 170
c37ec602
MT
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
5cf160e0 176 # tracker.ipfire.org
940227cb
MT
177 self.add_handlers(r"(torrent|tracker)\.ipfire\.org", [
178 (r"/", TrackerIndexHandler),
344a3d62 179 (r"/announce.*", TrackerAnnounceHandler),
e2afbd6a 180 (r"/scrape", TrackerScrapeHandler),
940227cb 181 (r"/torrent/([0-9a-f]+)", TrackerDetailHandler),
fadcfd00
MT
182 (r"/([0-9a-f]{40})", TrackerDetailHandler),
183 (r"/([0-9a-f]{40})/download", TrackerDownloadHandler),
ae0228e1
MT
184 ] + static_handlers)
185
8e2e1261
MT
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),
8e2e1261
MT
194
195 # Static files
e847f85c 196 (r"/(boot\.png|premenu\.cfg|pxelinux\.0|menu\.c32|vesamenu\.c32)",
8e2e1261
MT
197 tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }),
198 ])
199
60024cc8
MT
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
7771acea
MT
206 # wishlist.ipfire.org
207 self.add_handlers(r"wishlist\.ipfire\.org", [
208 (r"/", WishlistIndexHandler),
1bf8d482 209 (r"/closed", WishlistClosedHandler),
7771acea
MT
210 (r"/wish/(.*)/donate", WishDonateHandler),
211 (r"/wish/(.*)", WishHandler),
212 (r"/terms", WishlistTermsHandler),
213 ] + static_handlers)
214
9068dba1
MT
215 # geoip.ipfire.org
216 self.add_handlers(r"geoip\.ipfire\.org", [
217 (r"/", GeoIPHandler),
218 ] + static_handlers)
219
d0d074e0
MT
220 # admin.ipfire.org
221 self.add_handlers(r"admin\.ipfire\.org", [
222 (r"/", AdminIndexHandler),
940227cb
MT
223 (r"/login", AdminLoginHandler),
224 (r"/logout", AdminLogoutHandler),
d0d074e0
MT
225 # Accounts
226 (r"/accounts", AdminAccountsHandler),
940227cb
MT
227 #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler),
228 #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler),
d0d074e0
MT
229 # Planet
230 (r"/planet", AdminPlanetHandler),
231 (r"/planet/compose", AdminPlanetComposeHandler),
67ab72b8
MT
232 (r"/planet/edit/(.*)", AdminPlanetEditHandler),
233 (r"/planet/publish/(.*)", AdminPlanetPublishHandler),
940227cb
MT
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),
71e1ece7
MT
241 # Fireinfo
242 (r"/fireinfo/stats", AdminFireinfoStatsHandler),
60024cc8
MT
243 # Downloads
244 (r"/downloads", AdminDownloadsHandler),
245 (r"/downloads/mirrors", AdminDownloadsMirrorsHandler),
d0d074e0 246 # API
8876f3df 247 (r"/api/planet/search/autocomplete", PlanetAPISearchAutocomplete),
d0d074e0
MT
248 (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler)
249 ] + static_handlers)
250
ae0228e1 251 # ipfire.org
3add293a 252 self.add_handlers(r".*", [
ae0228e1 253 (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" })
5cf160e0 254 ])
3add293a 255
feb02477
MT
256 logging.info("Successfully initialied application")
257
258 self.__running = True
259
3add293a 260 def __del__(self):
feb02477
MT
261 logging.info("Shutting down application")
262
a6dc0bad
MT
263 @property
264 def backend(self):
265 if self.__backend is None:
9068dba1
MT
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)
a6dc0bad
MT
271
272 return self.__backend
273
feb02477
MT
274 @property
275 def ioloop(self):
276 return tornado.ioloop.IOLoop.instance()
277
940227cb 278 def shutdown(self, *args):
feb02477
MT
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)
feb02477 288
cef37a4a
MT
289 num_processes = multiprocessing.cpu_count() / 2
290
940227cb
MT
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)
cef37a4a 295 http_server.start(num_processes=num_processes)
940227cb
MT
296 else:
297 http_server.listen(port)
de683d7c
MT
298
299 # All requests should be done after 30 seconds or they will be killed.
300 self.ioloop.set_blocking_log_threshold(30)
feb02477 301
940227cb 302 self.ioloop.start()
feb02477
MT
303
304 def reload(self):
305 logging.debug("Caught reload signal")
cc3b928d
MT
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