]> git.ipfire.org Git - ipfire.org.git/blame - www/webapp/__init__.py
fireinfo: Fix geoip information.
[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
8d48f4ef
MT
66 # Handle old pages that have moved elsewhere
67 (r"/screenshots", tornado.web.RedirectHandler, { "url" : "/about" }),
68
de683d7c 69 # RSS feed
bcc3ed4d 70 (r"/news.rss", RSSNewsHandler),
de683d7c
MT
71
72 # Redirection for bookmarks, etc.
940227cb
MT
73 (r"/(de|en)/(.*)", LangCompatHandler)
74
75 ] + static_handlers + [
81675874 76 # Always the last rule
940227cb
MT
77 (r"/(.*)", StaticHandler),
78 ])
79
940227cb 80 # downloads.ipfire.org
54af860e 81 self.add_handlers(r"downloads?\.ipfire\.org", [
940227cb
MT
82 (r"/", DownloadsIndexHandler),
83 (r"/latest", DownloadsLatestHandler),
84 (r"/release/([0-9]+)", DownloadsReleaseHandler),
85 (r"/older", DownloadsOlderHandler),
86 (r"/development", DownloadsDevelopmentHandler),
87 (r"/mirrors", tornado.web.RedirectHandler, { "url" : "http://mirrors.ipfire.org/" }),
88 (r"/source", tornado.web.RedirectHandler, { "url" : "http://source.ipfire.org/" }),
54af860e 89 ] + static_handlers + [
edd297c4 90 (r"/(iso|torrent)/(.*)", DownloadCompatHandler),
54af860e
MT
91 (r"/(.*)", DownloadFileHandler),
92 ])
940227cb
MT
93
94 # mirrors.ipfire.org
95 self.add_handlers(r"mirrors\.ipfire\.org", [
96 (r"/", MirrorIndexHandler),
97 (r"/mirror/([0-9]+)", MirrorItemHandler),
98 ] + static_handlers)
99
d0d074e0
MT
100 # planet.ipfire.org
101 self.add_handlers(r"planet\.ipfire\.org", [
940227cb 102 (r"/", PlanetMainHandler),
d0d074e0 103 (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler),
27066195 104 (r"/user/([a-z0-9_-]+)", PlanetUserHandler),
bcc3ed4d
MT
105
106 # RSS
107 (r"/rss", RSSPlanetAllHandler),
108 (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler),
d0d074e0
MT
109 ] + static_handlers)
110
940227cb 111 # stasy.ipfire.org
91a446f0 112 self.add_handlers(r"(fireinfo|stasy)\.ipfire\.org", [
940227cb 113 (r"/", StasyIndexHandler),
91a446f0
MT
114 (r"/profile/([a-z0-9]{40})", StasyProfileDetailHandler),
115 (r"/vendor/(pci|usb)/([0-9a-f]{4})", StasyStatsVendorDetail),
116 (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", StasyStatsModelDetail),
117
118 # Stats handlers
119 (r"/stats", StasyStatsHandler),
120 (r"/stats/cpus", StasyStatsCPUHandler),
121 (r"/stats/cpuflags", StasyStatsCPUFlagsHandler),
122 (r"/stats/geo", StasyStatsGeoHandler),
123 (r"/stats/memory", StasyStatsMemoryHandler),
54af860e 124 (r"/stats/network", StasyStatsNetworkHandler),
91a446f0
MT
125 (r"/stats/oses", StasyStatsOSesHandler),
126 (r"/stats/virtual", StasyStatsVirtualHandler),
ae0228e1 127 ] + static_handlers)
5cf160e0 128
c37ec602
MT
129 # i-use.ipfire.org
130 self.add_handlers(r"i-use\.ipfire\.org", [
131 (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage),
132 ])
133
5cf160e0 134 # tracker.ipfire.org
940227cb
MT
135 self.add_handlers(r"(torrent|tracker)\.ipfire\.org", [
136 (r"/", TrackerIndexHandler),
43d991f6 137 (r"/a.*", TrackerAnnounceHandler),
e2afbd6a 138 (r"/scrape", TrackerScrapeHandler),
940227cb 139 (r"/torrent/([0-9a-f]+)", TrackerDetailHandler),
ae0228e1
MT
140 ] + static_handlers)
141
d0d074e0
MT
142 # admin.ipfire.org
143 self.add_handlers(r"admin\.ipfire\.org", [
144 (r"/", AdminIndexHandler),
940227cb
MT
145 (r"/login", AdminLoginHandler),
146 (r"/logout", AdminLogoutHandler),
d0d074e0
MT
147 # Accounts
148 (r"/accounts", AdminAccountsHandler),
940227cb
MT
149 #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler),
150 #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler),
d0d074e0
MT
151 # Planet
152 (r"/planet", AdminPlanetHandler),
153 (r"/planet/compose", AdminPlanetComposeHandler),
154 (r"/planet/edit/([0-9]+)", AdminPlanetEditHandler),
940227cb
MT
155 # Mirrors
156 (r"/mirrors", AdminMirrorsHandler),
157 (r"/mirrors/create", AdminMirrorsCreateHandler),
158 (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler),
159 (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler),
160 (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler),
161 (r"/mirrors/update", AdminMirrorsUpdateHandler),
71e1ece7
MT
162 # Fireinfo
163 (r"/fireinfo/stats", AdminFireinfoStatsHandler),
d0d074e0
MT
164 # API
165 (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler)
166 ] + static_handlers)
167
ae0228e1 168 # ipfire.org
3add293a 169 self.add_handlers(r".*", [
ae0228e1 170 (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" })
5cf160e0 171 ])
3add293a 172
feb02477
MT
173 logging.info("Successfully initialied application")
174
175 self.__running = True
176
3add293a 177 def __del__(self):
feb02477
MT
178 logging.info("Shutting down application")
179
feb02477
MT
180 @property
181 def ioloop(self):
182 return tornado.ioloop.IOLoop.instance()
183
940227cb 184 def shutdown(self, *args):
feb02477
MT
185 logging.debug("Caught shutdown signal")
186 self.ioloop.stop()
187
188 self.__running = False
189
190 def run(self, port=8001):
191 logging.debug("Going to background")
192
193 http_server = tornado.httpserver.HTTPServer(self, xheaders=True)
feb02477 194
940227cb
MT
195 # If we are not running in debug mode, we can actually run multiple
196 # frontends to get best performance out of our service.
197 if not self.settings["debug"]:
198 http_server.bind(port)
199 http_server.start(num_processes=4)
200 else:
201 http_server.listen(port)
de683d7c
MT
202
203 # All requests should be done after 30 seconds or they will be killed.
204 self.ioloop.set_blocking_log_threshold(30)
feb02477 205
940227cb 206 self.ioloop.start()
feb02477
MT
207
208 def reload(self):
209 logging.debug("Caught reload signal")