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