#!/usr/bin/env python3

from pathlib import Path

import libsession
from flask import Flask, g, redirect, request
from mod_api import mod_api
from mod_csp import mod_csp
from mod_hello import mod_hello
from mod_mfa import mod_mfa
from mod_posts import mod_posts
from mod_user import mod_user

app = Flask("Athena")
app.config[
    "SECRET_KEY"
] = "123aa8a93bdde342c871564a62282af857bda14b3359fde95d0c5e4b321610c1"

app.register_blueprint(mod_hello, url_prefix="/hello")
app.register_blueprint(mod_user, url_prefix="/user")
app.register_blueprint(mod_posts, url_prefix="/posts")
app.register_blueprint(mod_mfa, url_prefix="/mfa")
app.register_blueprint(mod_csp, url_prefix="/csp")
app.register_blueprint(mod_api, url_prefix="/api")

csp_file = Path("csp.txt")
csp = ""

if csp_file.is_file():
    with csp_file.open() as f:
        for line in f.readlines():
            if line.startswith("#"):
                continue
            line = line.replace("\n", "")
            if line:
                csp += line
        print("CSP:", csp)


@app.route("/")
def do_home():
    return redirect("/posts")


@app.before_request
def before_request():
    g.session = libsession.load(request)


@app.after_request
def add_csp_headers(response):
    if csp:
        response.headers["Content-Security-Policy"] = csp
    return response


app.run(debug=True, host="127.0.1.1", port=5001, extra_files="csp.txt")
