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