]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blame - webapp/__init__.py
Add donation link to header menu.
[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,
81675874 63 },
64 xsrf_cookies = True,
65 )
9068dba1 66 settings.update(kwargs)
5cf160e0 67
ae0228e1
MT
68 tornado.web.Application.__init__(self, **settings)
69
70 self.settings["static_path"] = static_path = os.path.join(BASEDIR, "static")
71 static_handlers = [
72 (r"/static/(.*)", tornado.web.StaticFileHandler, dict(path = static_path)),
73 (r"/(favicon\.ico)", tornado.web.StaticFileHandler, dict(path = static_path)),
74 (r"/(robots\.txt)", tornado.web.StaticFileHandler, dict(path = static_path)),
75 ]
76
d4dc517c 77 self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [
940227cb
MT
78 # Entry site that lead the user to index
79 (r"/", IndexHandler),
80 (r"/index\.?(s?html?)?", RootHandler),
81
82 # Handle news items
940227cb 83 (r"/news", NewsIndexHandler),
7771acea 84 (r"/news/year/([0-9]*)", NewsYearHandler),
940227cb
MT
85 (r"/news/(.*)", NewsItemHandler),
86 (r"/author/(.*)", NewsAuthorHandler),
87
81675874 88 # Download sites
60024cc8
MT
89 (r"/download", DownloadHandler),
90 (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }),
940227cb 91
8d48f4ef
MT
92 # Handle old pages that have moved elsewhere
93 (r"/screenshots", tornado.web.RedirectHandler, { "url" : "/about" }),
60024cc8
MT
94 (r"/about", tornado.web.RedirectHandler, { "url" : "/features" }),
95 (r"/support", tornado.web.RedirectHandler, { "url" : "/getinvolved" }),
c801ee56 96 (r"/features/.*", tornado.web.RedirectHandler, { "url" : "/features" }),
60024cc8
MT
97
98 (r"/donate", tornado.web.RedirectHandler, { "url" : "/donation" }),
8d48f4ef 99
de683d7c 100 # RSS feed
bcc3ed4d 101 (r"/news.rss", RSSNewsHandler),
de683d7c
MT
102
103 # Redirection for bookmarks, etc.
940227cb
MT
104 (r"/(de|en)/(.*)", LangCompatHandler)
105
106 ] + static_handlers + [
81675874 107 # Always the last rule
940227cb
MT
108 (r"/(.*)", StaticHandler),
109 ])
110
940227cb 111 # downloads.ipfire.org
80594ae3 112 self.add_handlers(r"downloads?(\.dev)?\.ipfire\.org", [
940227cb
MT
113 (r"/", DownloadsIndexHandler),
114 (r"/latest", DownloadsLatestHandler),
9068dba1
MT
115 (r"/release/(\d)", DownloadsReleaseHandler),
116 (r"/release/([\w\.\-]*)", DownloadsReleaseHandler),
940227cb
MT
117 (r"/older", DownloadsOlderHandler),
118 (r"/development", DownloadsDevelopmentHandler),
119 (r"/mirrors", tornado.web.RedirectHandler, { "url" : "http://mirrors.ipfire.org/" }),
120 (r"/source", tornado.web.RedirectHandler, { "url" : "http://source.ipfire.org/" }),
60024cc8 121 (r"/download-splash", DownloadSplashHandler),
54af860e 122 ] + static_handlers + [
edd297c4 123 (r"/(iso|torrent)/(.*)", DownloadCompatHandler),
54af860e
MT
124 (r"/(.*)", DownloadFileHandler),
125 ])
940227cb
MT
126
127 # mirrors.ipfire.org
128 self.add_handlers(r"mirrors\.ipfire\.org", [
129 (r"/", MirrorIndexHandler),
9068dba1 130 (r"/mirror/(.*)", MirrorItemHandler),
0a003782 131 (r"/lists/pakfire2", MirrorListPakfire2Handler),
940227cb
MT
132 ] + static_handlers)
133
d0d074e0 134 # planet.ipfire.org
7cad1818 135 self.add_handlers(r"planet(\.dev)?\.ipfire\.org", [
940227cb 136 (r"/", PlanetMainHandler),
d0d074e0 137 (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler),
27066195 138 (r"/user/([a-z0-9_-]+)", PlanetUserHandler),
2bdd073f 139 (r"/search", PlanetSearchHandler),
cc3b928d 140 (r"/year/(\d+)", PlanetYearHandler),
bcc3ed4d
MT
141
142 # RSS
143 (r"/rss", RSSPlanetAllHandler),
144 (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler),
20ccd136 145 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "/rss" }),
d0d074e0
MT
146 ] + static_handlers)
147
940227cb 148 # stasy.ipfire.org
60024cc8 149 self.add_handlers(r"fireinfo\.ipfire\.org", [
940227cb 150 (r"/", StasyIndexHandler),
91a446f0
MT
151 (r"/profile/([a-z0-9]{40})", StasyProfileDetailHandler),
152 (r"/vendor/(pci|usb)/([0-9a-f]{4})", StasyStatsVendorDetail),
153 (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", StasyStatsModelDetail),
154
30852a9e
MT
155 # Send profiles.
156 (r"/send/([a-z0-9]+)", StasyProfileSendHandler),
157
91a446f0
MT
158 # Stats handlers
159 (r"/stats", StasyStatsHandler),
160 (r"/stats/cpus", StasyStatsCPUHandler),
161 (r"/stats/cpuflags", StasyStatsCPUFlagsHandler),
162 (r"/stats/geo", StasyStatsGeoHandler),
163 (r"/stats/memory", StasyStatsMemoryHandler),
54af860e 164 (r"/stats/network", StasyStatsNetworkHandler),
91a446f0
MT
165 (r"/stats/oses", StasyStatsOSesHandler),
166 (r"/stats/virtual", StasyStatsVirtualHandler),
ae0228e1 167 ] + static_handlers)
5cf160e0 168
c37ec602
MT
169 # i-use.ipfire.org
170 self.add_handlers(r"i-use\.ipfire\.org", [
171 (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage),
172 ])
173
5cf160e0 174 # tracker.ipfire.org
940227cb
MT
175 self.add_handlers(r"(torrent|tracker)\.ipfire\.org", [
176 (r"/", TrackerIndexHandler),
344a3d62 177 (r"/announce.*", TrackerAnnounceHandler),
e2afbd6a 178 (r"/scrape", TrackerScrapeHandler),
940227cb 179 (r"/torrent/([0-9a-f]+)", TrackerDetailHandler),
fadcfd00
MT
180 (r"/([0-9a-f]{40})", TrackerDetailHandler),
181 (r"/([0-9a-f]{40})/download", TrackerDownloadHandler),
ae0228e1
MT
182 ] + static_handlers)
183
8e2e1261
MT
184 # boot.ipfire.org
185 BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot")
186 self.add_handlers(r"boot\.ipfire\.org", [
187 (r"/", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org/download" }),
188
189 # Configurations
190 (r"/menu.gpxe", MenuGPXEHandler),
191 (r"/menu.cfg", MenuCfgHandler),
8e2e1261
MT
192
193 # Static files
e847f85c 194 (r"/(boot\.png|premenu\.cfg|pxelinux\.0|menu\.c32|vesamenu\.c32)",
8e2e1261
MT
195 tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }),
196 ])
197
60024cc8
MT
198 # nopaste.ipfire.org
199 self.add_handlers(r"nopaste\.ipfire\.org", [
200 (r"/", NopasteIndexHandler),
201 (r"/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", NopasteEntryHandler),
202 ] + static_handlers)
203
7771acea
MT
204 # wishlist.ipfire.org
205 self.add_handlers(r"wishlist\.ipfire\.org", [
206 (r"/", WishlistIndexHandler),
1bf8d482 207 (r"/closed", WishlistClosedHandler),
7771acea
MT
208 (r"/wish/(.*)/donate", WishDonateHandler),
209 (r"/wish/(.*)", WishHandler),
210 (r"/terms", WishlistTermsHandler),
211 ] + static_handlers)
212
9068dba1
MT
213 # geoip.ipfire.org
214 self.add_handlers(r"geoip\.ipfire\.org", [
215 (r"/", GeoIPHandler),
216 ] + static_handlers)
217
d0d074e0 218 # admin.ipfire.org
7cad1818 219 self.add_handlers(r"admin(\.dev)?\.ipfire\.org", [
d0d074e0 220 (r"/", AdminIndexHandler),
940227cb
MT
221 (r"/login", AdminLoginHandler),
222 (r"/logout", AdminLogoutHandler),
d0d074e0
MT
223 # Accounts
224 (r"/accounts", AdminAccountsHandler),
940227cb
MT
225 #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler),
226 #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler),
d0d074e0
MT
227 # Planet
228 (r"/planet", AdminPlanetHandler),
229 (r"/planet/compose", AdminPlanetComposeHandler),
67ab72b8
MT
230 (r"/planet/edit/(.*)", AdminPlanetEditHandler),
231 (r"/planet/publish/(.*)", AdminPlanetPublishHandler),
940227cb
MT
232 # Mirrors
233 (r"/mirrors", AdminMirrorsHandler),
234 (r"/mirrors/create", AdminMirrorsCreateHandler),
235 (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler),
236 (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler),
237 (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler),
238 (r"/mirrors/update", AdminMirrorsUpdateHandler),
71e1ece7
MT
239 # Fireinfo
240 (r"/fireinfo/stats", AdminFireinfoStatsHandler),
60024cc8
MT
241 # Downloads
242 (r"/downloads", AdminDownloadsHandler),
243 (r"/downloads/mirrors", AdminDownloadsMirrorsHandler),
d0d074e0
MT
244 # API
245 (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler)
246 ] + static_handlers)
247
ae0228e1 248 # ipfire.org
3add293a 249 self.add_handlers(r".*", [
ae0228e1 250 (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" })
5cf160e0 251 ])
3add293a 252
feb02477
MT
253 logging.info("Successfully initialied application")
254
255 self.__running = True
256
3add293a 257 def __del__(self):
feb02477
MT
258 logging.info("Shutting down application")
259
a6dc0bad
MT
260 @property
261 def backend(self):
262 if self.__backend is None:
9068dba1
MT
263 configfile = self.settings.get("configfile", None)
264 if not configfile:
265 raise RuntimeException("Could not find configuration file")
266
267 self.__backend = backend.Backend(configfile=configfile)
a6dc0bad
MT
268
269 return self.__backend
270
feb02477
MT
271 @property
272 def ioloop(self):
273 return tornado.ioloop.IOLoop.instance()
274
940227cb 275 def shutdown(self, *args):
feb02477
MT
276 logging.debug("Caught shutdown signal")
277 self.ioloop.stop()
278
279 self.__running = False
280
281 def run(self, port=8001):
282 logging.debug("Going to background")
283
284 http_server = tornado.httpserver.HTTPServer(self, xheaders=True)
feb02477 285
cef37a4a
MT
286 num_processes = multiprocessing.cpu_count() / 2
287
940227cb
MT
288 # If we are not running in debug mode, we can actually run multiple
289 # frontends to get best performance out of our service.
290 if not self.settings["debug"]:
291 http_server.bind(port)
cef37a4a 292 http_server.start(num_processes=num_processes)
940227cb
MT
293 else:
294 http_server.listen(port)
de683d7c
MT
295
296 # All requests should be done after 30 seconds or they will be killed.
297 self.ioloop.set_blocking_log_threshold(30)
feb02477 298
940227cb 299 self.ioloop.start()
feb02477
MT
300
301 def reload(self):
302 logging.debug("Caught reload signal")
cc3b928d
MT
303
304 def format_month_name(self, handler, month):
305 _ = handler.locale.translate
306
307 if month == 1:
308 return _("January")
309 elif month == 2:
310 return _("February")
311 elif month == 3:
312 return _("March")
313 elif month == 4:
314 return _("April")
315 elif month == 5:
316 return _("May")
317 elif month == 6:
318 return _("June")
319 elif month == 7:
320 return _("July")
321 elif month == 8:
322 return _("August")
323 elif month == 9:
324 return _("September")
325 elif month == 10:
326 return _("October")
327 elif month == 11:
328 return _("November")
329 elif month == 12:
330 return _("December")
331
332 return month