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