]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blame - www/webapp/__init__.py
i-use: Return an empty image if an error was raised.
[people/shoehn/ipfire.org.git] / www / webapp / __init__.py
CommitLineData
81675874 1#/usr/bin/python
2
feb02477 3import logging
81675874 4import os.path
8ccd2ff0 5import simplejson
feb02477 6import tornado.httpserver
81675874 7import tornado.locale
8import tornado.options
9import tornado.web
10
940227cb 11import backend
feb02477 12
8ccd2ff0
MT
13from handlers import *
14from ui_modules import *
81675874 15
16BASEDIR = os.path.join(os.path.dirname(__file__), "..")
17
18tornado.locale.load_translations(os.path.join(BASEDIR, "translations"))
81675874 19
20class Application(tornado.web.Application):
21 def __init__(self):
81675874 22 settings = dict(
23 cookie_secret = "aXBmaXJlY29va2llc2VjcmV0Cg==",
c4b8d029 24 debug = False,
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 86 ] + static_handlers + [
edd297c4 87 (r"/(iso|torrent)/(.*)", DownloadCompatHandler),
54af860e
MT
88 (r"/(.*)", DownloadFileHandler),
89 ])
940227cb
MT
90
91 # mirrors.ipfire.org
92 self.add_handlers(r"mirrors\.ipfire\.org", [
93 (r"/", MirrorIndexHandler),
94 (r"/mirror/([0-9]+)", MirrorItemHandler),
95 ] + static_handlers)
96
d0d074e0
MT
97 # planet.ipfire.org
98 self.add_handlers(r"planet\.ipfire\.org", [
940227cb 99 (r"/", PlanetMainHandler),
d0d074e0 100 (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler),
27066195 101 (r"/user/([a-z0-9_-]+)", PlanetUserHandler),
bcc3ed4d
MT
102
103 # RSS
104 (r"/rss", RSSPlanetAllHandler),
105 (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler),
d0d074e0
MT
106 ] + static_handlers)
107
940227cb 108 # stasy.ipfire.org
91a446f0 109 self.add_handlers(r"(fireinfo|stasy)\.ipfire\.org", [
940227cb 110 (r"/", StasyIndexHandler),
91a446f0
MT
111 (r"/profile/([a-z0-9]{40})", StasyProfileDetailHandler),
112 (r"/vendor/(pci|usb)/([0-9a-f]{4})", StasyStatsVendorDetail),
113 (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", StasyStatsModelDetail),
114
115 # Stats handlers
116 (r"/stats", StasyStatsHandler),
117 (r"/stats/cpus", StasyStatsCPUHandler),
118 (r"/stats/cpuflags", StasyStatsCPUFlagsHandler),
119 (r"/stats/geo", StasyStatsGeoHandler),
120 (r"/stats/memory", StasyStatsMemoryHandler),
54af860e 121 (r"/stats/network", StasyStatsNetworkHandler),
91a446f0
MT
122 (r"/stats/oses", StasyStatsOSesHandler),
123 (r"/stats/virtual", StasyStatsVirtualHandler),
ae0228e1 124 ] + static_handlers)
5cf160e0 125
c37ec602
MT
126 # i-use.ipfire.org
127 self.add_handlers(r"i-use\.ipfire\.org", [
128 (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage),
129 ])
130
5cf160e0 131 # tracker.ipfire.org
940227cb
MT
132 self.add_handlers(r"(torrent|tracker)\.ipfire\.org", [
133 (r"/", TrackerIndexHandler),
43d991f6 134 (r"/a.*", TrackerAnnounceHandler),
e2afbd6a 135 (r"/scrape", TrackerScrapeHandler),
940227cb 136 (r"/torrent/([0-9a-f]+)", TrackerDetailHandler),
ae0228e1
MT
137 ] + static_handlers)
138
d0d074e0
MT
139 # admin.ipfire.org
140 self.add_handlers(r"admin\.ipfire\.org", [
141 (r"/", AdminIndexHandler),
940227cb
MT
142 (r"/login", AdminLoginHandler),
143 (r"/logout", AdminLogoutHandler),
d0d074e0
MT
144 # Accounts
145 (r"/accounts", AdminAccountsHandler),
940227cb
MT
146 #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler),
147 #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler),
d0d074e0
MT
148 # Planet
149 (r"/planet", AdminPlanetHandler),
150 (r"/planet/compose", AdminPlanetComposeHandler),
151 (r"/planet/edit/([0-9]+)", AdminPlanetEditHandler),
940227cb
MT
152 # Mirrors
153 (r"/mirrors", AdminMirrorsHandler),
154 (r"/mirrors/create", AdminMirrorsCreateHandler),
155 (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler),
156 (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler),
157 (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler),
158 (r"/mirrors/update", AdminMirrorsUpdateHandler),
71e1ece7
MT
159 # Fireinfo
160 (r"/fireinfo/stats", AdminFireinfoStatsHandler),
d0d074e0
MT
161 # API
162 (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler)
163 ] + static_handlers)
164
ae0228e1 165 # ipfire.org
3add293a 166 self.add_handlers(r".*", [
ae0228e1 167 (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" })
5cf160e0 168 ])
3add293a 169
feb02477
MT
170 logging.info("Successfully initialied application")
171
172 self.__running = True
173
3add293a 174 def __del__(self):
feb02477
MT
175 logging.info("Shutting down application")
176
feb02477
MT
177 @property
178 def ioloop(self):
179 return tornado.ioloop.IOLoop.instance()
180
940227cb 181 def shutdown(self, *args):
feb02477
MT
182 logging.debug("Caught shutdown signal")
183 self.ioloop.stop()
184
185 self.__running = False
186
187 def run(self, port=8001):
188 logging.debug("Going to background")
189
190 http_server = tornado.httpserver.HTTPServer(self, xheaders=True)
feb02477 191
940227cb
MT
192 # If we are not running in debug mode, we can actually run multiple
193 # frontends to get best performance out of our service.
194 if not self.settings["debug"]:
195 http_server.bind(port)
196 http_server.start(num_processes=4)
197 else:
198 http_server.listen(port)
de683d7c
MT
199
200 # All requests should be done after 30 seconds or they will be killed.
201 self.ioloop.set_blocking_log_threshold(30)
feb02477 202
940227cb 203 self.ioloop.start()
feb02477
MT
204
205 def reload(self):
206 logging.debug("Caught reload signal")