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