]> git.ipfire.org Git - ipfire.org.git/blame - webapp/__init__.py
Drop wishlist
[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,
e00c06b9 39 "DonationButton" : DonationButtonModule,
60b0917c 40 "DonationInputBox" : DonationInputBoxModule,
9068dba1 41 "DownloadButton" : DownloadButtonModule,
66862195 42 "LanguageName" : LanguageNameModule,
9068dba1
MT
43 "Map" : MapModule,
44 "Menu" : MenuModule,
45 "MirrorItem" : MirrorItemModule,
46 "MirrorsTable" : MirrorsTableModule,
47 "NetBootMenuConfig" : NetBootMenuConfigModule,
48 "NetBootMenuHeader" : NetBootMenuHeaderModule,
49 "NetBootMenuSeparator" : NetBootMenuSeparatorModule,
50 "NewsItem" : NewsItemModule,
51 "NewsLine" : NewsLineModule,
52 "NewsTable" : NewsTableModule,
53 "NewsYearNavigation" : NewsYearNavigationModule,
d88b8f41 54 "PlanetAuthorBox" : PlanetAuthorBoxModule,
9068dba1 55 "PlanetEntry" : PlanetEntryModule,
fa7e1a0a 56 "PlanetSearchBox" : PlanetSearchBoxModule,
66862195 57 "ProgressBar" : ProgressBarModule,
9068dba1
MT
58 "ReleaseItem" : ReleaseItemModule,
59 "SidebarBanner" : SidebarBannerModule,
60 "SidebarRelease" : SidebarReleaseModule,
66862195
MT
61 "FireinfoDeviceTable" : FireinfoDeviceTableModule,
62 "FireinfoDeviceAndGroupsTable" : FireinfoDeviceAndGroupsTableModule,
63 "FireinfoGeoTable" : FireinfoGeoTableModule,
5ac74b02 64 "TalkContact" : TalkContactModule,
66862195 65 "TalkCallLog" : TalkCallLogModule,
77431b9c 66 "TalkLines" : TalkLinesModule,
66862195 67 "TalkOngoingCalls" : TalkOngoingCallsModule,
81675874 68 },
69 xsrf_cookies = True,
70 )
9068dba1 71 settings.update(kwargs)
5cf160e0 72
ae0228e1
MT
73 tornado.web.Application.__init__(self, **settings)
74
75 self.settings["static_path"] = static_path = os.path.join(BASEDIR, "static")
76 static_handlers = [
77 (r"/static/(.*)", tornado.web.StaticFileHandler, dict(path = static_path)),
78 (r"/(favicon\.ico)", tornado.web.StaticFileHandler, dict(path = static_path)),
79 (r"/(robots\.txt)", tornado.web.StaticFileHandler, dict(path = static_path)),
80 ]
81
66862195
MT
82 authentication_handlers = [
83 (r"/login", LoginHandler),
84 (r"/logout", LogoutHandler),
85 ]
86
d4dc517c 87 self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [
940227cb
MT
88 # Entry site that lead the user to index
89 (r"/", IndexHandler),
90 (r"/index\.?(s?html?)?", RootHandler),
91
92 # Handle news items
940227cb 93 (r"/news", NewsIndexHandler),
7771acea 94 (r"/news/year/([0-9]*)", NewsYearHandler),
940227cb 95 (r"/news/(.*)", NewsItemHandler),
940227cb 96
81675874 97 # Download sites
60b0917c
MT
98 (r"/download", DownloadHandler),
99 (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }),
940227cb 100
8d48f4ef
MT
101 # Handle old pages that have moved elsewhere
102 (r"/screenshots", tornado.web.RedirectHandler, { "url" : "/about" }),
60024cc8 103 (r"/about", tornado.web.RedirectHandler, { "url" : "/features" }),
c801ee56 104 (r"/features/.*", tornado.web.RedirectHandler, { "url" : "/features" }),
60b0917c 105 (r"/getinvolved", tornado.web.RedirectHandler, { "url" : "/get-involved" }),
60024cc8 106
e64ce07e
MT
107 # Donate
108 (r"/donate", DonateHandler),
109 (r"/donation", tornado.web.RedirectHandler, { "url" : "/donate" }),
8d48f4ef 110
f11f1807
MT
111 # Old imprint
112 (r"/imprint", tornado.web.RedirectHandler, { "url" : "/legal" }),
113
de683d7c 114 # RSS feed
bcc3ed4d 115 (r"/news.rss", RSSNewsHandler),
de683d7c
MT
116
117 # Redirection for bookmarks, etc.
940227cb
MT
118 (r"/(de|en)/(.*)", LangCompatHandler)
119
120 ] + static_handlers + [
81675874 121 # Always the last rule
940227cb
MT
122 (r"/(.*)", StaticHandler),
123 ])
124
940227cb 125 # downloads.ipfire.org
80594ae3 126 self.add_handlers(r"downloads?(\.dev)?\.ipfire\.org", [
940227cb
MT
127 (r"/", DownloadsIndexHandler),
128 (r"/latest", DownloadsLatestHandler),
9068dba1
MT
129 (r"/release/(\d)", DownloadsReleaseHandler),
130 (r"/release/([\w\.\-]*)", DownloadsReleaseHandler),
940227cb
MT
131 (r"/older", DownloadsOlderHandler),
132 (r"/development", DownloadsDevelopmentHandler),
133 (r"/mirrors", tornado.web.RedirectHandler, { "url" : "http://mirrors.ipfire.org/" }),
134 (r"/source", tornado.web.RedirectHandler, { "url" : "http://source.ipfire.org/" }),
60024cc8 135 (r"/download-splash", DownloadSplashHandler),
54af860e 136 ] + static_handlers + [
edd297c4 137 (r"/(iso|torrent)/(.*)", DownloadCompatHandler),
54af860e
MT
138 (r"/(.*)", DownloadFileHandler),
139 ])
940227cb
MT
140
141 # mirrors.ipfire.org
8fceca0a 142 self.add_handlers(r"mirrors(\.dev)?\.ipfire\.org", [
940227cb 143 (r"/", MirrorIndexHandler),
9068dba1 144 (r"/mirror/(.*)", MirrorItemHandler),
940227cb
MT
145 ] + static_handlers)
146
d0d074e0 147 # planet.ipfire.org
7cad1818 148 self.add_handlers(r"planet(\.dev)?\.ipfire\.org", [
940227cb 149 (r"/", PlanetMainHandler),
d88b8f41 150 (r"/hottest", PlanetHotEntriesHandler),
d0d074e0 151 (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler),
27066195 152 (r"/user/([a-z0-9_-]+)", PlanetUserHandler),
2bdd073f 153 (r"/search", PlanetSearchHandler),
cc3b928d 154 (r"/year/(\d+)", PlanetYearHandler),
bcc3ed4d
MT
155
156 # RSS
157 (r"/rss", RSSPlanetAllHandler),
158 (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler),
20ccd136 159 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "/rss" }),
d0d074e0
MT
160 ] + static_handlers)
161
66862195 162 # fireinfo.ipfire.org
8fceca0a 163 self.add_handlers(r"fireinfo(\.dev)?\.ipfire\.org", [
66862195
MT
164 (r"/", FireinfoIndexHandler),
165 (r"/device/driver/(.*)", FireinfoDeviceDriverDetail),
166 (r"/device/vendors", FireinfoDeviceVendorsHandler),
167 (r"/device/(pci|usb)/([0-9a-f]{4})", FireinfoDeviceVendorHandler),
168 (r"/device/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", FireinfoDeviceModelHandler),
169
170 # Show profiles
171 (r"/profile/random", FireinfoRandomProfileHandler),
172 (r"/profile/([a-z0-9]{40})", FireinfoProfileDetailHandler),
91a446f0 173
30852a9e 174 # Send profiles.
66862195
MT
175 (r"/send/([a-z0-9]+)", FireinfoProfileSendHandler),
176
177 # Stats handlers
178 (r"/statistics", FireinfoStatsHandler),
179 (r"/statistics/processors", FireinfoStatsProcessorsHandler),
180 (r"/statistics/processors/(arm|x86)", FireinfoStatsProcessorDetailHandler),
181 (r"/statistics/geo-locations", FireinfoStatsGeoHandler),
182 (r"/statistics/languages", FireinfoStatsLanguagesHandler),
183 (r"/statistics/memory", FireinfoStatsMemoryHandler),
184 (r"/statistics/networking", FireinfoStatsNetworkingHandler),
185 (r"/statistics/releases", FireinfoStatsReleasesHandler),
186 (r"/statistics/virtualization", FireinfoStatsVirtualHandler),
187
188 # Compat handlers
189 (r"/stats", tornado.web.RedirectHandler, { "url" : "/statistics" }),
190 (r"/stats/cpus", tornado.web.RedirectHandler, { "url" : "/statistics/processors" }),
191 (r"/stats/geo", tornado.web.RedirectHandler, { "url" : "/statistics/geo-locations" }),
192 (r"/stats/network", tornado.web.RedirectHandler, { "url" : "/statistics/networking" }),
193 (r"/stats/oses", tornado.web.RedirectHandler, { "url" : "/statistics/releases" }),
194 (r"/stats/virtual", tornado.web.RedirectHandler, { "url" : "/statistics/virtualization" }),
195 (r"/vendor/(pci|usb)/([0-9a-f]{4})", FireinfoDeviceVendorCompatHandler),
196 (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", FireinfoDeviceModelCompatHandler),
ae0228e1 197 ] + static_handlers)
5cf160e0 198
c37ec602 199 # i-use.ipfire.org
8fceca0a 200 self.add_handlers(r"i-use(\.dev)?\.ipfire\.org", [
c37ec602
MT
201 (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage),
202 ])
203
8e2e1261
MT
204 # boot.ipfire.org
205 BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot")
8fceca0a 206 self.add_handlers(r"boot(\.dev)?\.ipfire\.org", [
8e2e1261
MT
207 (r"/", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org/download" }),
208
209 # Configurations
37b5c0cf 210 (r"/premenu.cfg", PremenuCfgHandler),
8e2e1261
MT
211 (r"/menu.gpxe", MenuGPXEHandler),
212 (r"/menu.cfg", MenuCfgHandler),
8e2e1261
MT
213
214 # Static files
37b5c0cf 215 (r"/(boot\.png|pxelinux\.0|menu\.c32|vesamenu\.c32)",
8e2e1261
MT
216 tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }),
217 ])
218
60024cc8 219 # nopaste.ipfire.org
8fceca0a 220 self.add_handlers(r"nopaste(\.dev)?\.ipfire\.org", [
66862195
MT
221 (r"/", NopasteCreateHandler),
222 (r"/raw/(.*)", NopasteRawHandler),
223 (r"/view/(.*)", NopasteViewHandler),
224 ] + authentication_handlers + static_handlers)
60024cc8 225
9068dba1 226 # geoip.ipfire.org
8fceca0a 227 self.add_handlers(r"geoip(\.dev)?\.ipfire\.org", [
9068dba1
MT
228 (r"/", GeoIPHandler),
229 ] + static_handlers)
230
66862195
MT
231 # talk.ipfire.org
232 self.add_handlers(r"talk(\.dev)?\.ipfire\.org", [
233 (r"/", TalkIndexHandler),
66862195 234 (r"/conferences", TalkConferencesHandler),
66862195 235 (r"/diagnosis", TalkDiagnosisHandler),
40818cf2 236 (r"/hangup/(.*)", TalkHangupChannelHandler),
66862195
MT
237 (r"/phonebook/(\w+)", TalkPhonebookAccountHandler),
238 (r"/phonebook", TalkPhonebookHandler),
239 (r"/profile", TalkProfileHandler),
66862195
MT
240 ] + authentication_handlers + static_handlers)
241
2cd9af74
MT
242 # accounts.ipfire.org
243 self.add_handlers(r"accounts(\.dev)?\.ipfire\.org", [
244 (r"/avatar/(\w+)\.jpg", AccountsAvatarHandler),
245 ] + static_handlers)
246
d0d074e0 247 # admin.ipfire.org
7cad1818 248 self.add_handlers(r"admin(\.dev)?\.ipfire\.org", [
d0d074e0 249 (r"/", AdminIndexHandler),
d0d074e0
MT
250 # Accounts
251 (r"/accounts", AdminAccountsHandler),
940227cb
MT
252 #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler),
253 #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler),
d0d074e0
MT
254 # Planet
255 (r"/planet", AdminPlanetHandler),
256 (r"/planet/compose", AdminPlanetComposeHandler),
67ab72b8
MT
257 (r"/planet/edit/(.*)", AdminPlanetEditHandler),
258 (r"/planet/publish/(.*)", AdminPlanetPublishHandler),
940227cb
MT
259 # Mirrors
260 (r"/mirrors", AdminMirrorsHandler),
261 (r"/mirrors/create", AdminMirrorsCreateHandler),
262 (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler),
263 (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler),
264 (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler),
265 (r"/mirrors/update", AdminMirrorsUpdateHandler),
71e1ece7 266 # Fireinfo
66862195 267 (r"/fireinfo", AdminFireinfoHandler),
60024cc8
MT
268 # Downloads
269 (r"/downloads", AdminDownloadsHandler),
270 (r"/downloads/mirrors", AdminDownloadsMirrorsHandler),
d0d074e0
MT
271 # API
272 (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler)
66862195 273 ] + authentication_handlers + static_handlers)
d0d074e0 274
ae0228e1 275 # ipfire.org
3add293a 276 self.add_handlers(r".*", [
ae0228e1 277 (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" })
5cf160e0 278 ])
3add293a 279
feb02477
MT
280 logging.info("Successfully initialied application")
281
282 self.__running = True
283
3add293a 284 def __del__(self):
feb02477
MT
285 logging.info("Shutting down application")
286
a6dc0bad
MT
287 @property
288 def backend(self):
289 if self.__backend is None:
9068dba1
MT
290 configfile = self.settings.get("configfile", None)
291 if not configfile:
292 raise RuntimeException("Could not find configuration file")
293
2cd9af74
MT
294 self.__backend = backend.Backend(configfile=configfile,
295 debug=self.settings.get("debug", False))
a6dc0bad
MT
296
297 return self.__backend
298
feb02477
MT
299 @property
300 def ioloop(self):
301 return tornado.ioloop.IOLoop.instance()
302
940227cb 303 def shutdown(self, *args):
feb02477
MT
304 logging.debug("Caught shutdown signal")
305 self.ioloop.stop()
306
307 self.__running = False
308
309 def run(self, port=8001):
310 logging.debug("Going to background")
311
312 http_server = tornado.httpserver.HTTPServer(self, xheaders=True)
feb02477 313
e48a74ea 314 num_processes = multiprocessing.cpu_count()
cef37a4a 315
940227cb
MT
316 # If we are not running in debug mode, we can actually run multiple
317 # frontends to get best performance out of our service.
318 if not self.settings["debug"]:
319 http_server.bind(port)
cef37a4a 320 http_server.start(num_processes=num_processes)
940227cb
MT
321 else:
322 http_server.listen(port)
6aa3b1ec 323
de683d7c
MT
324 # All requests should be done after 30 seconds or they will be killed.
325 self.ioloop.set_blocking_log_threshold(30)
feb02477 326
940227cb 327 self.ioloop.start()
feb02477
MT
328
329 def reload(self):
330 logging.debug("Caught reload signal")
cc3b928d
MT
331
332 def format_month_name(self, handler, month):
333 _ = handler.locale.translate
334
335 if month == 1:
336 return _("January")
337 elif month == 2:
338 return _("February")
339 elif month == 3:
340 return _("March")
341 elif month == 4:
342 return _("April")
343 elif month == 5:
344 return _("May")
345 elif month == 6:
346 return _("June")
347 elif month == 7:
348 return _("July")
349 elif month == 8:
350 return _("August")
351 elif month == 9:
352 return _("September")
353 elif month == 10:
354 return _("October")
355 elif month == 11:
356 return _("November")
357 elif month == 12:
358 return _("December")
359
360 return month