Skip to content

Commit

Permalink
only install routes once
Browse files Browse the repository at this point in the history
  • Loading branch information
jokester committed Jun 8, 2024
1 parent 6576b5f commit fd1dfb9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ format:
test:
@bash -c "set -uexo allexport && source .env.test && exec venv/bin/pytest"

test_smoketest:
test_single:
@bash -c "set -uexo allexport && source .env.test && exec venv/bin/pytest --capture=sys --log-cli-level=DEBUG tests/base/test_not_exist_error.py"

test_logging:
#--capture=no
# WTF: setting level to above WARNING not working?
@bash -c "set -uexo allexport && source .env.test && exec venv/bin/pytest --capture=sys --log-cli-level=DEBUG tests/base/test_it_runs.py"
# @bash -c "set -uexo allexport && source .env.test && exec venv/bin/pytest --capture=sys --log-cli-level=DEBUG tests/base/test_it_runs.py"
23 changes: 2 additions & 21 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@
import logging

from flask import Flask, g, request
from flask_apikit import APIKit
from flask_babel import Babel

from .factory import app_config, create_celery, create_flask_app
from .factory import app_config, create_celery, create_flask_app, babel, oss

from app.constants.locale import Locale
from app.core.rbac import AllowApplyType, ApplicationCheckType
from app.services.google_storage import GoogleStorage
from app.services.oss import OSS
from app.utils.logging import configure_root_logger, configure_extra_logs

from .apis import register_apis

configure_root_logger()
flask_app = create_flask_app(Flask(__name__))
configure_extra_logs(flask_app)
Expand All @@ -28,11 +23,7 @@
FILE_PATH = os.path.abspath(os.path.join(APP_PATH, "..", "files")) # 一般文件
TMP_PATH = os.path.abspath(os.path.join(FILE_PATH, "tmp")) # 临时文件存放地址
STORAGE_PATH = os.path.abspath(os.path.join(APP_PATH, "..", "storage")) # 储存地址
# 插件
babel = Babel()
oss = OSS()
gs_vision = GoogleStorage()
apikit = APIKit()


def create_default_team(admin_user):
Expand Down Expand Up @@ -84,17 +75,7 @@ def create_or_override_default_admin(app):


def create_app():
app = flask_app
logger.info("-" * 50)
register_apis(app)
babel.init_app(app)
apikit.init_app(app)

logger.info("-" * 50)
logger.info("站点支持语言: " + str([str(i) for i in babel.list_translations()]))
oss.init(app.config) # 文件储存

return app
return flask_app


def init_db(app: Flask):
Expand Down
2 changes: 1 addition & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
SECRET_KEY = env["SECRET_KEY"] # 必填 - 密钥
LOG_LEVEL = env.get("LOG_LEVEL", "INFO")
# DEPRECATED: please use modern container logging collector
LOG_PATH = env.get('LOG_PATH')
LOG_PATH = env.get("LOG_PATH")
MAX_CONTENT_LENGTH = int(env.get("MAX_CONTENT_LENGTH_MB", 1024)) * 1024 * 1024
ADMIN_EMAIL = env["ADMIN_EMAIL"]
# -----------
Expand Down
21 changes: 21 additions & 0 deletions app/factory.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import logging
from celery import Celery
from flask import Flask
from flask.logging import default_handler as flask_default_handler
from flask_apikit import APIKit
from flask_babel import Babel
import app.config as _app_config
from app.services.oss import OSS
from .apis import register_apis

from app.models import connect_db

logger = logging.getLogger(__name__)
babel = Babel()
apikit = APIKit()
# 插件
oss = OSS()

app_config = {
k: getattr(_app_config, k) for k in dir(_app_config) if not k.startswith("_")
}
Expand All @@ -20,6 +32,15 @@ def create_flask_app(app: Flask) -> Flask:
return app


def init_flask_app(app: Flask) -> Flask:
register_apis(app)
babel.init_app(app)
apikit.init_app(app)
logger.info("-" * 50)
logger.info("站点支持语言: " + str([str(i) for i in babel.list_translations()]))
oss.init(app.config) # 文件储存


def create_celery(app: Flask) -> Celery:
# 通过app配置创建celery实例
created = Celery(
Expand Down

0 comments on commit fd1dfb9

Please sign in to comment.