diff --git a/learnapp.py b/learnapp.py index f8e56b6..ddc711d 100644 --- a/learnapp.py +++ b/learnapp.py @@ -287,6 +287,7 @@ def build_dependency_graph(config={}): # iterate over topics and build graph topics = config.get('topics', {}) + tcount = qcount = 0 # topic and question counters for ref,attr in topics.items(): g.add_node(ref) tnode = g.node[ref] # current node (topic) @@ -313,5 +314,8 @@ def build_dependency_graph(config={}): tnode['factory'][q['ref']] = QFactory(q) logger.info(f'{len(tnode["questions"]):6} {ref}') + qcount += len(tnode["questions"]) # count total questions + tcount += 1 + logger.info(f'Total loaded: {tcount} topics, {qcount} questions') return g diff --git a/serve.py b/serve.py index 78a6984..087bc1a 100755 --- a/serve.py +++ b/serve.py @@ -13,7 +13,7 @@ from concurrent.futures import ThreadPoolExecutor import tornado.ioloop import tornado.web import tornado.httpserver -from tornado import template #, gen +from tornado import template, iostream, gen from tornado.concurrent import run_on_executor from tornado.platform.asyncio import to_tornado_future @@ -101,7 +101,7 @@ class ChangePasswordHandler(BaseHandler): @tornado.web.authenticated def post(self): uid = self.current_user - pw = self.get_body_arguments('new_password')[0]; + pw = self.get_body_arguments('new_password')[0] if self.learn.change_password(uid, pw): notification = tornado.escape.to_unicode(self.render_string('notification.html', type='success', msg='A password foi alterada!')) @@ -152,12 +152,13 @@ class TopicHandler(BaseHandler): # Based on https://bhch.github.io/posts/2017/12/serving-large-files-with-tornado-safely-without-blocking/ # ---------------------------------------------------------------------------- class FileHandler(BaseHandler): + chunk_size = 4 * 1024 * 1024 # serve up to 4 MiB multiple times + @tornado.web.authenticated async def get(self, filename): uid = self.current_user public_dir = self.learn.get_current_public_dir(uid) filepath = path.expanduser(path.join(public_dir, filename)) - chunk_size = 1024 * 1024 # serve up to 1MiB multiple times try: f = open(filepath, 'rb') except FileNotFoundError: @@ -166,7 +167,7 @@ class FileHandler(BaseHandler): logging.error(f'No permission: {filepath}') else: with f: - chunk = f.read(chunk_size) + chunk = f.read(self.chunk_size) while chunk: try: self.write(chunk) # write the cunk to response @@ -178,7 +179,7 @@ class FileHandler(BaseHandler): del chunk await gen.sleep(0.000000001) # 1 nanosecond (hack) # in tornnado 5.0 use `await asyncio.sleep(0)` instead - chunk = f.read(chunk_size) + chunk = f.read(self.chunk_size) # ---------------------------------------------------------------------------- @@ -320,12 +321,17 @@ def main(): raise e # --- create webserver - http_server = tornado.httpserver.HTTPServer(webapp, - ssl_options={ - "certfile": "certs/cert.pem", - "keyfile": "certs/privkey.pem" - }) - http_server.listen(8443) + try: + http_server = tornado.httpserver.HTTPServer(webapp, + ssl_options={ + "certfile": "certs/cert.pem", + "keyfile": "certs/privkey.pem" + }) + except ValueError: + logging.critical('Certificates cert.pem, privkey.pem not found') + sys.exit(1) + else: + http_server.listen(8443) # --- run webserver logging.info('Webserver running...') diff --git a/templates/maintopics.html b/templates/maintopics.html index 023abe9..3bd93d8 100644 --- a/templates/maintopics.html +++ b/templates/maintopics.html @@ -98,9 +98,11 @@ {% end %} {% end %} - + + +