This repository has been archived on 2023-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
web-gpt/project/__init__.py

98 lines
3.1 KiB
Python
Raw Normal View History

2023-04-18 01:27:49 +08:00
from flask import Flask, request, send_from_directory
2023-03-31 22:11:34 +08:00
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
2023-04-17 22:44:07 +08:00
import yaml
2023-04-17 23:28:00 +08:00
import logging
import time
import traceback
2023-04-18 01:27:49 +08:00
import os
2023-03-31 22:11:34 +08:00
# init SQLAlchemy so we can use it later in our models
db = SQLAlchemy()
def create_app():
app = Flask(__name__)
2023-04-17 23:59:48 +08:00
print("project instance dir: ", app.instance_path)
config_file = app.instance_path+"/config.yaml"
print("config file path: ", config_file)
with open(config_file) as config_file:
conf = yaml.safe_load(config_file)
app.config['SECRET_KEY'] = conf['app']['SECRET_KEY']
app.config['LOGGING_LEVEL'] = conf['app']['LOGGING_LEVEL']
app.config['SQLALCHEMY_DATABASE_URI'] = conf['app']['SQLALCHEMY_DATABASE_URI']
app.config['HOMEPAGE_NOTICE'] = conf['app']['HOMEPAGE_NOTICE']
app.config['NETWORK_PROXY'] = conf['network']['PROXY']
app.config['OPENAI_API_KEY'] = conf['openai']['API_KEY']
app.config['OPENAI_MODEL_NAME'] = conf['openai']['MODEL_NAME']
app.config['OPENAI_PROMPT'] = conf['openai']['PROMPT']
2023-03-31 22:11:34 +08:00
db.init_app(app)
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
2023-03-31 22:53:52 +08:00
login_manager.login_message = "请先登录"
2023-03-31 22:11:34 +08:00
from .models import User
@login_manager.user_loader
def load_user(user_id):
# since the user_id is just the primary key of our user table, use it in the query for the user
return User.query.get(int(user_id))
with app.app_context():
db.create_all()
# blueprint for auth routes in our app
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
# blueprint for non-auth parts of app
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
2023-04-08 10:41:13 +08:00
app = create_app()
2023-04-17 23:28:00 +08:00
logger = logging.getLogger('waitress')
logger.setLevel(app.config['LOGGING_LEVEL'])
2023-04-18 01:28:58 +08:00
def get_actual_addr(request):
ip = request.remote_addr
if request.headers.getlist("X-Forwarded-For"):
ip = request.headers.getlist("X-Forwarded-For")[0]
return ip
2023-04-18 01:27:49 +08:00
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
2023-04-17 23:28:00 +08:00
@app.before_request
def before_request():
timestamp = time.strftime('[%Y-%b-%d %H:%M]')
2023-04-18 01:28:58 +08:00
logger.info('%s > %s %s %s %s', timestamp, get_actual_addr(request),
2023-04-17 23:28:00 +08:00
request.method, request.scheme, request.full_path)
@app.after_request
def after_request(response):
timestamp = time.strftime('[%Y-%b-%d %H:%M]')
2023-04-18 01:28:58 +08:00
logger.info('%s < %s %s %s %s %s', timestamp, get_actual_addr(request),
2023-04-17 23:28:00 +08:00
request.method, request.scheme, request.full_path, response.status)
return response
@app.errorhandler(Exception)
def exceptions(e):
tb = traceback.format_exc()
timestamp = time.strftime('[%Y-%b-%d %H:%M]')
logger.error('%s %s %s %s %s 5xx INTERNAL SERVER ERROR\n%s', timestamp,
2023-04-18 01:28:58 +08:00
get_actual_addr(request), request.method, request.scheme, request.full_path, tb)
2023-04-17 23:28:00 +08:00
return e.status_code