]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/__init__.py
planet: Add proper full-text search.
[people/shoehn/ipfire.org.git] / webapp / __init__.py
1 #/usr/bin/python
2
3 import logging
4 import multiprocessing
5 import os.path
6 import simplejson
7 import tornado.httpserver
8 import tornado.locale
9 import tornado.web
10
11 from tornado.options import options
12
13 import backend
14
15 from handlers import *
16 from ui_modules import *
17
18 BASEDIR = os.path.join(os.path.dirname(__file__), "..")
19
20 # Load translations.
21 tornado.locale.load_gettext_translations(os.path.join(BASEDIR, "translations"), "webapp")
22
23 class Application(tornado.web.Application):
24 def __init__(self, **kwargs):
25 self.__backend = None
26
27 settings = dict(
28 cookie_secret = "aXBmaXJlY29va2llc2VjcmV0Cg==",
29 debug = options.debug,
30 gzip = True,
31 login_url = "/login",
32 template_path = os.path.join(BASEDIR, "templates"),
33 ui_methods = {
34 "format_month_name" : self.format_month_name,
35 },
36 ui_modules = {
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,
52 "PlanetSearchBox" : PlanetSearchBoxModule,
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,
63 },
64 xsrf_cookies = True,
65 )
66 settings.update(kwargs)
67
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
77 self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [
78 # Entry site that lead the user to index
79 (r"/", IndexHandler),
80 (r"/index\.?(s?html?)?", RootHandler),
81
82 # Handle news items
83 (r"/news", NewsIndexHandler),
84 (r"/news/year/([0-9]*)", NewsYearHandler),
85 (r"/news/(.*)", NewsItemHandler),
86 (r"/author/(.*)", NewsAuthorHandler),
87
88 # Download sites
89 (r"/download", DownloadHandler),
90 (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }),
91
92 # Handle old pages that have moved elsewhere
93 (r"/screenshots", tornado.web.RedirectHandler, { "url" : "/about" }),
94 (r"/about", tornado.web.RedirectHandler, { "url" : "/features" }),
95 (r"/support", tornado.web.RedirectHandler, { "url" : "/getinvolved" }),
96 (r"/features/.*", tornado.web.RedirectHandler, { "url" : "/features" }),
97
98 (r"/donate", tornado.web.RedirectHandler, { "url" : "/donation" }),
99
100 # RSS feed
101 (r"/news.rss", RSSNewsHandler),
102
103 # Redirection for bookmarks, etc.
104 (r"/(de|en)/(.*)", LangCompatHandler)
105
106 ] + static_handlers + [
107 # Always the last rule
108 (r"/(.*)", StaticHandler),
109 ])
110
111 # downloads.ipfire.org
112 self.add_handlers(r"downloads?\.ipfire\.org", [
113 (r"/", DownloadsIndexHandler),
114 (r"/latest", DownloadsLatestHandler),
115 (r"/release/(\d)", DownloadsReleaseHandler),
116 (r"/release/([\w\.\-]*)", DownloadsReleaseHandler),
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/" }),
121 (r"/download-splash", DownloadSplashHandler),
122 ] + static_handlers + [
123 (r"/(iso|torrent)/(.*)", DownloadCompatHandler),
124 (r"/(.*)", DownloadFileHandler),
125 ])
126
127 # mirrors.ipfire.org
128 self.add_handlers(r"mirrors\.ipfire\.org", [
129 (r"/", MirrorIndexHandler),
130 (r"/mirror/(.*)", MirrorItemHandler),
131 (r"/lists/pakfire2", MirrorListPakfire2Handler),
132 ] + static_handlers)
133
134 # planet.ipfire.org
135 self.add_handlers(r"planet\.ipfire\.org", [
136 (r"/", PlanetMainHandler),
137 (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler),
138 (r"/user/([a-z0-9_-]+)", PlanetUserHandler),
139 (r"/search", PlanetSearchHandler),
140 (r"/year/(\d+)", PlanetYearHandler),
141
142 # RSS
143 (r"/rss", RSSPlanetAllHandler),
144 (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler),
145 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "/rss" }),
146 ] + static_handlers)
147
148 # stasy.ipfire.org
149 self.add_handlers(r"fireinfo\.ipfire\.org", [
150 (r"/", StasyIndexHandler),
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
155 # Send profiles.
156 (r"/send/([a-z0-9]+)", StasyProfileSendHandler),
157
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),
164 (r"/stats/network", StasyStatsNetworkHandler),
165 (r"/stats/oses", StasyStatsOSesHandler),
166 (r"/stats/virtual", StasyStatsVirtualHandler),
167 ] + static_handlers)
168
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
174 # tracker.ipfire.org
175 self.add_handlers(r"(torrent|tracker)\.ipfire\.org", [
176 (r"/", TrackerIndexHandler),
177 (r"/announce.*", TrackerAnnounceHandler),
178 (r"/scrape", TrackerScrapeHandler),
179 (r"/torrent/([0-9a-f]+)", TrackerDetailHandler),
180 (r"/([0-9a-f]{40})", TrackerDetailHandler),
181 (r"/([0-9a-f]{40})/download", TrackerDownloadHandler),
182 ] + static_handlers)
183
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),
192
193 # Static files
194 (r"/(boot\.png|premenu\.cfg|pxelinux\.0|menu\.c32|vesamenu\.c32)",
195 tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }),
196 ])
197
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
204 # wishlist.ipfire.org
205 self.add_handlers(r"wishlist\.ipfire\.org", [
206 (r"/", WishlistIndexHandler),
207 (r"/closed", WishlistClosedHandler),
208 (r"/wish/(.*)/donate", WishDonateHandler),
209 (r"/wish/(.*)", WishHandler),
210 (r"/terms", WishlistTermsHandler),
211 ] + static_handlers)
212
213 # geoip.ipfire.org
214 self.add_handlers(r"geoip\.ipfire\.org", [
215 (r"/", GeoIPHandler),
216 ] + static_handlers)
217
218 # admin.ipfire.org
219 self.add_handlers(r"admin\.ipfire\.org", [
220 (r"/", AdminIndexHandler),
221 (r"/login", AdminLoginHandler),
222 (r"/logout", AdminLogoutHandler),
223 # Accounts
224 (r"/accounts", AdminAccountsHandler),
225 #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler),
226 #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler),
227 # Planet
228 (r"/planet", AdminPlanetHandler),
229 (r"/planet/compose", AdminPlanetComposeHandler),
230 (r"/planet/edit/(.*)", AdminPlanetEditHandler),
231 (r"/planet/publish/(.*)", AdminPlanetPublishHandler),
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),
239 # Fireinfo
240 (r"/fireinfo/stats", AdminFireinfoStatsHandler),
241 # Downloads
242 (r"/downloads", AdminDownloadsHandler),
243 (r"/downloads/mirrors", AdminDownloadsMirrorsHandler),
244 # API
245 (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler)
246 ] + static_handlers)
247
248 # ipfire.org
249 self.add_handlers(r".*", [
250 (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" })
251 ])
252
253 logging.info("Successfully initialied application")
254
255 self.__running = True
256
257 def __del__(self):
258 logging.info("Shutting down application")
259
260 @property
261 def backend(self):
262 if self.__backend is None:
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)
268
269 return self.__backend
270
271 @property
272 def ioloop(self):
273 return tornado.ioloop.IOLoop.instance()
274
275 def shutdown(self, *args):
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)
285
286 num_processes = multiprocessing.cpu_count() / 2
287
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)
292 http_server.start(num_processes=num_processes)
293 else:
294 http_server.listen(port)
295
296 # All requests should be done after 30 seconds or they will be killed.
297 self.ioloop.set_blocking_log_threshold(30)
298
299 self.ioloop.start()
300
301 def reload(self):
302 logging.debug("Caught reload signal")
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