Skip to content
Snippets Groups Projects
Commit e5263301 authored by David Huss's avatar David Huss :speech_balloon:
Browse files

Add pattern route

parent efedff00
Branches
No related tags found
No related merge requests found
#! /bin/bash
python3 -m venv env
source env/bin/activate
pip3 install -r requirements.txt
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#-*- coding: utf-8 -*- #-*- coding: utf-8 -*-
import os, getpass, sys import os, getpass, sys
import logging
import toml import toml
from pathlib import Path from pathlib import Path
from logging.config import dictConfig from logging.config import dictConfig
...@@ -19,6 +20,7 @@ DEFAULT_CONFIG = """ ...@@ -19,6 +20,7 @@ DEFAULT_CONFIG = """
# server adress is visited. Only use for debugging! # server adress is visited. Only use for debugging!
expose_visitor_data = false expose_visitor_data = false
expose_current_visitor_number = false expose_current_visitor_number = false
loglevel = "info"
[database] [database]
# Path of the sqlite database file, use ":memory:" to use in RAM only database # Path of the sqlite database file, use ":memory:" to use in RAM only database
...@@ -192,6 +194,9 @@ def initialize_config(logger=None) -> dict: ...@@ -192,6 +194,9 @@ def initialize_config(logger=None) -> dict:
else: else:
print("Config [{}]: {} (overrides previous configs)".format(i+2, p)) print("Config [{}]: {} (overrides previous configs)".format(i+2, p))
if logger is not None:
logger = set_loglevel(config, logger)
return config return config
...@@ -205,6 +210,26 @@ def read_config(config_path: str) -> dict: ...@@ -205,6 +210,26 @@ def read_config(config_path: str) -> dict:
return config return config
def set_loglevel(config, logger):
"""
Set the loglevel based on the config settings
"""
if config["application"]["loglevel"].lower().strip() == "debug":
logger.setLevel(logging.DEBUG)
elif config["application"]["loglevel"].lower().strip() == "info":
logger.setLevel(logging.INFO)
elif config["application"]["loglevel"].lower().strip() == "warning":
logger.setLevel(logging.WARNING)
elif config["application"]["loglevel"].lower().strip() == "error":
logger.setLevel(logging.ERROR)
elif config["application"]["loglevel"].lower().strip() == "critical":
logger.setLevel(logging.CRITICAL)
else:
logger.critical("The loglevel \"{}\" set in config.toml is invalid use one of the following: \"Debug\", \"Info\", \"Warning\", \"Error\" or \"Critical\"".format(config["application"]["loglevel"]))
exit(1)
return logger
def main(): def main():
""" """
Gets run only if config.py is called directly or via `poetry run config` Gets run only if config.py is called directly or via `poetry run config`
...@@ -396,8 +421,16 @@ def create_config(): ...@@ -396,8 +421,16 @@ def create_config():
selection = config_directories[int(selection)] selection = config_directories[int(selection)]
# Create the directory if it doesn't exist yet # Create the directory if it doesn't exist yet
selection.mkdir(mode=0o755, parents=True, exist_ok=True) try:
config_path = Path("{}/{}".format(selection, "00-config.toml")) selection["path"].mkdir(mode=0o755, parents=True, exist_ok=True)
except PermissionError:
print()
print("Error: Didn't have the permissions to create the config directory at {}".format(selection["path"]), file=sys.stderr)
print("Hint: Change the owner of the directory temporarily to {} or run {} config create with more permissions".format(getpass.getuser(), APPLICATION_NAME))
exit()
config_path = Path("{}/{}".format(selection["path"], "00-config.toml"))
# If the 00-config.toml already exists, ask whether it shall be moved to 00-config.toml.old # If the 00-config.toml already exists, ask whether it shall be moved to 00-config.toml.old
if config_path.is_file(): if config_path.is_file():
...@@ -427,7 +460,7 @@ def create_config(): ...@@ -427,7 +460,7 @@ def create_config():
except PermissionError as e: except PermissionError as e:
print() print()
print("Error: Didn't have the permissions to write the file to {}".format(config_path), file=sys.stderr) print("Error: Didn't have the permissions to write the file to {}".format(config_path), file=sys.stderr)
print(" Directory \"{}\" belongs to user {} (was running as user {})".format(selection, selection.owner(), getpass.getuser()), file=sys.stderr) print(" Directory \"{}\" belongs to user {} (was running as user {})".format(selection["path"], selection["path"].owner(), getpass.getuser()), file=sys.stderr)
print() print()
print("Hint: Change the owner of the directory temporarily to {} or run {} config create with more permissions".format(getpass.getuser(), APPLICATION_NAME)) print("Hint: Change the owner of the directory temporarily to {} or run {} config create with more permissions".format(getpass.getuser(), APPLICATION_NAME))
exit() exit()
......
...@@ -214,6 +214,14 @@ def get(): ...@@ -214,6 +214,14 @@ def get():
return render_template('all.html', title=APPLICATION_NAME, visitors=visitors) return render_template('all.html', title=APPLICATION_NAME, visitors=visitors)
@app.route('/config/database/id_patterns', methods = ['GET'])
def get_rconfig_database_id_patterns():
return "\n".join([str(p.pattern).strip() for p in config["database"]["id_patterns"]])
@app.route('/alive', methods = ['GET'])
def get_is_alive():
return "yes"
# This gets run for each request # This gets run for each request
@app.route('/today', methods = ['GET']) @app.route('/today', methods = ['GET'])
def today(): def today():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment