]>
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 | |
940227cb | 88 | # downloads.ipfire.org |
54af860e | 89 | self.add_handlers(r"downloads?\.ipfire\.org", [ |
940227cb MT |
90 | (r"/", DownloadsIndexHandler), |
91 | (r"/latest", DownloadsLatestHandler), | |
92 | (r"/release/([0-9]+)", DownloadsReleaseHandler), | |
93 | (r"/older", DownloadsOlderHandler), | |
94 | (r"/development", DownloadsDevelopmentHandler), | |
95 | (r"/mirrors", tornado.web.RedirectHandler, { "url" : "http://mirrors.ipfire.org/" }), | |
96 | (r"/source", tornado.web.RedirectHandler, { "url" : "http://source.ipfire.org/" }), | |
54af860e MT |
97 | ] + static_handlers + [ |
98 | (r"/(.*)", DownloadFileHandler), | |
99 | ]) | |
940227cb MT |
100 | |
101 | # mirrors.ipfire.org | |
102 | self.add_handlers(r"mirrors\.ipfire\.org", [ | |
103 | (r"/", MirrorIndexHandler), | |
104 | (r"/mirror/([0-9]+)", MirrorItemHandler), | |
105 | ] + static_handlers) | |
106 | ||
d0d074e0 MT |
107 | # planet.ipfire.org |
108 | self.add_handlers(r"planet\.ipfire\.org", [ | |
940227cb | 109 | (r"/", PlanetMainHandler), |
d0d074e0 | 110 | (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler), |
27066195 | 111 | (r"/user/([a-z0-9_-]+)", PlanetUserHandler), |
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 | ||
940227cb MT |
137 | # source.ipfire.org |
138 | # self.add_handlers(r"source\.ipfire\.org", [ | |
139 | # (r"/", MainHandler), | |
140 | # (r"/[A-Za-z]{2}/?", MainHandler), | |
141 | # (r"/[A-Za-z]{2}/index", SourceHandler), | |
142 | # (r"(/source.*|/toolchains/.*)", SourceDownloadHandler), | |
143 | # ] + static_handlers) | |
5cf160e0 MT |
144 | |
145 | # tracker.ipfire.org | |
940227cb MT |
146 | self.add_handlers(r"(torrent|tracker)\.ipfire\.org", [ |
147 | (r"/", TrackerIndexHandler), | |
43d991f6 | 148 | (r"/a.*", TrackerAnnounceHandler), |
e2afbd6a | 149 | (r"/scrape", TrackerScrapeHandler), |
940227cb | 150 | (r"/torrent/([0-9a-f]+)", TrackerDetailHandler), |
ae0228e1 MT |
151 | ] + static_handlers) |
152 | ||
d0d074e0 MT |
153 | # admin.ipfire.org |
154 | self.add_handlers(r"admin\.ipfire\.org", [ | |
155 | (r"/", AdminIndexHandler), | |
940227cb MT |
156 | (r"/login", AdminLoginHandler), |
157 | (r"/logout", AdminLogoutHandler), | |
d0d074e0 MT |
158 | # Accounts |
159 | (r"/accounts", AdminAccountsHandler), | |
940227cb MT |
160 | #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler), |
161 | #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler), | |
d0d074e0 MT |
162 | # Planet |
163 | (r"/planet", AdminPlanetHandler), | |
164 | (r"/planet/compose", AdminPlanetComposeHandler), | |
165 | (r"/planet/edit/([0-9]+)", AdminPlanetEditHandler), | |
940227cb MT |
166 | # Mirrors |
167 | (r"/mirrors", AdminMirrorsHandler), | |
168 | (r"/mirrors/create", AdminMirrorsCreateHandler), | |
169 | (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler), | |
170 | (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler), | |
171 | (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler), | |
172 | (r"/mirrors/update", AdminMirrorsUpdateHandler), | |
d0d074e0 MT |
173 | # API |
174 | (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler) | |
175 | ] + static_handlers) | |
176 | ||
ae0228e1 | 177 | # ipfire.org |
3add293a | 178 | self.add_handlers(r".*", [ |
ae0228e1 | 179 | (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" }) |
5cf160e0 | 180 | ]) |
3add293a | 181 | |
feb02477 MT |
182 | logging.info("Successfully initialied application") |
183 | ||
184 | self.__running = True | |
185 | ||
3add293a | 186 | def __del__(self): |
feb02477 MT |
187 | logging.info("Shutting down application") |
188 | ||
feb02477 MT |
189 | @property |
190 | def ioloop(self): | |
191 | return tornado.ioloop.IOLoop.instance() | |
192 | ||
940227cb | 193 | def shutdown(self, *args): |
feb02477 MT |
194 | logging.debug("Caught shutdown signal") |
195 | self.ioloop.stop() | |
196 | ||
197 | self.__running = False | |
198 | ||
199 | def run(self, port=8001): | |
200 | logging.debug("Going to background") | |
940227cb MT |
201 | |
202 | # All requests should be done after 30 seconds or they will be killed. | |
203 | self.ioloop.set_blocking_log_threshold(30) | |
feb02477 MT |
204 | |
205 | http_server = tornado.httpserver.HTTPServer(self, xheaders=True) | |
feb02477 | 206 | |
940227cb MT |
207 | # If we are not running in debug mode, we can actually run multiple |
208 | # frontends to get best performance out of our service. | |
209 | if not self.settings["debug"]: | |
210 | http_server.bind(port) | |
211 | http_server.start(num_processes=4) | |
212 | else: | |
213 | http_server.listen(port) | |
feb02477 | 214 | |
940227cb | 215 | self.ioloop.start() |
feb02477 MT |
216 | |
217 | def reload(self): | |
218 | logging.debug("Caught reload signal") |