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