]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blame - webapp/__init__.py
boot: Support serial console installations.
[people/shoehn/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,
fa7e1a0a 52 "PlanetSearchBox" : PlanetSearchBoxModule,
9068dba1
MT
53 "ReleaseItem" : ReleaseItemModule,
54 "SidebarBanner" : SidebarBannerModule,
55 "SidebarRelease" : SidebarReleaseModule,
56 "StasyTable" : StasyTableModule,
57 "StasyCPUCoreTable" : StasyCPUCoreTableModule,
58 "StasyDeviceTable" : StasyDeviceTableModule,
59 "StasyGeoTable" : StasyGeoTableModule,
60 "TrackerPeerList" : TrackerPeerListModule,
61 "Wish" : WishModule,
62 "Wishlist" : WishlistModule,
e64ce07e 63 "WishlistItems" : WishlistItemsModule,
81675874 64 },
65 xsrf_cookies = True,
66 )
9068dba1 67 settings.update(kwargs)
5cf160e0 68
ae0228e1
MT
69 tornado.web.Application.__init__(self, **settings)
70
71 self.settings["static_path"] = static_path = os.path.join(BASEDIR, "static")
72 static_handlers = [
73 (r"/static/(.*)", tornado.web.StaticFileHandler, dict(path = static_path)),
74 (r"/(favicon\.ico)", tornado.web.StaticFileHandler, dict(path = static_path)),
75 (r"/(robots\.txt)", tornado.web.StaticFileHandler, dict(path = static_path)),
76 ]
77
d4dc517c 78 self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [
940227cb
MT
79 # Entry site that lead the user to index
80 (r"/", IndexHandler),
81 (r"/index\.?(s?html?)?", RootHandler),
82
83 # Handle news items
940227cb 84 (r"/news", NewsIndexHandler),
7771acea 85 (r"/news/year/([0-9]*)", NewsYearHandler),
940227cb
MT
86 (r"/news/(.*)", NewsItemHandler),
87 (r"/author/(.*)", NewsAuthorHandler),
88
81675874 89 # Download sites
60024cc8
MT
90 (r"/download", DownloadHandler),
91 (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }),
940227cb 92
8d48f4ef
MT
93 # Handle old pages that have moved elsewhere
94 (r"/screenshots", tornado.web.RedirectHandler, { "url" : "/about" }),
60024cc8
MT
95 (r"/about", tornado.web.RedirectHandler, { "url" : "/features" }),
96 (r"/support", tornado.web.RedirectHandler, { "url" : "/getinvolved" }),
c801ee56 97 (r"/features/.*", tornado.web.RedirectHandler, { "url" : "/features" }),
60024cc8 98
e64ce07e
MT
99 # Donate
100 (r"/donate", DonateHandler),
101 (r"/donation", tornado.web.RedirectHandler, { "url" : "/donate" }),
8d48f4ef 102
de683d7c 103 # RSS feed
bcc3ed4d 104 (r"/news.rss", RSSNewsHandler),
de683d7c
MT
105
106 # Redirection for bookmarks, etc.
940227cb
MT
107 (r"/(de|en)/(.*)", LangCompatHandler)
108
109 ] + static_handlers + [
81675874 110 # Always the last rule
940227cb
MT
111 (r"/(.*)", StaticHandler),
112 ])
113
940227cb 114 # downloads.ipfire.org
80594ae3 115 self.add_handlers(r"downloads?(\.dev)?\.ipfire\.org", [
940227cb
MT
116 (r"/", DownloadsIndexHandler),
117 (r"/latest", DownloadsLatestHandler),
9068dba1
MT
118 (r"/release/(\d)", DownloadsReleaseHandler),
119 (r"/release/([\w\.\-]*)", DownloadsReleaseHandler),
940227cb
MT
120 (r"/older", DownloadsOlderHandler),
121 (r"/development", DownloadsDevelopmentHandler),
122 (r"/mirrors", tornado.web.RedirectHandler, { "url" : "http://mirrors.ipfire.org/" }),
123 (r"/source", tornado.web.RedirectHandler, { "url" : "http://source.ipfire.org/" }),
60024cc8 124 (r"/download-splash", DownloadSplashHandler),
54af860e 125 ] + static_handlers + [
edd297c4 126 (r"/(iso|torrent)/(.*)", DownloadCompatHandler),
54af860e
MT
127 (r"/(.*)", DownloadFileHandler),
128 ])
940227cb
MT
129
130 # mirrors.ipfire.org
8fceca0a 131 self.add_handlers(r"mirrors(\.dev)?\.ipfire\.org", [
940227cb 132 (r"/", MirrorIndexHandler),
9068dba1 133 (r"/mirror/(.*)", MirrorItemHandler),
0a003782 134 (r"/lists/pakfire2", MirrorListPakfire2Handler),
940227cb
MT
135 ] + static_handlers)
136
d0d074e0 137 # planet.ipfire.org
7cad1818 138 self.add_handlers(r"planet(\.dev)?\.ipfire\.org", [
940227cb 139 (r"/", PlanetMainHandler),
d0d074e0 140 (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler),
27066195 141 (r"/user/([a-z0-9_-]+)", PlanetUserHandler),
2bdd073f 142 (r"/search", PlanetSearchHandler),
cc3b928d 143 (r"/year/(\d+)", PlanetYearHandler),
bcc3ed4d
MT
144
145 # RSS
146 (r"/rss", RSSPlanetAllHandler),
147 (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler),
20ccd136 148 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "/rss" }),
d0d074e0
MT
149 ] + static_handlers)
150
940227cb 151 # stasy.ipfire.org
8fceca0a 152 self.add_handlers(r"fireinfo(\.dev)?\.ipfire\.org", [
940227cb 153 (r"/", StasyIndexHandler),
91a446f0
MT
154 (r"/profile/([a-z0-9]{40})", StasyProfileDetailHandler),
155 (r"/vendor/(pci|usb)/([0-9a-f]{4})", StasyStatsVendorDetail),
156 (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", StasyStatsModelDetail),
157
30852a9e
MT
158 # Send profiles.
159 (r"/send/([a-z0-9]+)", StasyProfileSendHandler),
160
91a446f0
MT
161 # Stats handlers
162 (r"/stats", StasyStatsHandler),
163 (r"/stats/cpus", StasyStatsCPUHandler),
164 (r"/stats/cpuflags", StasyStatsCPUFlagsHandler),
165 (r"/stats/geo", StasyStatsGeoHandler),
166 (r"/stats/memory", StasyStatsMemoryHandler),
54af860e 167 (r"/stats/network", StasyStatsNetworkHandler),
91a446f0
MT
168 (r"/stats/oses", StasyStatsOSesHandler),
169 (r"/stats/virtual", StasyStatsVirtualHandler),
ae0228e1 170 ] + static_handlers)
5cf160e0 171
c37ec602 172 # i-use.ipfire.org
8fceca0a 173 self.add_handlers(r"i-use(\.dev)?\.ipfire\.org", [
c37ec602
MT
174 (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage),
175 ])
176
5cf160e0 177 # tracker.ipfire.org
8fceca0a 178 self.add_handlers(r"(torrent|tracker)(\.dev)?\.ipfire\.org", [
940227cb 179 (r"/", TrackerIndexHandler),
344a3d62 180 (r"/announce.*", TrackerAnnounceHandler),
e2afbd6a 181 (r"/scrape", TrackerScrapeHandler),
940227cb 182 (r"/torrent/([0-9a-f]+)", TrackerDetailHandler),
fadcfd00
MT
183 (r"/([0-9a-f]{40})", TrackerDetailHandler),
184 (r"/([0-9a-f]{40})/download", TrackerDownloadHandler),
ae0228e1
MT
185 ] + static_handlers)
186
8e2e1261
MT
187 # boot.ipfire.org
188 BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot")
8fceca0a 189 self.add_handlers(r"boot(\.dev)?\.ipfire\.org", [
8e2e1261
MT
190 (r"/", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org/download" }),
191
192 # Configurations
193 (r"/menu.gpxe", MenuGPXEHandler),
194 (r"/menu.cfg", MenuCfgHandler),
8e2e1261
MT
195
196 # Static files
e847f85c 197 (r"/(boot\.png|premenu\.cfg|pxelinux\.0|menu\.c32|vesamenu\.c32)",
8e2e1261
MT
198 tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }),
199 ])
200
60024cc8 201 # nopaste.ipfire.org
8fceca0a 202 self.add_handlers(r"nopaste(\.dev)?\.ipfire\.org", [
60024cc8
MT
203 (r"/", NopasteIndexHandler),
204 (r"/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", NopasteEntryHandler),
205 ] + static_handlers)
206
7771acea 207 # wishlist.ipfire.org
e64ce07e 208 self.add_handlers(r"wishlist(\.dev)?\.ipfire\.org", [
7771acea 209 (r"/", WishlistIndexHandler),
1bf8d482 210 (r"/closed", WishlistClosedHandler),
7771acea
MT
211 (r"/wish/(.*)/donate", WishDonateHandler),
212 (r"/wish/(.*)", WishHandler),
213 (r"/terms", WishlistTermsHandler),
214 ] + static_handlers)
215
9068dba1 216 # geoip.ipfire.org
8fceca0a 217 self.add_handlers(r"geoip(\.dev)?\.ipfire\.org", [
9068dba1
MT
218 (r"/", GeoIPHandler),
219 ] + static_handlers)
220
d0d074e0 221 # admin.ipfire.org
7cad1818 222 self.add_handlers(r"admin(\.dev)?\.ipfire\.org", [
d0d074e0 223 (r"/", AdminIndexHandler),
940227cb
MT
224 (r"/login", AdminLoginHandler),
225 (r"/logout", AdminLogoutHandler),
d0d074e0
MT
226 # Accounts
227 (r"/accounts", AdminAccountsHandler),
940227cb
MT
228 #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler),
229 #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler),
d0d074e0
MT
230 # Planet
231 (r"/planet", AdminPlanetHandler),
232 (r"/planet/compose", AdminPlanetComposeHandler),
67ab72b8
MT
233 (r"/planet/edit/(.*)", AdminPlanetEditHandler),
234 (r"/planet/publish/(.*)", AdminPlanetPublishHandler),
940227cb
MT
235 # Mirrors
236 (r"/mirrors", AdminMirrorsHandler),
237 (r"/mirrors/create", AdminMirrorsCreateHandler),
238 (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler),
239 (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler),
240 (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler),
241 (r"/mirrors/update", AdminMirrorsUpdateHandler),
71e1ece7
MT
242 # Fireinfo
243 (r"/fireinfo/stats", AdminFireinfoStatsHandler),
60024cc8
MT
244 # Downloads
245 (r"/downloads", AdminDownloadsHandler),
246 (r"/downloads/mirrors", AdminDownloadsMirrorsHandler),
d0d074e0
MT
247 # API
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