This repository has been archived on 2026-08-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
RequestCounter/app/__init__.py
T

59 lines
1.3 KiB
Python
Raw Normal View History

2022-03-26 08:02:25 +08:00
#!/usr/bin/env python3
# -- coding:utf-8 --
# @Author: markushammered@gmail.com
# @Development Tool: PyCharm
# @Create Time: 2022/3/25
# @File Name: __init__.py
2022-04-10 14:31:24 +08:00
import os
import random
2022-03-26 08:02:25 +08:00
from flask import Flask
2022-04-07 21:53:36 +08:00
from flask_sslify import SSLify
from .main import main as main_blueprint
from .api import api as api_blueprint
2022-04-05 17:56:46 +08:00
from app.config import config
2022-03-27 17:03:20 +08:00
2022-04-10 14:31:24 +08:00
def generate_random_string(length):
"""
生成随机密码
:param length:
:return:
"""
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
return ''.join(random.choice(letters) for _ in range(length))
2022-03-26 08:41:15 +08:00
def create_app(config_name):
2022-04-10 14:31:24 +08:00
"""
创建 app
:param config_name:
:return:
"""
2022-03-26 08:02:25 +08:00
app = Flask(__name__)
2022-03-26 08:41:15 +08:00
app.config.from_object(config[config_name])
config[config_name].init_app(app)
2022-04-10 14:31:24 +08:00
if os.environ.get('SSL_REDIRECT'):
SSLify(app)
2022-03-26 08:41:15 +08:00
app.register_blueprint(main_blueprint)
2022-04-09 11:11:13 +08:00
app.register_blueprint(api_blueprint, url_prefix='/api/v1/')
2022-03-26 08:02:25 +08:00
2022-04-10 14:31:24 +08:00
if not os.getenv('ACCESS_KEY'):
access_key = generate_random_string(32)
os.environ['ACCESS_KEY'] = access_key
else:
access_key = os.getenv('ACCESS_KEY')
print('==========Access Key: {}=========='.format(access_key))
2022-03-26 08:02:25 +08:00
return app
2022-03-26 09:07:54 +08:00
2022-03-26 08:41:15 +08:00
2022-04-02 18:03:25 +08:00
__all__ = [
'db',
'main',
'tests',
'utils',
'create_app'
]