import logging
import os

from flask import Flask
from flask import Blueprint
from flask import request

from be.view import testconn
from be.view import auth
from be.view import seller
from be.view import buyer
from be.model.creatTB import createTable
from be.model.buyer import tostop

bp_shutdown = Blueprint("shutdown", __name__)

def shutdown_server():
    func = request.environ.get("werkzeug.server.shutdown")
    if func is None:
        raise RuntimeError("Not running with the Werkzeug Server")
    func()


@bp_shutdown.route("/shutdown", methods=["POST","GET"])
def be_shutdown():
    # tostop() ## 不需要测试自动取消时删去此部分
    shutdown_server()
    return "Server shutting down..."

def be_run():
    this_path = os.path.dirname(__file__)
    parent_path = os.path.dirname(this_path)
    log_file = os.path.join(parent_path, "app.log")
    createTable()
    
    logging.basicConfig(filename=log_file, level=logging.ERROR)
    handler = logging.StreamHandler()
    formatter = logging.Formatter(
        "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s"
    )
    handler.setFormatter(formatter)
    logging.getLogger().addHandler(handler)
    
    app = Flask(__name__)
    
    app.register_blueprint(bp_shutdown)
    app.register_blueprint(testconn.bp_testconn)
    app.register_blueprint(auth.bp_auth)
    app.register_blueprint(seller.bp_seller)
    app.register_blueprint(buyer.bp_buyer)
    
    app.run()