Browse Source

add basic templates

Davide Alberani 6 years ago
parent
commit
84cdfac30d
6 changed files with 31 additions and 7 deletions
  1. 0 1
      .gitignore
  2. 7 6
      diffido.py
  3. 8 0
      dist/base.html
  4. 5 0
      dist/diff.html
  5. 6 0
      dist/index.html
  6. 5 0
      dist/page.html

+ 0 - 1
.gitignore

@@ -2,7 +2,6 @@
 node_modules/
 storage/
 __pycache__
-dist/
 npm-debug*.log
 ssl/*.pem
 *.py[cod]

+ 7 - 6
diffido.py

@@ -86,15 +86,16 @@ class BaseHandler(tornado.web.RequestHandler):
         self.write({'error': True, 'message': message})
 
 
-class RootHandler(BaseHandler):
+class TemplateHandler(BaseHandler):
     """Handler for the / path."""
     app_path = os.path.join(os.path.dirname(__file__), "dist")
 
     @gen.coroutine
     def get(self, *args, **kwargs):
-        # serve the ./dist/index.html file
-        with open(self.app_path + "/index.html", 'r') as fd:
-            self.write(fd.read())
+        page = 'index.html'
+        if args and args[0]:
+            page = args[0].strip('/')
+        self.render(page, **self.arguments)
 
 
 def serve():
@@ -128,10 +129,10 @@ def serve():
     application = tornado.web.Application([
             # (_days_path, DaysHandler, init_params),
             # (r'/v%s%s' % (API_VERSION, _days_path), DaysHandler, init_params),
-            (r"/(?:index.html)?", RootHandler, init_params),
-            (r'/?(.*)', tornado.web.StaticFileHandler, {"path": "dist"})
+            (r"/?(.*)", TemplateHandler, init_params),
         ],
         static_path=os.path.join(os.path.dirname(__file__), "dist/static"),
+        template_path=os.path.join(os.path.dirname(__file__), 'dist/'),
         debug=options.debug)
     http_server = tornado.httpserver.HTTPServer(application, ssl_options=ssl_options or None)
     logger.info('Start serving on %s://%s:%d', 'https' if ssl_options else 'http',

+ 8 - 0
dist/base.html

@@ -0,0 +1,8 @@
+<html>
+    <head>
+        <title>{% block title %}diffido{% end %}</title>
+    </head>
+    <body>
+        {% block body %}{% end %}
+    </body>
+</html>

+ 5 - 0
dist/diff.html

@@ -0,0 +1,5 @@
+{% extends "base.html" %}
+
+{% block body %}
+diff page {{ id }}
+{% end %}

+ 6 - 0
dist/index.html

@@ -0,0 +1,6 @@
+{% extends "base.html" %}
+
+{% block body %}
+Index
+<a href="/page.html/?id=3">setting 3</a>; <a href="/diff.html?id=3">diff 3</a>
+{% end %}

+ 5 - 0
dist/page.html

@@ -0,0 +1,5 @@
+{% extends "base.html" %}
+
+{% block body %}
+setting page {{ id }}
+{% end %}