From cb43ddeb7ea480a405ff763b307003445db4baf8 Mon Sep 17 00:00:00 2001 From: David Huss <dh@atoav.com> Date: Tue, 2 Feb 2021 15:14:57 +0100 Subject: [PATCH] Refactor config and main --- stechuhr_server/config.py | 53 +++++++++++++++++++++++++++++++ stechuhr_server/server.py | 67 ++++----------------------------------- 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/stechuhr_server/config.py b/stechuhr_server/config.py index c7c7091..51d9b5f 100644 --- a/stechuhr_server/config.py +++ b/stechuhr_server/config.py @@ -4,6 +4,59 @@ import os, getpass import toml from pathlib import Path +from logging.config import dictConfig + + +APPLICATION_NAME = "stechuhr-server" + +DEFAULT_CONFIG = """ +[application] +# Warning: setting this to true can will display all visitor movments when the +# server adress is visited. Only use for debugging! +expose_visitor_data = true +expose_current_visitor_number = true + +[database] +# sqlite db path, use ":memory:" for RAM db +path = "visitors.db" + +# A list of possible regex patterns for the id (logical OR!) +id_patterns = [ + "^806[A-Z0-9]{9}04$", + "^FB6A1E60$", + "^FB6D6950$", + "^FB6A9DE0$", + "^FB67D500$", +] + +# minimum and maximum lengths for the received strings +min_entrance_length = 1 +max_entrance_length = 128 +min_location_length = 1 +max_location_length = 128 + +[retention] +# how old is data allowed to be (in days) +duration = 365 +""" + +# Config for the logger, there should be no need to make +# manual changes here +dictConfig({ + 'version': 1, + 'formatters': {'default': { + 'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s', + }}, + 'handlers': {'wsgi': { + 'class': 'logging.StreamHandler', + 'stream': 'ext://flask.logging.wsgi_errors_stream', + 'formatter': 'default' + }}, + 'root': { + 'level': 'INFO', + 'handlers': ['wsgi'] + } +}) def this_or_else(this: str, other: str) -> str: diff --git a/stechuhr_server/server.py b/stechuhr_server/server.py index 045ee57..5195250 100644 --- a/stechuhr_server/server.py +++ b/stechuhr_server/server.py @@ -4,70 +4,15 @@ import re import datetime as dt import sqlite3 from flask import Flask, request, render_template -from logging.config import dictConfig - -from .config import initialize_config - - - - -APPLICATION_NAME = "stechuhr-server" - -DEFAULT_CONFIG = """ -[application] -# Warning: setting this to true can will display all visitor movments when the -# server adress is visited. Only use for debugging! -expose_visitor_data = true -expose_current_visitor_number = true - -[database] -# sqlite db path, use ":memory:" for RAM db -path = "visitors.db" - -# A list of possible regex patterns for the id (logical OR!) -id_patterns = [ - "^806[A-Z0-9]{9}04$", - "^FB6A1E60$", - "^FB6D6950$", - "^FB6A9DE0$", - "^FB67D500$", -] - -# minimum and maximum lengths for the received strings -min_entrance_length = 1 -max_entrance_length = 128 -min_location_length = 1 -max_location_length = 128 - -[retention] -# how old is data allowed to be (in days) -duration = 365 -""" - - -# Config for the logger, there should be no need to make -# manual changes here -dictConfig({ - 'version': 1, - 'formatters': {'default': { - 'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s', - }}, - 'handlers': {'wsgi': { - 'class': 'logging.StreamHandler', - 'stream': 'ext://flask.logging.wsgi_errors_stream', - 'formatter': 'default' - }}, - 'root': { - 'level': 'INFO', - 'handlers': ['wsgi'] - } -}) + +from .config import initialize_config, APPLICATION_NAME, DEFAULT_CONFIG + # Initialization app = Flask(APPLICATION_NAME) # Initialize the configuration (create a default one if needed) -config = initialize_config(APPLICATION_NAME, DEFAULT_CONFIG, app) +config = initialize_config(APPLICATION_NAME, DEFAULT_CONFIG, app.logger) # Compile the patterns for the ids once at startup config["database"]["id_patterns"] = [re.compile(p) for p in config["database"]["id_patterns"]] @@ -89,12 +34,14 @@ try: app.logger.info('Constructing table for visitors') cursor.execute(sqlite_create_table_query) except sqlite3.OperationalError as e: - app.logger.warning(e) + # Ignore error (signifying table already exists) pass + conn.close() app.logger.info('Ready to take requests') + def register_movement(data: dict, conn, cursor): """ Construct and store a new movment in the database -- GitLab