about summary refs log tree commit diff
path: root/src/blog_node5_net.py
diff options
context:
space:
mode:
authoruser <user@node5.net>2024-03-10 22:57:13 +0100
committeruser <user@node5.net>2024-03-10 22:57:13 +0100
commit87e7c1b585474e961d3888f0c46930b2f7c3f651 (patch)
treecf2e482dae1a096d7ff6a4291a4b14e51a9c28d6 /src/blog_node5_net.py
parent1c70616bcbb909a38b96a45f2c1840f26e0fedd8 (diff)
reload articles from source on SIGHUB signal
Diffstat (limited to 'src/blog_node5_net.py')
-rw-r--r--src/blog_node5_net.py49
1 files changed, 33 insertions, 16 deletions
diff --git a/src/blog_node5_net.py b/src/blog_node5_net.py
index 006a751..e0b065d 100644
--- a/src/blog_node5_net.py
+++ b/src/blog_node5_net.py
@@ -1,6 +1,7 @@
 import logging
 import random
 import os
+import signal
 import typing
 import urllib
 
@@ -84,27 +85,43 @@ def view_article():
 
 def register_urls(folder: article_generator.Folder):
     # Use recursion to traverse folder tree structure
-    app.add_url_rule(folder.url, view_func=view_folder)
+    if strip_trailing_slash(folder.url) not in folders_by_url.keys():
+        # If statement is because it can be called multiple times, which would cause an error
+        app.add_url_rule(folder.url, view_func=view_folder)
     folders_by_url[strip_trailing_slash(folder.url) if folder.url != '/' else folder.url] = folder
     for article in folder.articles:
-        logger.debug(f"Registering url: {article.url}, static path: {article.folder_path}")
-        blueprint_args = {}
-        # Some articles are one file, and don't have a folder, hence argument would give an error
-        if article.folder_path:
-            blueprint_args['static_folder'] = os.path.abspath(article.folder_path)
-            blueprint_args['static_url_path'] = ''
-        blueprint = flask.Blueprint(article.name, __name__, url_prefix=article.url, **blueprint_args)
-        blueprint.add_url_rule('/', view_func=view_article)
-        app.register_blueprint(blueprint)
+        if article.url not in articles_by_url.keys():
+            # If statement is because it can be called multiple times, which would cause an error
+            logger.debug(f"Registering url: {article.url}, static path: {article.folder_path}")
+            blueprint_args = {}
+            # Some articles are one file, and don't have a folder, hence argument would give an error
+            if article.folder_path:
+                blueprint_args['static_folder'] = os.path.abspath(article.folder_path)
+                blueprint_args['static_url_path'] = ''
+            blueprint = flask.Blueprint(article.name, __name__, url_prefix=article.url, **blueprint_args)
+            blueprint.add_url_rule('/', view_func=view_article)
+            app.register_blueprint(blueprint)
         # app.add_url_rule(article.url, view_func=view_article)
         articles_by_url[article.url] = article
     for sub_folder in folder.sub_folders.values():
         register_urls(sub_folder)
 
 
-try:
-    articles = article_generator.discover_articles(os.path.join(site_root_folder_path, 'articles'))
-    register_urls(articles)
-except article_generator.ArticleHandlerException as exception:  # Known exceptions
-    logger.error(exception)
-    exit(1)  # Exit code 1: Code for generic errors
+def load_articles():
+    try:
+        articles = article_generator.discover_articles(os.path.join(site_root_folder_path, 'articles'))
+        register_urls(articles)
+    except article_generator.ArticleHandlerException as exception:  # Known exceptions
+        logger.error(exception)
+        exit(1)  # Exit code 1: Code for generic errors
+
+
+load_articles()
+
+
+def signal_handler(signum, frame):
+    logger.info("Signal Number:", signum, " Frame: ", frame)
+    load_articles()
+
+
+signal.signal(signal.SIGHUP, signal_handler)