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