Refactor application startup.

Use Flask app config and remove package singletons
pull/18/head
Yax 6 months ago
parent 885593db5b
commit 477477edae

@ -7,7 +7,6 @@ import background
from flask import Flask from flask import Flask
from stacosys.db import dao from stacosys.db import dao
from stacosys.service import config, mailer
from stacosys.service.configuration import ConfigParameter from stacosys.service.configuration import ConfigParameter
app = Flask(__name__) app = Flask(__name__)
@ -20,7 +19,7 @@ logger = logging.getLogger(__name__)
@background.task @background.task
def submit_new_comment(comment): def submit_new_comment(comment):
site_url = config.get(ConfigParameter.SITE_URL) site_url = app.config['CONFIG'].get(ConfigParameter.SITE_URL)
comment_list = ( comment_list = (
f"Web admin interface: {site_url}/web/admin", f"Web admin interface: {site_url}/web/admin",
"", "",
@ -35,9 +34,9 @@ def submit_new_comment(comment):
email_body = "\n".join(comment_list) email_body = "\n".join(comment_list)
# send email to notify admin # send email to notify admin
site_name = config.get(ConfigParameter.SITE_NAME) site_name = app.config['CONFIG'].get(ConfigParameter.SITE_NAME)
subject = f"STACOSYS {site_name}" subject = f"STACOSYS {site_name}"
if mailer.send(subject, email_body): if app.config['MAILER'].send(subject, email_body):
logger.debug("new comment processed") logger.debug("new comment processed")
# save notification datetime # save notification datetime
dao.notify_comment(comment) dao.notify_comment(comment)

@ -6,7 +6,6 @@ from flask import abort, redirect, request
from stacosys.db import dao from stacosys.db import dao
from stacosys.interface import app, submit_new_comment from stacosys.interface import app, submit_new_comment
from stacosys.service import config
from stacosys.service.configuration import ConfigParameter from stacosys.service.configuration import ConfigParameter
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -47,7 +46,7 @@ def new_form_comment():
# send notification e-mail asynchronously # send notification e-mail asynchronously
submit_new_comment(comment) submit_new_comment(comment)
return redirect(config.get(ConfigParameter.SITE_REDIRECT), code=302) return redirect(app.config['CONFIG'].get(ConfigParameter.SITE_REDIRECT), code=302)
def check_form_data(posted_comment): def check_form_data(posted_comment):

@ -8,7 +8,6 @@ from flask import flash, redirect, render_template, request, session
from stacosys.db import dao from stacosys.db import dao
from stacosys.interface import app from stacosys.interface import app
from stacosys.service import config, rss
from stacosys.service.configuration import ConfigParameter from stacosys.service.configuration import ConfigParameter
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -25,8 +24,8 @@ def index():
def is_login_ok(username, password): def is_login_ok(username, password):
hashed = hashlib.sha256(password.encode()).hexdigest().upper() hashed = hashlib.sha256(password.encode()).hexdigest().upper()
return ( return (
config.get(ConfigParameter.WEB_USERNAME) == username app.config['CONFIG'].get(ConfigParameter.WEB_USERNAME) == username
and config.get(ConfigParameter.WEB_PASSWORD) == hashed and app.config['CONFIG'].get(ConfigParameter.WEB_PASSWORD) == hashed
) )
@ -42,7 +41,7 @@ def login():
flash("Identifiant ou mot de passe incorrect") flash("Identifiant ou mot de passe incorrect")
return redirect("/web/login") return redirect("/web/login")
# GET # GET
return render_template("login_" + config.get(ConfigParameter.LANG) + ".html") return render_template("login_" + app.config['CONFIG'].get(ConfigParameter.LANG) + ".html")
@app.route("/web/logout", methods=["GET"]) @app.route("/web/logout", methods=["GET"])
@ -55,7 +54,7 @@ def logout():
def admin_homepage(): def admin_homepage():
if not ( if not (
"user" in session "user" in session
and session["user"] == config.get(ConfigParameter.WEB_USERNAME) and session["user"] == app.config['CONFIG'].get(ConfigParameter.WEB_USERNAME)
): ):
# TODO localization # TODO localization
flash("Vous avez été déconnecté.") flash("Vous avez été déconnecté.")
@ -63,9 +62,9 @@ def admin_homepage():
comments = dao.find_not_published_comments() comments = dao.find_not_published_comments()
return render_template( return render_template(
"admin_" + config.get(ConfigParameter.LANG) + ".html", "admin_" + app.config['CONFIG'].get(ConfigParameter.LANG) + ".html",
comments=comments, comments=comments,
baseurl=config.get(ConfigParameter.SITE_URL), baseurl=app.config['CONFIG'].get(ConfigParameter.SITE_URL),
) )
@ -77,7 +76,7 @@ def admin_action():
flash("Commentaire introuvable") flash("Commentaire introuvable")
elif request.form.get("action") == "APPROVE": elif request.form.get("action") == "APPROVE":
dao.publish_comment(comment) dao.publish_comment(comment)
rss.generate() app.config['RSS'].generate()
# TODO localization # TODO localization
flash("Commentaire publié") flash("Commentaire publié")
else: else:

@ -9,55 +9,34 @@ import sys
from stacosys.db import database from stacosys.db import database
from stacosys.interface import api, app, form from stacosys.interface import api, app, form
from stacosys.interface.web import admin from stacosys.interface.web import admin
from stacosys.service import config, mailer, rss from stacosys.service.mail import Mailer
from stacosys.service.configuration import ConfigParameter from stacosys.service.rssfeed import Rss
from stacosys.service.configuration import Config, ConfigParameter
# configure logging # configure logging
def configure_logging(level): def configure_logging() -> logging.Logger:
root_logger = logging.getLogger() logging.basicConfig(level=logging.INFO, format="[%(asctime)s] %(name)s %(levelname)s %(message)s")
root_logger.setLevel(level)
handler = logging.StreamHandler()
handler.setLevel(level)
formatter = logging.Formatter("[%(asctime)s] %(name)s %(levelname)s %(message)s")
handler.setFormatter(formatter)
root_logger.addHandler(handler)
def stacosys_server(config_pathname):
# configure logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
configure_logging(logging.INFO)
logging.getLogger("werkzeug").level = logging.WARNING logging.getLogger("werkzeug").level = logging.WARNING
return logger
# check config file exists
def load_and_validate_config(config_pathname: str, logger: logging.Logger) -> Config:
if not os.path.isfile(config_pathname): if not os.path.isfile(config_pathname):
logger.error("Configuration file '%s' not found.", config_pathname) logger.error("Configuration file '%s' not found.", config_pathname)
sys.exit(1) raise FileNotFoundError(f"Configuration file '{config_pathname}' not found.")
# load and check config config = Config()
config.load(config_pathname) config.load(config_pathname)
is_config_ok, config_error = config.check() if not config.check():
if not is_config_ok: raise ValueError(f"Invalid configuration '{config_pathname}'")
logger.error("Invalid configuration '%s'", config_error) logger.info("Configuration loaded successfully.")
sys.exit(1) return config
logger.info(config)
# initialize database
database.configure(config.get(ConfigParameter.DB))
logger.info("Start Stacosys application")
# generate RSS
rss.configure(
config.get(ConfigParameter.RSS_FILE),
config.get(ConfigParameter.SITE_NAME),
config.get(ConfigParameter.SITE_PROTO),
config.get(ConfigParameter.SITE_URL),
)
rss.generate()
# configure mailer def configure_and_validate_mailer(config, logger):
mailer = Mailer()
mailer.configure_smtp( mailer.configure_smtp(
config.get(ConfigParameter.SMTP_HOST), config.get(ConfigParameter.SMTP_HOST),
config.get_int(ConfigParameter.SMTP_PORT), config.get_int(ConfigParameter.SMTP_PORT),
@ -68,10 +47,34 @@ def stacosys_server(config_pathname):
if not mailer.check(): if not mailer.check():
logger.error("Email configuration not working") logger.error("Email configuration not working")
sys.exit(1) sys.exit(1)
return mailer
logger.info("start interfaces %s %s %s", api, form, admin)
# start Flask def configure_rss(config):
rss = Rss()
rss.configure(
config.get(ConfigParameter.RSS_FILE),
config.get(ConfigParameter.SITE_NAME),
config.get(ConfigParameter.SITE_PROTO),
config.get(ConfigParameter.SITE_URL),
)
rss.generate()
return rss
def main(config_pathname):
logger = configure_logging()
config = load_and_validate_config(config_pathname, logger)
database.configure(config.get(ConfigParameter.DB))
logger.info("Start Stacosys application")
rss = configure_rss(config)
mailer = configure_and_validate_mailer(config, logger)
logger.info("start interfaces %s %s %s", api, form, admin)
app.config['CONFIG'] = config
app.config['MAILER'] = mailer
app.config['RSS'] = rss
app.run( app.run(
host=config.get(ConfigParameter.HTTP_HOST), host=config.get(ConfigParameter.HTTP_HOST),
port=config.get_int(ConfigParameter.HTTP_PORT), port=config.get_int(ConfigParameter.HTTP_PORT),
@ -84,4 +87,8 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("config", help="config path name") parser.add_argument("config", help="config path name")
args = parser.parse_args() args = parser.parse_args()
stacosys_server(args.config) try:
main(args.config)
except Exception as e:
logging.error(f"Failed to start application: {e}")
sys.exit(1)

@ -1,10 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from .configuration import Config
from .mail import Mailer
from .rssfeed import Rss
config = Config()
mailer = Mailer()
rss = Rss()
Loading…
Cancel
Save