]> git.ipfire.org Git - ipfire.org.git/blame_incremental - www/webapp/__init__.py
Change mirror priority.
[ipfire.org.git] / www / webapp / __init__.py
... / ...
CommitLineData
1#/usr/bin/python
2
3import logging
4import os.path
5import simplejson
6import tornado.httpserver
7import tornado.locale
8import tornado.web
9
10from tornado.options import options
11
12import backend
13
14from handlers import *
15from ui_modules import *
16
17BASEDIR = os.path.join(os.path.dirname(__file__), "..")
18
19tornado.locale.load_translations(os.path.join(BASEDIR, "translations"))
20
21class Application(tornado.web.Application):
22 def __init__(self):
23 settings = dict(
24 cookie_secret = "aXBmaXJlY29va2llc2VjcmV0Cg==",
25 debug = options.debug,
26 gzip = True,
27 login_url = "/login",
28 template_path = os.path.join(BASEDIR, "templates"),
29 ui_modules = {
30 "Menu" : MenuModule,
31 "MirrorItem" : MirrorItemModule,
32 "MirrorsTable" : MirrorsTableModule,
33 "NewsItem" : NewsItemModule,
34 "NewsLine" : NewsLineModule,
35 "PlanetEntry" : PlanetEntryModule,
36 "ReleaseItem" : ReleaseItemModule,
37 "SidebarBanner" : SidebarBannerModule,
38 "SidebarRelease" : SidebarReleaseModule,
39 "StasyTable" : StasyTableModule,
40 "StasyDeviceTable" : StasyDeviceTableModule,
41 "StasyGeoTable" : StasyGeoTableModule,
42 "TrackerPeerList": TrackerPeerListModule,
43 },
44 xsrf_cookies = True,
45 )
46
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
56 self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [
57 # Entry site that lead the user to index
58 (r"/", IndexHandler),
59 (r"/index\.?(s?html?)?", RootHandler),
60
61 # Handle news items
62 (r"/news", NewsIndexHandler),
63 (r"/news/(.*)", NewsItemHandler),
64 (r"/author/(.*)", NewsAuthorHandler),
65
66 # Download sites
67 (r"/downloads?", DownloadHandler),
68
69 # Handle old pages that have moved elsewhere
70 (r"/screenshots", tornado.web.RedirectHandler, { "url" : "/about" }),
71
72 # RSS feed
73 (r"/news.rss", RSSNewsHandler),
74
75 # Redirection for bookmarks, etc.
76 (r"/(de|en)/(.*)", LangCompatHandler)
77
78 ] + static_handlers + [
79 # Always the last rule
80 (r"/(.*)", StaticHandler),
81 ])
82
83 # downloads.ipfire.org
84 self.add_handlers(r"downloads?\.ipfire\.org", [
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/" }),
92 ] + static_handlers + [
93 (r"/(iso|torrent)/(.*)", DownloadCompatHandler),
94 (r"/(.*)", DownloadFileHandler),
95 ])
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
103 # planet.ipfire.org
104 self.add_handlers(r"planet\.ipfire\.org", [
105 (r"/", PlanetMainHandler),
106 (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler),
107 (r"/user/([a-z0-9_-]+)", PlanetUserHandler),
108
109 # RSS
110 (r"/rss", RSSPlanetAllHandler),
111 (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler),
112 ] + static_handlers)
113
114 # stasy.ipfire.org
115 self.add_handlers(r"(fireinfo|stasy)\.ipfire\.org", [
116 (r"/", StasyIndexHandler),
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),
127 (r"/stats/network", StasyStatsNetworkHandler),
128 (r"/stats/oses", StasyStatsOSesHandler),
129 (r"/stats/virtual", StasyStatsVirtualHandler),
130 ] + static_handlers)
131
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
137 # tracker.ipfire.org
138 self.add_handlers(r"(torrent|tracker)\.ipfire\.org", [
139 (r"/", TrackerIndexHandler),
140 (r"/a.*", TrackerAnnounceHandler),
141 (r"/scrape", TrackerScrapeHandler),
142 (r"/torrent/([0-9a-f]+)", TrackerDetailHandler),
143 ] + static_handlers)
144
145 # admin.ipfire.org
146 self.add_handlers(r"admin\.ipfire\.org", [
147 (r"/", AdminIndexHandler),
148 (r"/login", AdminLoginHandler),
149 (r"/logout", AdminLogoutHandler),
150 # Accounts
151 (r"/accounts", AdminAccountsHandler),
152 #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler),
153 #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler),
154 # Planet
155 (r"/planet", AdminPlanetHandler),
156 (r"/planet/compose", AdminPlanetComposeHandler),
157 (r"/planet/edit/([0-9]+)", AdminPlanetEditHandler),
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),
165 # Fireinfo
166 (r"/fireinfo/stats", AdminFireinfoStatsHandler),
167 # API
168 (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler)
169 ] + static_handlers)
170
171 # ipfire.org
172 self.add_handlers(r".*", [
173 (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" })
174 ])
175
176 logging.info("Successfully initialied application")
177
178 self.__running = True
179
180 def __del__(self):
181 logging.info("Shutting down application")
182
183 @property
184 def ioloop(self):
185 return tornado.ioloop.IOLoop.instance()
186
187 def shutdown(self, *args):
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)
197
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)
205
206 # All requests should be done after 30 seconds or they will be killed.
207 self.ioloop.set_blocking_log_threshold(30)
208
209 self.ioloop.start()
210
211 def reload(self):
212 logging.debug("Caught reload signal")