Browse Source

basic setting file

Davide Alberani 6 years ago
parent
commit
161b7c582b
7 changed files with 65 additions and 45 deletions
  1. 1 0
      .gitignore
  2. 0 0
      conf/.gitkeep
  3. 34 21
      diffido.py
  4. 1 0
      dist/base.html
  5. 28 23
      dist/index.html
  6. 0 0
      dist/schedule.html
  7. 1 1
      dist/static/js/axios.min.js

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
+conf/*.json
 .DS_Store
 node_modules/
 storage/

+ 0 - 0
conf/.gitkeep


+ 34 - 21
diffido.py

@@ -2,6 +2,7 @@
 # -*- coding: utf-8 -*-
 
 import os
+import json
 import logging
 
 from tornado.ioloop import IOLoop
@@ -19,6 +20,7 @@ from tornado import gen, escape
 CONF_DIR = ''
 JOBS_STORE = 'sqlite:///storage/jobs.db'
 API_VERSION = '1.0'
+SCHEDULES_FILE = 'conf/schedules.json'
 
 
 class DiffidoBaseException(Exception):
@@ -86,6 +88,23 @@ class BaseHandler(tornado.web.RequestHandler):
         self.write({'error': True, 'message': message})
 
 
+class SchedulesHandler(BaseHandler):
+    def read_schedules(self):
+        if not os.path.isfile(SCHEDULES_FILE):
+            return {}
+        with open(SCHEDULES_FILE, 'r') as fd:
+            return json.loads(fd.read())
+
+    def write_schedules(self, schedules):
+        with open(SCHEDULES_FILE, 'w') as fd:
+            fd.write(json.dumps(schedules, indent=2))
+
+    @gen.coroutine
+    def get(self, id_=None, *args, **kwargs):
+        schedules = self.read_schedules()
+        self.write(schedules)
+
+
 class TemplateHandler(BaseHandler):
     """Handler for the / path."""
     app_path = os.path.join(os.path.dirname(__file__), "dist")
@@ -96,13 +115,6 @@ class TemplateHandler(BaseHandler):
         if args and args[0]:
             page = args[0].strip('/')
         arguments = self.arguments
-        arguments['schedules'] = [{
-            'id': 1,
-            'title': 'wikipedia',
-            'url': 'https://it.wikipedia.org/wiki/Pagina_principale',
-            'scheduling': {'interval': 5, 'unit': 'minutes'},
-            'enabled': True
-        }]
         self.render(page, **arguments)
 
 
@@ -119,14 +131,14 @@ def serve():
     #scheduler.remove_job('run')
     #scheduler.add_job(run, 'interval', minutes=1)
 
-    define("port", default=3210, help="run on the given port", type=int)
-    define("address", default='', help="bind the server at the given address", type=str)
-    define("ssl_cert", default=os.path.join(os.path.dirname(__file__), 'ssl', 'diffido_cert.pem'),
-            help="specify the SSL certificate to use for secure connections")
-    define("ssl_key", default=os.path.join(os.path.dirname(__file__), 'ssl', 'diffido_key.pem'),
-            help="specify the SSL private key to use for secure connections")
-    define("debug", default=False, help="run in debug mode")
-    define("config", help="read configuration file",
+    define('port', default=3210, help='run on the given port', type=int)
+    define('address', default='', help='bind the server at the given address', type=str)
+    define('ssl_cert', default=os.path.join(os.path.dirname(__file__), 'ssl', 'diffido_cert.pem'),
+            help='specify the SSL certificate to use for secure connections')
+    define('ssl_key', default=os.path.join(os.path.dirname(__file__), 'ssl', 'diffido_key.pem'),
+            help='specify the SSL private key to use for secure connections')
+    define('debug', default=False, help='run in debug mode')
+    define('config', help='read configuration file',
             callback=lambda path: tornado.options.parse_config_file(path, final=False))
     tornado.options.parse_command_line()
 
@@ -139,15 +151,16 @@ def serve():
     if os.path.isfile(options.ssl_key) and os.path.isfile(options.ssl_cert):
         ssl_options = dict(certfile=options.ssl_cert, keyfile=options.ssl_key)
 
-    init_params = dict(listen_port=options.port, logger=logger, ssl_options=ssl_options)
+    init_params = dict(listen_port=options.port, logger=logger, ssl_options=ssl_options,
+                       scheduler=scheduler)
 
-    # _days_path = r"/days/?(?P<day>[\d_-]+)?"
+    _schedules_path = r'schedules/?(?P<id_>\d+)?'
     application = tornado.web.Application([
-            # (_days_path, DaysHandler, init_params),
-            # (r'/v%s%s' % (API_VERSION, _days_path), DaysHandler, init_params),
-            (r"/?(.*)", TemplateHandler, init_params),
+            ('/api/%s' % _schedules_path, SchedulesHandler, init_params),
+            (r'/api/v%s/%s' % (API_VERSION, _schedules_path), SchedulesHandler, init_params),
+            (r'/?(.*)', TemplateHandler, init_params),
         ],
-        static_path=os.path.join(os.path.dirname(__file__), "dist/static"),
+        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)

+ 1 - 0
dist/base.html

@@ -8,6 +8,7 @@
         <link rel="stylesheet" href="/static/css/themes/default.css">
         <script src="/static/js/vue.js"></script>
         <script src="/static/js/vue-material.min.js"></script>
+        <script src="/static/js/axios.min.js"></script>
     </head>
     <body>
         {% block body %}{% end %}

+ 28 - 23
dist/index.html

@@ -1,29 +1,18 @@
 {% extends "base.html" %}
 
 {% block body %}
-Index
-<a href="/page.html/?id=3">setting 3</a>; <a href="/diff.html?id=3">diff 3</a>
-
 <div id="app">
     <div class="md-layout">
-        <div class="md-layout-item">
-            <md-table>
-            <md-table-row>
-                <md-table-head md-numeric>#</md-table-head>
-                <md-table-head>title</md-table-head>
-                <md-table-head>url</md-table-head>
-                <md-table-head>schedule</md-table-head>
-            </md-table-row>
-
-            {% for schedule in schedules %}
-            <md-table-row>
-                <md-table-cell md-numeric>{{ schedule['id'] }}</md-table-cell>
-                <md-table-cell>{{ schedule['title'] }}</md-table-cell>
-                <md-table-cell>{{ schedule['url'] }}</md-table-cell>
-                <md-table-cell>meh</md-table-cell>
-            </md-table-row>
-            {% end %}
-
+        <div class="md-layout-item" md-card>
+            <md-table id="schedules-table" v-model="schedules">
+                <md-table-toolbar>
+                    <h1 class="md-title">Schedules <a href="schedule.html">add new</a></h1>
+                </md-table-toolbar>
+                <md-table-row slot="md-table-row" slot-scope="{item}">
+                    <md-table-cell md-label="#" md-sort-by="id" md-numeric>${ item.id }</md-table-cell>
+                    <md-table-cell md-label="title" md-sort-by="title">${ item.title }</md-table-cell>
+                    <md-table-cell md-label="url" md-sort-by="email">${ item.url }</md-table-cell>
+                </md-table-row>
             </md-table>
         </div>
     </div>
@@ -34,7 +23,23 @@ Index
 Vue.use(VueMaterial.default);
 
 var app = new Vue({
-    el: '#app'
+    el: '#app',
+    delimiters: ['${', '}'],
+    data: {
+        schedules: []
+    },
+    mounted: function() {
+        this.getSchedules();
+    },
+    methods: {
+        getSchedules: function() {
+            self = this;
+            var data = axios.get('/api/schedules').then(function(response) {
+                console.log(response);
+                self.schedules = response.data.schedules || [];
+            });
+        }
+    }
 });
 
 </script>
@@ -47,7 +52,7 @@ body {
 .md-table {
     width: 60%;
     min-height: 200px;
-	max-height: 600px;
+    max-height: 600px;
 }
 
 </style>

+ 0 - 0
dist/page.html → dist/schedule.html


File diff suppressed because it is too large
+ 1 - 1
dist/static/js/axios.min.js


Some files were not shown because too many files changed in this diff