]> git.ipfire.org Git - people/ms/webapp.git/commitdiff
Split into many seperate files.
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 23 Nov 2009 11:57:53 +0000 (12:57 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 23 Nov 2009 11:57:53 +0000 (12:57 +0100)
.gitignore [new file with mode: 0644]
application/__init__.py [new file with mode: 0644]
application/auth.py [new file with mode: 0644]
application/handlers.py [new file with mode: 0644]
webapp.py

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..539da74
--- /dev/null
@@ -0,0 +1 @@
+*.py[co]
diff --git a/application/__init__.py b/application/__init__.py
new file mode 100644 (file)
index 0000000..e089049
--- /dev/null
@@ -0,0 +1,48 @@
+#!/usr/bin/python
+
+import os
+import sys
+import tornado.web
+
+BASEDIR = os.path.join(os.path.dirname(__file__), "..")
+
+from auth import LoginHandler, LogoutHandler
+from handlers import MainHandler
+
+class Application(tornado.web.Application):
+       def __init__(self):
+               settings = dict(
+                       template_path = os.path.join(BASEDIR, "templates"),
+                       static_path = os.path.join(BASEDIR, "static"),
+                       xsrf_cookies = True,
+                       cookie_secret = "123456789",
+                       login_url = "/login",
+               )
+
+               handlers = [
+                       (r"/", MainHandler),
+                       (r"/login", LoginHandler),
+                       (r"/logout", LogoutHandler),
+               ] #self.get_handlers()
+
+               tornado.web.Application.__init__(self, handlers, **settings)
+
+#      # XXX This is not a nice solution but works for the moment
+#      def get_handlers(self):
+#              handlers_dir = os.path.join(BASEDIR, "pages")
+#              sys.path.append(handlers_dir)
+#              handlers = []
+#
+#              for handler in os.listdir(handlers_dir):
+#                      if not handler.endswith(".py"):
+#                              continue
+#
+#                      handler = handler[:-3]
+#                      handler = __import__(handler)
+#                      
+#                      if type(handler.handle) == type([]):
+#                              handlers.extend(handler.handle)
+#                      else:
+#                              handlers.append(handler.handle)
+#
+#              return handlers
diff --git a/application/auth.py b/application/auth.py
new file mode 100644 (file)
index 0000000..b8ea744
--- /dev/null
@@ -0,0 +1,17 @@
+#!/usr/bin/python
+
+from handlers import BaseHandler
+
+class LoginHandler(BaseHandler):
+       def get(self):
+               self.render("login.html", footer=self.footer())
+
+       def post(self):
+               self.set_secure_cookie("user", self.get_argument("user"))
+               self.redirect("/")
+
+
+class LogoutHandler(BaseHandler):
+       def get(self):
+               self.clear_cookie("user")
+               self.render("logout.html", footer=self.footer())
diff --git a/application/handlers.py b/application/handlers.py
new file mode 100644 (file)
index 0000000..0c6a73c
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/python
+
+import socket
+
+import tornado.web
+
+class BaseHandler(tornado.web.RequestHandler):
+       def get_current_user(self):
+               return self.get_secure_cookie("user")
+
+       def footer(self):
+               return "%s" % socket.gethostname()
+
+class MainHandler(BaseHandler):
+       def get(self):
+               if not self.current_user:
+                       self.redirect("/login")
+                       return
+
+               self.render("template.html", title="Testsite", slogan="Security now!",
+                       footer=self.footer(), user=self.current_user)
index ba7280412135d63698fe3f181ffd21200fa80787..6e83f4b999facf8ab56b5bb6e2beaf89fcd81d49 100644 (file)
--- a/webapp.py
+++ b/webapp.py
@@ -1,62 +1,15 @@
 #!/usr/bin/python
 
-import os
-import socket
-import time
+import application
 
 import tornado.httpserver
 import tornado.ioloop
-import tornado.web
+import tornado.options
 
-BASEDIR = os.path.dirname(__file__)
-
-settings = {
-       "static_path"   : os.path.join(BASEDIR, "static"),
-       "template_path" : os.path.join(BASEDIR, "templates"),
-       "login_url"     : "/login",
-       "cookie_secret" : "123456789",
-       "xsrf_cookies"  : True,
-}
-
-class BaseHandler(tornado.web.RequestHandler):
-       def get_current_user(self):
-               return self.get_secure_cookie("user")
-
-       def footer(self):
-               return "%s" % socket.gethostname()
-
-
-class MainHandler(BaseHandler):
-       def get(self):
-               if not self.current_user:
-                       self.redirect("/login")
-                       return
-
-               self.render("template.html", title="Testsite", slogan="Security now!",
-                       footer=self.footer(), user=self.current_user)
-
-
-class LoginHandler(BaseHandler):
-       def get(self):
-               self.render("login.html", footer=self.footer())
-
-       def post(self):
-               self.set_secure_cookie("user", self.get_argument("user"))
-               self.redirect("/")
-
-class LogoutHandler(BaseHandler):
-       def get(self):
-               self.clear_cookie("user")
-               self.render("logout.html", footer=self.footer())
-
-
-application = tornado.web.Application([
-       (r"/", MainHandler),
-       (r"/login", LoginHandler),
-       (r"/logout", LogoutHandler),
-], **settings)
+app = application.Application()
 
 if __name__ == "__main__":
-       http_server = tornado.httpserver.HTTPServer(application)
+       tornado.options.enable_pretty_logging()
+       http_server = tornado.httpserver.HTTPServer(app)
        http_server.listen(8080)
        tornado.ioloop.IOLoop.instance().start()