app.py 2.8 KB
Newer Older
D
dogsheng 已提交
1 2
#!/usr/bin/python3
# -*- coding: utf-8 -*-
W
willwolf 已提交
3
# Copyright (c) 2020 Huawei Technologies Co., Ltd.
4 5 6
# A-Tune is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
7
#     http://license.coscl.org.cn/MulanPSL2
D
dogsheng 已提交
8 9 10
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
11
# See the Mulan PSL v2 for more details.
W
willwolf 已提交
12
# Create: 2020-08-25
D
dogsheng 已提交
13 14

"""
W
willwolf 已提交
15
Flask application initialization, including log configuration, restful api registration.
D
dogsheng 已提交
16 17
"""
import os
Z
Zhipeng Xie 已提交
18
import ssl
D
dogsheng 已提交
19
import logging
Z
Zhipeng Xie 已提交
20 21 22 23
from configparser import ConfigParser
from logging.handlers import SysLogHandler
from flask import Flask
from flask_restful import Api
D
dogsheng 已提交
24 25


W
willwolf 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
class App:
    """flask application"""

    def __init__(self):
        self.app = Flask(__name__)
        self.app.config.update(SESSION_COOKIE_SECURE=True,
                               SESSION_COOKIE_HTTPONLY=True,
                               SESSION_COOKIE_SAMESITE='Lax')
        self.api = Api(self.app)

    @staticmethod
    def config_log(level):
        """app config log"""
        log = logging.getLogger('werkzeug')
        log.setLevel(logging.ERROR)
        logging_format = logging.Formatter('atuned: %(asctime)s [%(levelname)s] '
                                           '%(module)s [%(pathname)s:%(lineno)d] : %(message)s')
        syslog_handler = SysLogHandler(address="/dev/log", facility=SysLogHandler.LOG_LOCAL0)
        syslog_handler.setFormatter(logging_format)

        root_logger = logging.getLogger()
        root_logger.setLevel(level)
        root_logger.addHandler(syslog_handler)

    def add_resource(self):
        """flask app add resource"""

    def startup_app(self, filename, host_tag, port_tag, tls_tag, cert_tag, key_tag, ca_tag):
        """start flask app"""
        if not os.path.exists(filename):
            return
        config = ConfigParser()
        config.read(filename)
        level = logging.getLevelName(config.get("log", "level").upper())
        self.config_log(level)
        self.add_resource()
        host = config.get("server", host_tag)
        port = config.get("server", port_tag)
        context = None
        if config.has_option("server", tls_tag) and config.get("server", tls_tag) == "true":
            cert_file = config.get("server", cert_tag)
            key_file = config.get("server", key_tag)
            ca_file = config.get("server", ca_tag)
            context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
            context.load_cert_chain(certfile=cert_file, keyfile=key_file)
            context.load_verify_locations(ca_file)
            context.verify_mode = ssl.CERT_REQUIRED
        self.app.run(host=host, port=port, ssl_context=context)