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