]>
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 = { | |
60024cc8 | 31 | "DownloadButton" : DownloadButtonModule, |
81675874 | 32 | "Menu" : MenuModule, |
940227cb | 33 | "MirrorItem" : MirrorItemModule, |
0673d1b0 | 34 | "MirrorsTable" : MirrorsTableModule, |
81675874 | 35 | "NewsItem" : NewsItemModule, |
940227cb | 36 | "NewsLine" : NewsLineModule, |
d0d074e0 | 37 | "PlanetEntry" : PlanetEntryModule, |
81675874 | 38 | "ReleaseItem" : ReleaseItemModule, |
39 | "SidebarBanner" : SidebarBannerModule, | |
81675874 | 40 | "SidebarRelease" : SidebarReleaseModule, |
372efc19 | 41 | "StasyTable" : StasyTableModule, |
62c3fa48 | 42 | "StasyCPUCoreTable" : StasyCPUCoreTableModule, |
91a446f0 | 43 | "StasyDeviceTable" : StasyDeviceTableModule, |
638e9782 | 44 | "StasyGeoTable" : StasyGeoTableModule, |
940227cb | 45 | "TrackerPeerList": TrackerPeerListModule, |
81675874 | 46 | }, |
47 | xsrf_cookies = True, | |
48 | ) | |
5cf160e0 | 49 | |
ae0228e1 MT |
50 | tornado.web.Application.__init__(self, **settings) |
51 | ||
52 | self.settings["static_path"] = static_path = os.path.join(BASEDIR, "static") | |
53 | static_handlers = [ | |
54 | (r"/static/(.*)", tornado.web.StaticFileHandler, dict(path = static_path)), | |
55 | (r"/(favicon\.ico)", tornado.web.StaticFileHandler, dict(path = static_path)), | |
56 | (r"/(robots\.txt)", tornado.web.StaticFileHandler, dict(path = static_path)), | |
57 | ] | |
58 | ||
d4dc517c | 59 | self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [ |
940227cb MT |
60 | # Entry site that lead the user to index |
61 | (r"/", IndexHandler), | |
62 | (r"/index\.?(s?html?)?", RootHandler), | |
63 | ||
64 | # Handle news items | |
940227cb MT |
65 | (r"/news", NewsIndexHandler), |
66 | (r"/news/(.*)", NewsItemHandler), | |
67 | (r"/author/(.*)", NewsAuthorHandler), | |
68 | ||
81675874 | 69 | # Download sites |
60024cc8 MT |
70 | (r"/download", DownloadHandler), |
71 | (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }), | |
940227cb | 72 | |
8d48f4ef MT |
73 | # Handle old pages that have moved elsewhere |
74 | (r"/screenshots", tornado.web.RedirectHandler, { "url" : "/about" }), | |
60024cc8 MT |
75 | (r"/about", tornado.web.RedirectHandler, { "url" : "/features" }), |
76 | (r"/support", tornado.web.RedirectHandler, { "url" : "/getinvolved" }), | |
77 | ||
78 | (r"/donate", tornado.web.RedirectHandler, { "url" : "/donation" }), | |
8d48f4ef | 79 | |
de683d7c | 80 | # RSS feed |
bcc3ed4d | 81 | (r"/news.rss", RSSNewsHandler), |
de683d7c MT |
82 | |
83 | # Redirection for bookmarks, etc. | |
940227cb MT |
84 | (r"/(de|en)/(.*)", LangCompatHandler) |
85 | ||
86 | ] + static_handlers + [ | |
81675874 | 87 | # Always the last rule |
940227cb MT |
88 | (r"/(.*)", StaticHandler), |
89 | ]) | |
90 | ||
940227cb | 91 | # downloads.ipfire.org |
60024cc8 | 92 | self.add_handlers(r"downloads\.ipfire\.org", [ |
940227cb MT |
93 | (r"/", DownloadsIndexHandler), |
94 | (r"/latest", DownloadsLatestHandler), | |
95 | (r"/release/([0-9]+)", DownloadsReleaseHandler), | |
96 | (r"/older", DownloadsOlderHandler), | |
97 | (r"/development", DownloadsDevelopmentHandler), | |
98 | (r"/mirrors", tornado.web.RedirectHandler, { "url" : "http://mirrors.ipfire.org/" }), | |
99 | (r"/source", tornado.web.RedirectHandler, { "url" : "http://source.ipfire.org/" }), | |
60024cc8 | 100 | (r"/download-splash", DownloadSplashHandler), |
54af860e | 101 | ] + static_handlers + [ |
edd297c4 | 102 | (r"/(iso|torrent)/(.*)", DownloadCompatHandler), |
54af860e MT |
103 | (r"/(.*)", DownloadFileHandler), |
104 | ]) | |
60024cc8 MT |
105 | self.add_handlers(r"download\.ipfire\.org", [ |
106 | (r".*", tornado.web.RedirectHandler, { "url" : "http://downloads.ipfire.org" }) | |
107 | ]) | |
940227cb MT |
108 | |
109 | # mirrors.ipfire.org | |
110 | self.add_handlers(r"mirrors\.ipfire\.org", [ | |
111 | (r"/", MirrorIndexHandler), | |
112 | (r"/mirror/([0-9]+)", MirrorItemHandler), | |
113 | ] + static_handlers) | |
114 | ||
d0d074e0 MT |
115 | # planet.ipfire.org |
116 | self.add_handlers(r"planet\.ipfire\.org", [ | |
940227cb | 117 | (r"/", PlanetMainHandler), |
d0d074e0 | 118 | (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler), |
27066195 | 119 | (r"/user/([a-z0-9_-]+)", PlanetUserHandler), |
bcc3ed4d MT |
120 | |
121 | # RSS | |
122 | (r"/rss", RSSPlanetAllHandler), | |
123 | (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler), | |
d0d074e0 MT |
124 | ] + static_handlers) |
125 | ||
940227cb | 126 | # stasy.ipfire.org |
60024cc8 | 127 | self.add_handlers(r"fireinfo\.ipfire\.org", [ |
940227cb | 128 | (r"/", StasyIndexHandler), |
91a446f0 MT |
129 | (r"/profile/([a-z0-9]{40})", StasyProfileDetailHandler), |
130 | (r"/vendor/(pci|usb)/([0-9a-f]{4})", StasyStatsVendorDetail), | |
131 | (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", StasyStatsModelDetail), | |
132 | ||
133 | # Stats handlers | |
134 | (r"/stats", StasyStatsHandler), | |
135 | (r"/stats/cpus", StasyStatsCPUHandler), | |
136 | (r"/stats/cpuflags", StasyStatsCPUFlagsHandler), | |
137 | (r"/stats/geo", StasyStatsGeoHandler), | |
138 | (r"/stats/memory", StasyStatsMemoryHandler), | |
54af860e | 139 | (r"/stats/network", StasyStatsNetworkHandler), |
91a446f0 MT |
140 | (r"/stats/oses", StasyStatsOSesHandler), |
141 | (r"/stats/virtual", StasyStatsVirtualHandler), | |
ae0228e1 | 142 | ] + static_handlers) |
5cf160e0 | 143 | |
c37ec602 MT |
144 | # i-use.ipfire.org |
145 | self.add_handlers(r"i-use\.ipfire\.org", [ | |
146 | (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage), | |
147 | ]) | |
148 | ||
5cf160e0 | 149 | # tracker.ipfire.org |
940227cb MT |
150 | self.add_handlers(r"(torrent|tracker)\.ipfire\.org", [ |
151 | (r"/", TrackerIndexHandler), | |
43d991f6 | 152 | (r"/a.*", TrackerAnnounceHandler), |
e2afbd6a | 153 | (r"/scrape", TrackerScrapeHandler), |
940227cb | 154 | (r"/torrent/([0-9a-f]+)", TrackerDetailHandler), |
ae0228e1 MT |
155 | ] + static_handlers) |
156 | ||
60024cc8 MT |
157 | # nopaste.ipfire.org |
158 | self.add_handlers(r"nopaste\.ipfire\.org", [ | |
159 | (r"/", NopasteIndexHandler), | |
160 | (r"/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", NopasteEntryHandler), | |
161 | ] + static_handlers) | |
162 | ||
d0d074e0 MT |
163 | # admin.ipfire.org |
164 | self.add_handlers(r"admin\.ipfire\.org", [ | |
165 | (r"/", AdminIndexHandler), | |
940227cb MT |
166 | (r"/login", AdminLoginHandler), |
167 | (r"/logout", AdminLogoutHandler), | |
d0d074e0 MT |
168 | # Accounts |
169 | (r"/accounts", AdminAccountsHandler), | |
940227cb MT |
170 | #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler), |
171 | #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler), | |
d0d074e0 MT |
172 | # Planet |
173 | (r"/planet", AdminPlanetHandler), | |
174 | (r"/planet/compose", AdminPlanetComposeHandler), | |
175 | (r"/planet/edit/([0-9]+)", AdminPlanetEditHandler), | |
940227cb MT |
176 | # Mirrors |
177 | (r"/mirrors", AdminMirrorsHandler), | |
178 | (r"/mirrors/create", AdminMirrorsCreateHandler), | |
179 | (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler), | |
180 | (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler), | |
181 | (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler), | |
182 | (r"/mirrors/update", AdminMirrorsUpdateHandler), | |
71e1ece7 MT |
183 | # Fireinfo |
184 | (r"/fireinfo/stats", AdminFireinfoStatsHandler), | |
60024cc8 MT |
185 | # Downloads |
186 | (r"/downloads", AdminDownloadsHandler), | |
187 | (r"/downloads/mirrors", AdminDownloadsMirrorsHandler), | |
188 | (r"/downloads/test", AdminDownloadsGraphHandler), | |
d0d074e0 MT |
189 | # API |
190 | (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler) | |
191 | ] + static_handlers) | |
192 | ||
ae0228e1 | 193 | # ipfire.org |
3add293a | 194 | self.add_handlers(r".*", [ |
ae0228e1 | 195 | (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" }) |
5cf160e0 | 196 | ]) |
3add293a | 197 | |
feb02477 MT |
198 | logging.info("Successfully initialied application") |
199 | ||
200 | self.__running = True | |
201 | ||
3add293a | 202 | def __del__(self): |
feb02477 MT |
203 | logging.info("Shutting down application") |
204 | ||
feb02477 MT |
205 | @property |
206 | def ioloop(self): | |
207 | return tornado.ioloop.IOLoop.instance() | |
208 | ||
940227cb | 209 | def shutdown(self, *args): |
feb02477 MT |
210 | logging.debug("Caught shutdown signal") |
211 | self.ioloop.stop() | |
212 | ||
213 | self.__running = False | |
214 | ||
215 | def run(self, port=8001): | |
216 | logging.debug("Going to background") | |
217 | ||
218 | http_server = tornado.httpserver.HTTPServer(self, xheaders=True) | |
feb02477 | 219 | |
940227cb MT |
220 | # If we are not running in debug mode, we can actually run multiple |
221 | # frontends to get best performance out of our service. | |
222 | if not self.settings["debug"]: | |
223 | http_server.bind(port) | |
224 | http_server.start(num_processes=4) | |
225 | else: | |
226 | http_server.listen(port) | |
de683d7c MT |
227 | |
228 | # All requests should be done after 30 seconds or they will be killed. | |
229 | self.ioloop.set_blocking_log_threshold(30) | |
feb02477 | 230 | |
940227cb | 231 | self.ioloop.start() |
feb02477 MT |
232 | |
233 | def reload(self): | |
234 | logging.debug("Caught reload signal") |