You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
stacosys/run.py

125 lines
3.8 KiB
Python

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import os
6 years ago
import argparse
import logging
import hashlib
from stacosys.conf.config import Config, ConfigParameter
3 years ago
from stacosys.db import database
from stacosys.core.rss import Rss
from stacosys.core.mailer import Mailer
from stacosys.interface import app
from stacosys.interface import api
from stacosys.interface import form
from stacosys.interface import scheduler
6 years ago
# configure logging
def configure_logging(level):
root_logger = logging.getLogger()
root_logger.setLevel(level)
ch = logging.StreamHandler()
ch.setLevel(level)
# create formatter
formatter = logging.Formatter("[%(asctime)s] %(name)s %(levelname)s %(message)s")
6 years ago
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
root_logger.addHandler(ch)
6 years ago
def stacosys_server(config_pathname):
6 years ago
# configure logging
logger = logging.getLogger(__name__)
configure_logging(logging.INFO)
logging.getLogger("werkzeug").level = logging.WARNING
logging.getLogger("apscheduler.executors").level = logging.WARNING
6 years ago
# check config file exists
if not os.path.isfile(config_pathname):
logger.error(f"Configuration file '{config_pathname}' not found.")
sys.exit(1)
# initialize config
conf = Config.load(config_pathname)
logger.info(conf.__repr__())
# check database file exists (prevents from creating a fresh db)
db_pathname = conf.get(ConfigParameter.DB_SQLITE_FILE)
if not os.path.isfile(db_pathname):
logger.error(f"Database file '{db_pathname}' not found.")
sys.exit(1)
6 years ago
# initialize database
db = database.Database()
db.setup(db_pathname)
6 years ago
logger.info("Start Stacosys application")
6 years ago
6 years ago
# generate RSS for all sites
rss = Rss(
conf.get(ConfigParameter.LANG),
conf.get(ConfigParameter.RSS_FILE),
conf.get(ConfigParameter.RSS_PROTO),
conf.get(ConfigParameter.SITE_NAME),
conf.get(ConfigParameter.SITE_URL),
)
rss.generate()
# configure mailer
mailer = Mailer(
conf.get(ConfigParameter.IMAP_HOST),
conf.get_int(ConfigParameter.IMAP_PORT),
conf.get_bool(ConfigParameter.IMAP_SSL),
conf.get(ConfigParameter.IMAP_LOGIN),
conf.get(ConfigParameter.IMAP_PASSWORD),
conf.get(ConfigParameter.SMTP_HOST),
conf.get_int(ConfigParameter.SMTP_PORT),
conf.get_bool(ConfigParameter.SMTP_STARTTLS),
conf.get_bool(ConfigParameter.SMTP_SSL),
conf.get(ConfigParameter.SMTP_LOGIN),
conf.get(ConfigParameter.SMTP_PASSWORD),
conf.get(ConfigParameter.SITE_ADMIN_EMAIL)
)
6 years ago
# configure mailer logger
mail_handler = mailer.get_error_handler()
logger.addHandler(mail_handler)
app.logger.addHandler(mail_handler)
# configure scheduler
conf.put(ConfigParameter.SITE_TOKEN, hashlib.sha1(conf.get(ConfigParameter.SITE_NAME).encode('utf-8')).hexdigest())
scheduler.configure(
conf.get_int(ConfigParameter.IMAP_POLLING),
conf.get_int(ConfigParameter.COMMENT_POLLING),
conf.get(ConfigParameter.LANG),
conf.get(ConfigParameter.SITE_NAME),
conf.get(ConfigParameter.SITE_TOKEN),
conf.get(ConfigParameter.SITE_ADMIN_EMAIL),
mailer,
rss,
)
6 years ago
# inject config parameters into flask
app.config.update(SITE_REDIRECT=conf.get(ConfigParameter.SITE_REDIRECT))
logger.info(f"start interfaces {api} {form}")
# start Flask
6 years ago
app.run(
host=conf.get(ConfigParameter.HTTP_HOST),
port=conf.get(ConfigParameter.HTTP_PORT),
6 years ago
debug=False,
use_reloader=False,
)
if __name__ == "__main__":
6 years ago
parser = argparse.ArgumentParser()
parser.add_argument("config", help="config path name")
6 years ago
args = parser.parse_args()
stacosys_server(args.config)