]>
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 |
8 | import tornado.options | |
9 | import tornado.web | |
10 | ||
940227cb | 11 | import backend |
feb02477 | 12 | |
8ccd2ff0 MT |
13 | from handlers import * |
14 | from ui_modules import * | |
81675874 | 15 | |
16 | BASEDIR = os.path.join(os.path.dirname(__file__), "..") | |
17 | ||
940227cb MT |
18 | # Enable logging |
19 | tornado.options.enable_pretty_logging() | |
20 | tornado.options.parse_command_line() | |
21 | ||
81675874 | 22 | tornado.locale.load_translations(os.path.join(BASEDIR, "translations")) |
81675874 | 23 | |
24 | class Application(tornado.web.Application): | |
25 | def __init__(self): | |
81675874 | 26 | settings = dict( |
27 | cookie_secret = "aXBmaXJlY29va2llc2VjcmV0Cg==", | |
940227cb | 28 | debug = True, |
81675874 | 29 | gzip = True, |
d0d074e0 | 30 | login_url = "/login", |
81675874 | 31 | template_path = os.path.join(BASEDIR, "templates"), |
32 | ui_modules = { | |
81675874 | 33 | "Menu" : MenuModule, |
940227cb | 34 | "MirrorItem" : MirrorItemModule, |
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, |
91a446f0 | 42 | "StasyDeviceTable" : StasyDeviceTableModule, |
940227cb | 43 | "TrackerPeerList": TrackerPeerListModule, |
81675874 | 44 | }, |
45 | xsrf_cookies = True, | |
46 | ) | |
5cf160e0 | 47 | |
ae0228e1 MT |
48 | tornado.web.Application.__init__(self, **settings) |
49 | ||
50 | self.settings["static_path"] = static_path = os.path.join(BASEDIR, "static") | |
51 | static_handlers = [ | |
52 | (r"/static/(.*)", tornado.web.StaticFileHandler, dict(path = static_path)), | |
53 | (r"/(favicon\.ico)", tornado.web.StaticFileHandler, dict(path = static_path)), | |
54 | (r"/(robots\.txt)", tornado.web.StaticFileHandler, dict(path = static_path)), | |
55 | ] | |
56 | ||
d4dc517c | 57 | self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [ |
940227cb MT |
58 | # Entry site that lead the user to index |
59 | (r"/", IndexHandler), | |
60 | (r"/index\.?(s?html?)?", RootHandler), | |
61 | ||
62 | # Handle news items | |
63 | #(r"/news/(.*)", NewsRedirectHandler), | |
64 | (r"/news", NewsIndexHandler), | |
65 | (r"/news/(.*)", NewsItemHandler), | |
66 | (r"/author/(.*)", NewsAuthorHandler), | |
67 | ||
81675874 | 68 | # Download sites |
940227cb MT |
69 | (r"/downloads?", DownloadHandler), |
70 | # # RSS feed | |
71 | # (r"/([A-Za-z]{2})/news.rss", RSSHandler), | |
72 | # (r"/data/feeds/main-([A-Za-z]{2}).rss", RSSHandler), | |
73 | ||
74 | (r"/(de|en)/(.*)", LangCompatHandler) | |
75 | ||
76 | ] + static_handlers + [ | |
81675874 | 77 | # Always the last rule |
940227cb MT |
78 | (r"/(.*)", StaticHandler), |
79 | ]) | |
80 | ||
81 | # news.ipfire.org | |
82 | #self.add_handlers(r"news\.ipfire\.org", [ | |
83 | # (r"/", NewsIndexHandler), | |
84 | # (r"/news/(.*)", NewsItemHandler), | |
85 | # (r"/author/(.*)", NewsAuthorHandler), | |
86 | #] + static_handlers) | |
81675874 | 87 | |
5cf160e0 | 88 | # download.ipfire.org |
ae0228e1 | 89 | self.add_handlers(r"download\.ipfire\.org", [ |
3add293a MT |
90 | (r"/", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org/" }), |
91 | (r"/(favicon\.ico)", tornado.web.StaticFileHandler, dict(path = static_path)), | |
92 | (r"/(.*)", DownloadFileHandler), | |
93 | ]) | |
5cf160e0 | 94 | |
940227cb MT |
95 | # downloads.ipfire.org |
96 | self.add_handlers(r"downloads\.ipfire\.org", [ | |
97 | (r"/", DownloadsIndexHandler), | |
98 | (r"/latest", DownloadsLatestHandler), | |
99 | (r"/release/([0-9]+)", DownloadsReleaseHandler), | |
100 | (r"/older", DownloadsOlderHandler), | |
101 | (r"/development", DownloadsDevelopmentHandler), | |
102 | (r"/mirrors", tornado.web.RedirectHandler, { "url" : "http://mirrors.ipfire.org/" }), | |
103 | (r"/source", tornado.web.RedirectHandler, { "url" : "http://source.ipfire.org/" }), | |
104 | ] + static_handlers) | |
105 | ||
106 | # mirrors.ipfire.org | |
107 | self.add_handlers(r"mirrors\.ipfire\.org", [ | |
108 | (r"/", MirrorIndexHandler), | |
109 | (r"/mirror/([0-9]+)", MirrorItemHandler), | |
110 | ] + static_handlers) | |
111 | ||
d0d074e0 MT |
112 | # planet.ipfire.org |
113 | self.add_handlers(r"planet\.ipfire\.org", [ | |
940227cb | 114 | (r"/", PlanetMainHandler), |
d0d074e0 MT |
115 | (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler), |
116 | (r"/user/([a-z0-9]+)", PlanetUserHandler), | |
117 | ] + static_handlers) | |
118 | ||
940227cb | 119 | # stasy.ipfire.org |
91a446f0 | 120 | self.add_handlers(r"(fireinfo|stasy)\.ipfire\.org", [ |
940227cb | 121 | (r"/", StasyIndexHandler), |
91a446f0 MT |
122 | (r"/profile/([a-z0-9]{40})", StasyProfileDetailHandler), |
123 | (r"/vendor/(pci|usb)/([0-9a-f]{4})", StasyStatsVendorDetail), | |
124 | (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", StasyStatsModelDetail), | |
125 | ||
126 | # Stats handlers | |
127 | (r"/stats", StasyStatsHandler), | |
128 | (r"/stats/cpus", StasyStatsCPUHandler), | |
129 | (r"/stats/cpuflags", StasyStatsCPUFlagsHandler), | |
130 | (r"/stats/geo", StasyStatsGeoHandler), | |
131 | (r"/stats/memory", StasyStatsMemoryHandler), | |
132 | (r"/stats/oses", StasyStatsOSesHandler), | |
133 | (r"/stats/virtual", StasyStatsVirtualHandler), | |
ae0228e1 | 134 | ] + static_handlers) |
5cf160e0 | 135 | |
c37ec602 MT |
136 | # i-use.ipfire.org |
137 | self.add_handlers(r"i-use\.ipfire\.org", [ | |
138 | (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage), | |
139 | ]) | |
140 | ||
940227cb MT |
141 | # source.ipfire.org |
142 | # self.add_handlers(r"source\.ipfire\.org", [ | |
143 | # (r"/", MainHandler), | |
144 | # (r"/[A-Za-z]{2}/?", MainHandler), | |
145 | # (r"/[A-Za-z]{2}/index", SourceHandler), | |
146 | # (r"(/source.*|/toolchains/.*)", SourceDownloadHandler), | |
147 | # ] + static_handlers) | |
5cf160e0 MT |
148 | |
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 | ||
d0d074e0 MT |
157 | # admin.ipfire.org |
158 | self.add_handlers(r"admin\.ipfire\.org", [ | |
159 | (r"/", AdminIndexHandler), | |
940227cb MT |
160 | (r"/login", AdminLoginHandler), |
161 | (r"/logout", AdminLogoutHandler), | |
d0d074e0 MT |
162 | # Accounts |
163 | (r"/accounts", AdminAccountsHandler), | |
940227cb MT |
164 | #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler), |
165 | #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler), | |
d0d074e0 MT |
166 | # Planet |
167 | (r"/planet", AdminPlanetHandler), | |
168 | (r"/planet/compose", AdminPlanetComposeHandler), | |
169 | (r"/planet/edit/([0-9]+)", AdminPlanetEditHandler), | |
940227cb MT |
170 | # Mirrors |
171 | (r"/mirrors", AdminMirrorsHandler), | |
172 | (r"/mirrors/create", AdminMirrorsCreateHandler), | |
173 | (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler), | |
174 | (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler), | |
175 | (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler), | |
176 | (r"/mirrors/update", AdminMirrorsUpdateHandler), | |
d0d074e0 MT |
177 | # API |
178 | (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler) | |
179 | ] + static_handlers) | |
180 | ||
ae0228e1 | 181 | # ipfire.org |
3add293a | 182 | self.add_handlers(r".*", [ |
ae0228e1 | 183 | (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" }) |
5cf160e0 | 184 | ]) |
3add293a | 185 | |
feb02477 MT |
186 | logging.info("Successfully initialied application") |
187 | ||
188 | self.__running = True | |
189 | ||
3add293a | 190 | def __del__(self): |
feb02477 MT |
191 | logging.info("Shutting down application") |
192 | ||
feb02477 MT |
193 | @property |
194 | def ioloop(self): | |
195 | return tornado.ioloop.IOLoop.instance() | |
196 | ||
940227cb | 197 | def shutdown(self, *args): |
feb02477 MT |
198 | logging.debug("Caught shutdown signal") |
199 | self.ioloop.stop() | |
200 | ||
201 | self.__running = False | |
202 | ||
203 | def run(self, port=8001): | |
204 | logging.debug("Going to background") | |
940227cb 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 MT |
208 | |
209 | http_server = tornado.httpserver.HTTPServer(self, xheaders=True) | |
feb02477 | 210 | |
940227cb MT |
211 | # If we are not running in debug mode, we can actually run multiple |
212 | # frontends to get best performance out of our service. | |
213 | if not self.settings["debug"]: | |
214 | http_server.bind(port) | |
215 | http_server.start(num_processes=4) | |
216 | else: | |
217 | http_server.listen(port) | |
feb02477 | 218 | |
940227cb | 219 | self.ioloop.start() |
feb02477 MT |
220 | |
221 | def reload(self): | |
222 | logging.debug("Caught reload signal") |