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