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