fastapi-scaff 0.3.4__py3-none-any.whl → 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of fastapi-scaff might be problematic. Click here for more details.

fastapi_scaff/__init__.py CHANGED
@@ -7,4 +7,4 @@
7
7
  @history
8
8
  """
9
9
 
10
- __version__ = "0.3.4"
10
+ __version__ = "0.4.0"
fastapi_scaff/__main__.py CHANGED
@@ -13,7 +13,7 @@ import re
13
13
  import sys
14
14
  from pathlib import Path
15
15
 
16
- from . import __version__
16
+ from fastapi_scaff import __version__
17
17
 
18
18
  here = Path(__file__).absolute().parent
19
19
  prog = "fastapi-scaff"
@@ -42,9 +42,12 @@ def main():
42
42
  type=str,
43
43
  help="项目或api名称(多个api可逗号分隔)")
44
44
  parser.add_argument(
45
- "--light",
46
- action='store_true',
47
- help="`new`时可指定项目结构是否轻量版(默认标准版)")
45
+ "-e",
46
+ "--edition",
47
+ default="standard",
48
+ choices=["standard", "light", "micro"],
49
+ metavar="",
50
+ help="`new`时可指定项目结构版本(默认标准版)")
48
51
  parser.add_argument(
49
52
  "-d",
50
53
  "--db",
@@ -124,10 +127,9 @@ class CMD:
124
127
  with open(here.joinpath("_project_tpl.json"), "r") as f:
125
128
  project = json.loads(f.read())
126
129
  for k, v in project.items():
127
- if self.args.light:
128
- k, v = self._light_handler(k, v)
129
- if not k:
130
- continue
130
+ k, v = self._edition_handler(self.args.edition, k, v)
131
+ if not k:
132
+ continue
131
133
  tplpath = name.joinpath(k)
132
134
  tplpath.parent.mkdir(parents=True, exist_ok=True)
133
135
  with open(tplpath, "w+", encoding="utf-8") as f:
@@ -162,8 +164,17 @@ class CMD:
162
164
  f"----- More see README.md -----\n")
163
165
 
164
166
  @staticmethod
165
- def _light_handler(k: str, v: str):
166
- pat = r"^({filter_k})".format(filter_k="|".join([
167
+ def _edition_handler(edition: str, k: str, v: str):
168
+ if k in [
169
+ "app/initializer.py",
170
+ "app/middleware.py",
171
+ ]:
172
+ if edition == "micro":
173
+ return k, v
174
+ return None, None
175
+ if edition == "standard":
176
+ return k, v
177
+ filter_list = [
167
178
  "app/api/default/aping.py",
168
179
  "app/api/v1/user.py",
169
180
  "app/initializer/_redis.py",
@@ -177,7 +188,24 @@ class CMD:
177
188
  "tests/",
178
189
  "runcbeat.py",
179
190
  "runcworker.py",
180
- ]))
191
+ ]
192
+ if edition == "micro":
193
+ filter_list = [
194
+ "app/api/default/aping.py",
195
+ "app/api/v1/user.py",
196
+ "app/initializer/",
197
+ "app/middleware/",
198
+ "app/models/",
199
+ "app/schemas/",
200
+ "app/services/",
201
+ "app_celery/",
202
+ "deploy/",
203
+ "docs/",
204
+ "tests/",
205
+ "runcbeat.py",
206
+ "runcworker.py",
207
+ ]
208
+ pat = r"^({filter_k})".format(filter_k="|".join(filter_list))
181
209
  if re.match(pat, k) is not None:
182
210
  return None, None
183
211
  if k == "app/api/status.py":
@@ -326,10 +354,23 @@ celery==5.5.3""", "")
326
354
  work_dir = Path.cwd()
327
355
  with open(here.joinpath("_api_tpl.json"), "r", encoding="utf-8") as f:
328
356
  api_tpl_dict = json.loads(f.read())
329
- if target == "a":
357
+ if target != "a":
358
+ if not any([
359
+ work_dir.joinpath("app/schemas").is_dir(),
360
+ work_dir.joinpath("app/models").is_dir(),
361
+ ]):
362
+ target = "light"
363
+ if not work_dir.joinpath("app/services").is_dir():
364
+ target = "micro"
365
+ if target in ["a", "micro"]:
330
366
  tpl_mods = [
331
367
  "app/api",
332
368
  ]
369
+ elif target == "light":
370
+ tpl_mods = [
371
+ "app/api",
372
+ "app/services",
373
+ ]
333
374
  elif target == "as":
334
375
  tpl_mods = [
335
376
  "app/api",
@@ -343,16 +384,6 @@ celery==5.5.3""", "")
343
384
  "app/schemas",
344
385
  "app/models",
345
386
  ]
346
- if target != "a":
347
- if not any([work_dir.joinpath(mod).is_dir() for mod in [
348
- "app/schemas",
349
- "app/models",
350
- ]]): # is light
351
- target = "light"
352
- tpl_mods = [
353
- "app/api",
354
- "app/services",
355
- ]
356
387
  for mod in tpl_mods:
357
388
  if not work_dir.joinpath(mod).is_dir():
358
389
  sys.stderr.write(f"[error] not exists: {mod.replace('/', os.sep)}")
@@ -369,7 +400,7 @@ celery==5.5.3""", "")
369
400
  # - light:
370
401
  # - 创建a时,如果se存在为0,不存在为1
371
402
  # - 创建se时,如果a存在为0,不存在为1
372
- # a (a)
403
+ # a|micro (a)
373
404
  "0": [1],
374
405
  "1": [1],
375
406
  # as (a, se, sc)
@@ -9,6 +9,7 @@
9
9
  "a_app_api.py": "import traceback\n\nfrom fastapi import APIRouter\nfrom starlette.requests import Request\n\nfrom app.api.responses import Responses, response_docs\nfrom app.api.status import Status\nfrom app.initializer import g\n\nrouter = APIRouter()\n\n\n@router.get(\n path=\"/tpl/{tpl_id}\",\n summary=\"tplDetail\",\n responses=response_docs(),\n)\nasync def detail(\n request: Request,\n tpl_id: str,\n # TODO: \u8ba4\u8bc1\n):\n try:\n data = {} # TODO: \u6570\u636e\n if not data:\n return Responses.failure(status=Status.RECORD_NOT_EXIST_ERROR, request=request)\n except Exception as e:\n g.logger.error(traceback.format_exc())\n return Responses.failure(msg=\"tplDetail\u5931\u8d25\", error=e, request=request)\n return Responses.success(data=data, request=request)\n",
10
10
  "light_app_api.py": "import traceback\n\nfrom fastapi import APIRouter\nfrom starlette.requests import Request\n\nfrom app.api.responses import Responses, response_docs\nfrom app.api.status import Status\nfrom app.initializer import g\nfrom app.services.tpl import (\n TplDetailSvc,\n)\n\nrouter = APIRouter()\n\n\n@router.get(\n path=\"/tpl/{tpl_id}\",\n summary=\"tplDetail\",\n responses=response_docs(\n model=TplDetailSvc,\n ),\n)\nasync def detail(\n request: Request,\n tpl_id: str,\n # TODO: \u8ba4\u8bc1\n):\n try:\n tpl_svc = TplDetailSvc(id=tpl_id)\n data = await tpl_svc.detail()\n if not data:\n return Responses.failure(status=Status.RECORD_NOT_EXIST_ERROR, request=request)\n except Exception as e:\n g.logger.error(traceback.format_exc())\n return Responses.failure(msg=\"tplDetail\u5931\u8d25\", error=e, request=request)\n return Responses.success(data=data, request=request)\n",
11
11
  "light_app_services.py": "from pydantic import BaseModel\n\n\nclass TplDetailSvc(BaseModel):\n model_config = {\n \"json_schema_extra\": {\n \"title\": \"TplDetail\"\n }\n }\n\n async def detail(self):\n # TODO: \u4e1a\u52a1\u903b\u8f91\n pass\n",
12
+ "micro_app_api.py": "import traceback\n\nfrom fastapi import APIRouter\nfrom starlette.requests import Request\n\nfrom app.api.responses import Responses, response_docs\nfrom app.api.status import Status\nfrom app.initializer import g\n\nrouter = APIRouter()\n\n\n@router.get(\n path=\"/tpl/{tpl_id}\",\n summary=\"tplDetail\",\n responses=response_docs(),\n)\nasync def detail(\n request: Request,\n tpl_id: str,\n # TODO: \u8ba4\u8bc1\n):\n try:\n data = {} # TODO: \u6570\u636e\n if not data:\n return Responses.failure(status=Status.RECORD_NOT_EXIST_ERROR, request=request)\n except Exception as e:\n g.logger.error(traceback.format_exc())\n return Responses.failure(msg=\"tplDetail\u5931\u8d25\", error=e, request=request)\n return Responses.success(data=data, request=request)\n",
12
13
  "only_app_api.py": "import traceback\n\nfrom fastapi import APIRouter\nfrom starlette.requests import Request\n\nfrom app.api.responses import Responses, response_docs\nfrom app.api.status import Status\nfrom app.initializer import g\n\nrouter = APIRouter()\n\n\n@router.get(\n path=\"/tpl/{tpl_id}\",\n summary=\"tplDetail\",\n responses=response_docs(),\n)\nasync def detail(\n request: Request,\n tpl_id: str,\n # TODO: \u8ba4\u8bc1\n):\n try:\n data = {} # TODO: \u6570\u636e\n if not data:\n return Responses.failure(status=Status.RECORD_NOT_EXIST_ERROR, request=request)\n except Exception as e:\n g.logger.error(traceback.format_exc())\n return Responses.failure(msg=\"tplDetail\u5931\u8d25\", error=e, request=request)\n return Responses.success(data=data, request=request)\n",
13
14
  "only_app_models.py": "from sqlalchemy import Column, String\n\nfrom app.models import DeclBase\nfrom app.initializer import g\n\n\nclass Tpl(DeclBase):\n __tablename__ = \"tpl\"\n\n id = Column(String(20), primary_key=True, default=g.snow_client.gen_uid, comment=\"\u4e3b\u952e\")\n name = Column(String(50), nullable=False, comment=\"\u540d\u79f0\")\n",
14
15
  "only_app_schemas.py": "from pydantic import BaseModel, Field\n\nfrom app.schemas import filter_fields\n\n\nclass TplDetail(BaseModel):\n id: str = Field(...)\n # #\n name: str = None\n\n @classmethod\n def response_fields(cls):\n return filter_fields(\n cls,\n exclude=[]\n )\n",
@@ -1,11 +1,11 @@
1
1
  {
2
2
  ".gitignore": "# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packaging\n.Python\nbuild/\ndevelop-eggs/\ndist/\ndownloads/\neggs/\n.eggs/\nlib/\nlib64/\nparts/\nsdist/\nvar/\nwheels/\nshare/python-wheels/\n*.egg-info/\n.installed.cfg\n*.egg\nMANIFEST\n\n# PyInstaller\n# Usually these files are written by a python script from a template\n# before PyInstaller builds the exe, so as to inject date/other infos into it.\n*.manifest\n*.spec\n\n# Installer logs\npip-log.txt\npip-delete-this-directory.txt\n\n# Unit test / coverage reports\nhtmlcov/\n.tox/\n.nox/\n.coverage\n.coverage.*\n.cache\nnosetests.xml\ncoverage.xml\n*.cover\n*.py,cover\n.hypothesis/\n.pytest_cache/\ncover/\n\n# Translations\n*.mo\n*.pot\n\n# Django stuff:\n*.log\nlocal_settings.py\ndb.sqlite3\ndb.sqlite3-journal\n\n# Flask stuff:\ninstance/\n.webassets-cache\n\n# Scrapy stuff:\n.scrapy\n\n# Sphinx documentation\ndocs/_build/\n\n# PyBuilder\n.pybuilder/\ntarget/\n\n# Jupyter Notebook\n.ipynb_checkpoints\n\n# IPython\nprofile_default/\nipython_config.py\n\n# pyenv\n# For a library or package, you might want to ignore these files since the code is\n# intended to run in multiple environments; otherwise, check them in:\n# .python-version\n\n# pipenv\n# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.\n# However, in case of collaboration, if having platform-specific dependencies or dependencies\n# having no cross-platform support, pipenv may install dependencies that don't work, or not\n# install all needed dependencies.\n#Pipfile.lock\n\n# PEP 582; used by e.g. github.com/David-OConnor/pyflow\n__pypackages__/\n\n# Celery stuff\ncelerybeat-schedule\ncelerybeat.pid\n\n# SageMath parsed files\n*.sage.py\n\n# Environments\n#.env\n.venv\nenv/\nvenv/\nENV/\nenv.bak/\nvenv.bak/\n\n# Spyder project settings\n.spyderproject\n.spyproject\n\n# Rope project settings\n.ropeproject\n\n# mkdocs documentation\n/site\n\n# mypy\n.mypy_cache/\n.dmypy.json\ndmypy.json\n\n# Pyre type checker\n.pyre/\n\n# pytype static type analyzer\n.pytype/\n\n# Cython debug symbols\ncython_debug/\n\n# Append\n.idea\n.vscode\n*.sqlite\ncelerybeat-schedule.*\n",
3
3
  "LICENSE": "Copyright (c) 2024 axiner\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n",
4
- "README.md": "# fastapi-scaff\n\n## What is this?\n\n- by: axiner\n- fastapi-scaff\n- This is a fastapi scaff.\n - new project\n - add api\n - about project:\n - auto init project (conf, db, log...)\n - auto register router\n - auto register middleware\n - ...\n - integrated sqlalchemy\n - integrated jwt\\api-key\n - integrated celery\n - ...\n - more documents: [\u8bf7\u70b9\u51fb\u94fe\u63a5](https://blog.csdn.net/atpuxiner/article/details/144291336?fromshare=blogdetail&sharetype=blogdetail&sharerId=144291336&sharerefer=PC&sharesource=atpuxiner&sharefrom=from_link)\n\n## Project structure\n\n- ASM: ASM\u6a21\u5f0f\n - A api\n - S services(&schemas)\n - M models\n- \u8c03\u7528\u8fc7\u7a0b: main.py(initializer) -> (middleware) - api - services(&schemas) - (models)\n- \u7ed3\u6784\u5982\u4e0b: (\u547d\u540d\u7ecf\u8fc7\u591a\u6b21\u4fee\u6539\u6572\u5b9a\uff0c\u7b80\u6d01\u6613\u61c2)\n ```\n \u2514\u2500\u2500 fastapi-scaff\n \u251c\u2500\u2500 app (\u5e94\u7528)\n \u2502 \u251c\u2500\u2500 api \u251c\u2500\u2500 (api)\n \u2502 \u2502 \u2514\u2500\u2500 v1 \u2502 \u2514\u2500\u2500 (v1)\n \u2502 \u251c\u2500\u2500 initializer \u251c\u2500\u2500 (\u521d\u59cb\u5316)\n \u2502 \u2502 \u251c\u2500\u2500 conf \u2502 \u251c\u2500\u2500 (\u914d\u7f6e)\n \u2502 \u2502 \u251c\u2500\u2500 db \u2502 \u251c\u2500\u2500 (\u6570\u636e\u5e93)\n \u2502 \u2502 \u251c\u2500\u2500 log \u2502 \u251c\u2500\u2500 (\u65e5\u5fd7)\n \u2502 \u2502 \u2514\u2500\u2500 ... \u2502 \u2514\u2500\u2500 (...)\n \u2502 \u251c\u2500\u2500 middleware \u251c\u2500\u2500 (\u4e2d\u95f4\u4ef6)\n \u2502 \u251c\u2500\u2500 models \u251c\u2500\u2500 (\u6570\u636e\u6a21\u578b)\n \u2502 \u251c\u2500\u2500 schemas \u251c\u2500\u2500 (\u6570\u636e\u7ed3\u6784)\n \u2502 \u251c\u2500\u2500 services \u251c\u2500\u2500 (\u4e1a\u52a1\u903b\u8f91)\n \u2502 \u251c\u2500\u2500 utils \u251c\u2500\u2500 (utils)\n \u2502 \u2514\u2500\u2500 main.py \u2514\u2500\u2500 (main.py)\n \u251c\u2500\u2500 app_celery (\u5e94\u7528-celery)\n \u251c\u2500\u2500 config (\u914d\u7f6e\u76ee\u5f55)\n \u251c\u2500\u2500 deploy (\u90e8\u7f72\u76ee\u5f55)\n \u251c\u2500\u2500 docs (\u6587\u6863\u76ee\u5f55)\n \u251c\u2500\u2500 logs (\u65e5\u5fd7\u76ee\u5f55)\n \u251c\u2500\u2500 tests (\u6d4b\u8bd5\u76ee\u5f55)\n \u251c\u2500\u2500 .gitignore\n \u251c\u2500\u2500 LICENSE\n \u251c\u2500\u2500 README.md\n \u251c\u2500\u2500 requirements.txt\n \u2514\u2500\u2500 runcbeat.py\n \u2514\u2500\u2500 runcworker.py\n \u2514\u2500\u2500 runserver.py\n ```\n \n- \u3010\u53e6\uff1a**\u8f7b\u91cf\u7248** > **\u5b8c\u6574\u7ed3\u6784** \u3011\u8bf7\u81ea\u884c\u521b\u5efa\u67e5\u770b\uff08`new`\u9879\u76ee\u65f6\u6307\u5b9a\u53c2\u6570`--light`\uff09\n\n## Installation\n\nThis package can be installed using pip (Python>=3.11):\n> pip install fastapi-scaff\n\n## Scaff usage\n\n- 1\uff09help document\n - `fastapi-scaff -h`\n- 2\uff09new project\n - `fastapi-scaff new <myproj>`\n - *light structure*: `fastapi-scaff new <myproj> --light`\n- 3\uff09add api\n - `cd to project root dir`\n - `fastapi-scaff add <myapi>`\n\n## Project run\n\n- 1\uff09cd to project root dir\n- 2\uff09modify the configuration, such as for the database\n- 3\uff09`pip install -r requirements.txt`\n- 4\uff09`python runserver.py`\n - more parameters see:\n - about uvicorn: [click here](https://www.uvicorn.org/)\n - about gunicorn: [click here](https://docs.gunicorn.org/en/stable/)\n\n## License\n\nThis project is released under the MIT License (MIT). See [LICENSE](LICENSE)\n",
4
+ "README.md": "# fastapi-scaff\n\n## What is this?\n\n- by: axiner\n- fastapi-scaff\n- This is a fastapi scaff.\n - new project\n - add api\n - about project:\n - auto init project (conf, db, log...)\n - auto register router\n - auto register middleware\n - ...\n - integrated sqlalchemy\n - integrated jwt\\api-key\n - integrated celery\n - ...\n - about structure:\n - standard\n - light\n - micro\n - more documents: [\u8bf7\u70b9\u51fb\u94fe\u63a5](https://blog.csdn.net/atpuxiner/article/details/144291336?fromshare=blogdetail&sharetype=blogdetail&sharerId=144291336&sharerefer=PC&sharesource=atpuxiner&sharefrom=from_link)\n\n## Project structure\n\n- ASM: ASM mode\n - A api\n - S services(&schemas)\n - M models\n- Calling process: main.py(initializer) -> (middleware) - api - services(&schemas) - (models)\n- Structure: (The naming has been finalized after multiple revisions, making it concise and easy to understand)\n ```\n \u2514\u2500\u2500 fastapi-scaff\n \u251c\u2500\u2500 app (\u5e94\u7528)\n \u2502 \u251c\u2500\u2500 api \u251c\u2500\u2500 (api)\n \u2502 \u2502 \u2514\u2500\u2500 v1 \u2502 \u2514\u2500\u2500 (v1)\n \u2502 \u251c\u2500\u2500 initializer \u251c\u2500\u2500 (\u521d\u59cb\u5316)\n \u2502 \u2502 \u251c\u2500\u2500 conf \u2502 \u251c\u2500\u2500 (\u914d\u7f6e)\n \u2502 \u2502 \u251c\u2500\u2500 db \u2502 \u251c\u2500\u2500 (\u6570\u636e\u5e93)\n \u2502 \u2502 \u251c\u2500\u2500 log \u2502 \u251c\u2500\u2500 (\u65e5\u5fd7)\n \u2502 \u2502 \u2514\u2500\u2500 ... \u2502 \u2514\u2500\u2500 (...)\n \u2502 \u251c\u2500\u2500 middleware \u251c\u2500\u2500 (\u4e2d\u95f4\u4ef6)\n \u2502 \u251c\u2500\u2500 models \u251c\u2500\u2500 (\u6570\u636e\u6a21\u578b)\n \u2502 \u251c\u2500\u2500 schemas \u251c\u2500\u2500 (\u6570\u636e\u7ed3\u6784)\n \u2502 \u251c\u2500\u2500 services \u251c\u2500\u2500 (\u4e1a\u52a1\u903b\u8f91)\n \u2502 \u251c\u2500\u2500 utils \u251c\u2500\u2500 (utils)\n \u2502 \u2514\u2500\u2500 main.py \u2514\u2500\u2500 (main.py)\n \u251c\u2500\u2500 app_celery (\u5e94\u7528-celery)\n \u251c\u2500\u2500 config (\u914d\u7f6e\u76ee\u5f55)\n \u251c\u2500\u2500 deploy (\u90e8\u7f72\u76ee\u5f55)\n \u251c\u2500\u2500 docs (\u6587\u6863\u76ee\u5f55)\n \u251c\u2500\u2500 logs (\u65e5\u5fd7\u76ee\u5f55)\n \u251c\u2500\u2500 tests (\u6d4b\u8bd5\u76ee\u5f55)\n \u251c\u2500\u2500 .gitignore\n \u251c\u2500\u2500 LICENSE\n \u251c\u2500\u2500 README.md\n \u251c\u2500\u2500 requirements.txt\n \u2514\u2500\u2500 runcbeat.py\n \u2514\u2500\u2500 runcworker.py\n \u2514\u2500\u2500 runserver.py\n ```\n\n- \u3010Other structure\u3011\n - light\uff1aPlease create and view (with `-e light`)\n - micro\uff1aPlease create and view (with `-e micro`)\n\n## Installation\n\nThis package can be installed using pip (Python>=3.11):\n> pip install fastapi-scaff\n\n## Scaff usage\n\n- 1\uff09help document\n - `fastapi-scaff -h`\n- 2\uff09new project\n - `fastapi-scaff new <myproj>`\n - *light structure*: `fastapi-scaff new <myproj> -e light`\n - *micro structure*: `fastapi-scaff new <myproj> -e micro`\n- 3\uff09add api\n - `cd to project root dir`\n - `fastapi-scaff add <myapi>`\n\n## Project run\n\n- 1\uff09cd to project root dir\n- 2\uff09modify the configuration, such as for the database\n- 3\uff09`pip install -r requirements.txt`\n- 4\uff09`python runserver.py`\n - more parameters see:\n - about uvicorn: [click here](https://www.uvicorn.org/)\n - about gunicorn: [click here](https://docs.gunicorn.org/en/stable/)\n\n## License\n\nThis project is released under the MIT License (MIT). See [LICENSE](LICENSE)\n",
5
5
  "requirements.txt": "# -*- coding: utf-8 -*-\n# Python>=3.11\nfastapi==0.120.0\nuvicorn==0.38.0\norjson==3.11.3\ntoollib==1.8.5\npython-dotenv==1.1.1\nPyYAML==6.0.3\nloguru==0.7.3\nSQLAlchemy==2.0.44\naiosqlite==0.21.0\nredis==7.0.0\nPyJWT==2.10.1\nbcrypt==5.0.0\ncelery==5.5.3",
6
6
  "runcbeat.py": "\"\"\"\n@author axiner\n@version v1.0.0\n@created 2025/09/20 10:10\n@abstract runcbeat\uff08\u66f4\u591a\u53c2\u6570\u8bf7\u81ea\u884c\u6307\u5b9a\uff09\n@description\n@history\n\"\"\"\nimport argparse\nimport subprocess\n\n\ndef main(\n loglevel: str = \"info\",\n scheduler: str = None,\n pidfile: str = None,\n max_interval: int = 5,\n):\n parser = argparse.ArgumentParser(description=\"CeleryBeat\u542f\u52a8\u5668\")\n parser.add_argument(\"-l\", \"--loglevel\", type=str, default=\"info\", metavar=\"\", help=\"\u65e5\u5fd7\u7b49\u7ea7\")\n parser.add_argument(\"-S\", \"--scheduler\", type=str, default=None, metavar=\"\", help=\"\u8c03\u5ea6\u5668\u7c7b\u578b\")\n parser.add_argument(\"--pidfile\", type=str, default=None, metavar=\"\", help=\"pid\u6587\u4ef6\")\n parser.add_argument(\"--max-interval\", type=int, default=5, metavar=\"\", help=\"\u68c0\u6d4b\u4efb\u52a1\u95f4\u9694\")\n args = parser.parse_args()\n loglevel = args.loglevel or loglevel\n scheduler = args.scheduler or scheduler\n pidfile = args.pidfile or pidfile\n max_interval = args.max_interval or max_interval\n command = [\n \"celery\",\n \"-A\",\n \"app_celery.consumer\",\n \"beat\",\n f\"--loglevel={loglevel}\",\n f\"--max-interval={max_interval}\",\n ]\n if scheduler:\n command.extend([\"--scheduler\", scheduler])\n if pidfile:\n command.extend([\"--pidfile\", pidfile])\n subprocess.run(command, check=True)\n\n\nif __name__ == '__main__':\n main()\n",
7
7
  "runcworker.py": "\"\"\"\n@author axiner\n@version v1.0.0\n@created 2025/09/20 10:10\n@abstract runcworker\uff08\u66f4\u591a\u53c2\u6570\u8bf7\u81ea\u884c\u6307\u5b9a\uff09\n@description\n@history\n\"\"\"\nimport argparse\nimport platform\nimport subprocess\nfrom os import cpu_count\n\n\ndef main(\n name: str, # `app_celery/consumer/workers`\u4e0b\u7684\u6a21\u5757\u540d\n loglevel: str = \"info\",\n concurrency: int = None,\n pool: str = None,\n):\n parser = argparse.ArgumentParser(description=\"CeleryWorker\u542f\u52a8\u5668\")\n parser.add_argument(\"-n\", \"--name\", type=str, metavar=\"\", help=\"\u540d\u79f0\")\n parser.add_argument(\"-l\", \"--loglevel\", type=str, default=\"info\", metavar=\"\", help=\"\u65e5\u5fd7\u7b49\u7ea7\")\n parser.add_argument(\"-c\", \"--concurrency\", type=int, default=None, metavar=\"\", help=\"\u5e76\u53d1\u6570\")\n parser.add_argument(\"-P\", \"--pool\", type=str, default=None, metavar=\"\", help=\"\u5e76\u53d1\u6a21\u578b\")\n args = parser.parse_args()\n name = args.name or name\n loglevel = args.loglevel or loglevel\n concurrency = args.concurrency or concurrency\n pool = args.pool or pool\n if pool is None:\n if platform.system().lower().startswith(\"win\"):\n pool = 'gevent'\n if not concurrency:\n concurrency = 100\n else:\n pool = 'prefork'\n if not concurrency:\n concurrency = cpu_count()\n command = [\n \"celery\",\n \"-A\",\n f\"app_celery.consumer.workers.{name}\",\n \"worker\",\n f\"--loglevel={loglevel}\",\n f\"--concurrency={concurrency}\",\n f\"--pool={pool}\",\n ]\n subprocess.run(\n command,\n check=True,\n )\n\n\nif __name__ == '__main__':\n main(\n name=\"ping\",\n )\n",
8
- "runserver.py": "\"\"\"\n@author axiner\n@version v1.0.0\n@created 2024/07/29 22:22\n@abstract runserver\uff08\u66f4\u591a\u53c2\u6570\u8bf7\u81ea\u884c\u6307\u5b9a\uff09\n@description\n@history\n\"\"\"\nimport argparse\nimport subprocess\nimport sys\n\nimport uvicorn\n\n\ndef run_by_unicorn(\n host: str,\n port: int,\n workers: int,\n log_level: str,\n is_reload: bool,\n):\n log_config = {\n \"version\": 1,\n \"disable_existing_loggers\": False,\n \"formatters\": {\n \"default\": {\n \"()\": \"uvicorn.logging.DefaultFormatter\",\n \"fmt\": \"%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s\",\n \"use_colors\": None\n },\n \"access\": {\n \"()\": \"uvicorn.logging.AccessFormatter\",\n \"fmt\": \"%(asctime)s %(levelname)s %(client_addr)s - \\\"%(request_line)s\\\" %(status_code)s\"\n }\n },\n \"handlers\": {\n \"default\": {\n \"formatter\": \"default\",\n \"class\": \"logging.StreamHandler\",\n \"stream\": \"ext://sys.stderr\"\n },\n \"access\": {\n \"formatter\": \"access\",\n \"class\": \"logging.StreamHandler\",\n \"stream\": \"ext://sys.stdout\"\n }\n },\n \"loggers\": {\n \"uvicorn\": {\n \"handlers\": [\n \"default\"\n ],\n \"level\": \"INFO\",\n \"propagate\": False\n },\n \"uvicorn.error\": {\n \"level\": \"INFO\"\n },\n \"uvicorn.access\": {\n \"handlers\": [\n \"access\"\n ],\n \"level\": \"INFO\",\n \"propagate\": False\n }\n }\n }\n uvicorn.run(\n app=\"app.main:app\",\n host=host,\n port=port,\n workers=workers,\n log_level=log_level,\n log_config=log_config,\n reload=is_reload,\n )\n\n\ndef run_by_gunicorn(\n host: str,\n port: int,\n workers: int,\n log_level: str,\n is_reload: bool,\n):\n cmd = (\n \"gunicorn app.main:app \"\n \"--worker-class=uvicorn.workers.UvicornWorker \"\n \"--bind={host}:{port} \"\n \"--workers={workers} \"\n \"--log-level={log_level} \"\n \"--access-logfile=- \"\n \"--error-logfile=- \"\n .format(\n host=host,\n port=port,\n workers=workers,\n log_level=log_level,\n )\n )\n if is_reload:\n cmd += f\" --reload\"\n subprocess.run(cmd, shell=True)\n\n\ndef main(\n host: str,\n port: int,\n workers: int,\n log_level: str,\n is_reload: bool,\n is_gunicorn: bool,\n):\n parser = argparse.ArgumentParser(description=\"App\u542f\u52a8\u5668\")\n parser.add_argument(\"--host\", type=str, metavar=\"\", help=\"host\")\n parser.add_argument(\"--port\", type=int, metavar=\"\", help=\"port\")\n parser.add_argument(\"--workers\", type=int, metavar=\"\", help=\"\u8fdb\u7a0b\u6570\")\n parser.add_argument(\"--log-level\", type=str, metavar=\"\", help=\"\u65e5\u5fd7\u7b49\u7ea7\")\n parser.add_argument(\"--is-reload\", action=\"store_true\", help=\"\u662f\u5426reload\")\n parser.add_argument(\"--is-gunicorn\", action=\"store_true\", help=\"\u662f\u5426gunicorn\")\n args = parser.parse_args()\n kwargs = {\n \"host\": args.host or host,\n \"port\": args.port or port,\n \"workers\": args.workers or workers,\n \"log_level\": args.log_level or log_level,\n \"is_reload\": args.is_reload or is_reload,\n }\n if (args.is_gunicorn or is_gunicorn) and not sys.platform.lower().startswith(\"win\"):\n try:\n import gunicorn # noqa\n except ImportError:\n sys.stderr.write(\"gunicorn\u672a\u627e\u5230\uff0c\u6b63\u5728\u5c1d\u8bd5\u81ea\u52a8\u5b89\u88c5...\\n\")\n try:\n subprocess.run(\n [\"pip\", \"install\", \"gunicorn\"],\n check=True,\n stdout=subprocess.PIPE,\n stderr=subprocess.PIPE)\n sys.stderr.write(\"gunicorn\u5b89\u88c5\u6210\u529f\\n\")\n except subprocess.CalledProcessError as e:\n sys.stderr.write(f\"gunicorn\u5b89\u88c5\u5931\u8d25: {e.stderr.decode().strip()}\\n\")\n raise\n run_by_gunicorn(**kwargs)\n else:\n run_by_unicorn(**kwargs)\n\n\nif __name__ == '__main__':\n main(\n host=\"0.0.0.0\",\n port=8000,\n workers=3,\n log_level=\"debug\",\n is_reload=False, # \u9002\u7528\u4e8edev\n is_gunicorn=False, # \u4e0d\u652f\u6301win\n )\n",
8
+ "runserver.py": "\"\"\"\n@author axiner\n@version v1.0.0\n@created 2024/07/29 22:22\n@abstract runserver\uff08\u66f4\u591a\u53c2\u6570\u8bf7\u81ea\u884c\u6307\u5b9a\uff09\n@description\n@history\n\"\"\"\nimport argparse\nimport subprocess\nimport sys\n\nimport uvicorn\n\n\ndef run_by_unicorn(\n host: str,\n port: int,\n workers: int,\n log_level: str,\n is_reload: bool,\n):\n log_config = {\n \"version\": 1,\n \"disable_existing_loggers\": False,\n \"formatters\": {\n \"default\": {\n \"()\": \"uvicorn.logging.DefaultFormatter\",\n \"fmt\": \"%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s\",\n \"use_colors\": None\n },\n \"access\": {\n \"()\": \"uvicorn.logging.AccessFormatter\",\n \"fmt\": \"%(asctime)s %(levelname)s %(client_addr)s - \\\"%(request_line)s\\\" %(status_code)s\"\n }\n },\n \"handlers\": {\n \"default\": {\n \"formatter\": \"default\",\n \"class\": \"logging.StreamHandler\",\n \"stream\": \"ext://sys.stderr\"\n },\n \"access\": {\n \"formatter\": \"access\",\n \"class\": \"logging.StreamHandler\",\n \"stream\": \"ext://sys.stdout\"\n }\n },\n \"loggers\": {\n \"uvicorn\": {\n \"handlers\": [\n \"default\"\n ],\n \"level\": \"INFO\",\n \"propagate\": False\n },\n \"uvicorn.error\": {\n \"level\": \"INFO\"\n },\n \"uvicorn.access\": {\n \"handlers\": [\n \"access\"\n ],\n \"level\": \"INFO\",\n \"propagate\": False\n }\n }\n }\n uvicorn.run(\n app=\"app.main:app\",\n host=host,\n port=port,\n workers=workers,\n log_level=log_level,\n log_config=log_config,\n reload=is_reload,\n )\n\n\ndef run_by_gunicorn(\n host: str,\n port: int,\n workers: int,\n log_level: str,\n is_reload: bool,\n):\n cmd = (\n \"gunicorn app.main:app \"\n \"--worker-class=uvicorn.workers.UvicornWorker \"\n \"--bind={host}:{port} \"\n \"--workers={workers} \"\n \"--log-level={log_level} \"\n \"--access-logfile=- \"\n \"--error-logfile=- \"\n .format(\n host=host,\n port=port,\n workers=workers,\n log_level=log_level,\n )\n )\n if is_reload:\n cmd += f\" --reload\"\n subprocess.run(cmd, shell=True)\n\n\ndef main(\n host: str,\n port: int,\n workers: int,\n log_level: str,\n is_reload: bool,\n is_gunicorn: bool,\n):\n parser = argparse.ArgumentParser(description=\"App\u542f\u52a8\u5668\")\n parser.add_argument(\"--host\", type=str, metavar=\"\", help=\"host\")\n parser.add_argument(\"--port\", type=int, metavar=\"\", help=\"port\")\n parser.add_argument(\"--workers\", type=int, metavar=\"\", help=\"\u8fdb\u7a0b\u6570\")\n parser.add_argument(\"--log-level\", type=str, metavar=\"\", help=\"\u65e5\u5fd7\u7b49\u7ea7\")\n parser.add_argument(\"--is-reload\", action=\"store_true\", help=\"\u662f\u5426reload\")\n parser.add_argument(\"--is-gunicorn\", action=\"store_true\", help=\"\u662f\u5426gunicorn\")\n args = parser.parse_args()\n kwargs = {\n \"host\": args.host or host,\n \"port\": args.port or port,\n \"workers\": args.workers or workers,\n \"log_level\": args.log_level or log_level,\n \"is_reload\": args.is_reload or is_reload,\n }\n if (args.is_gunicorn or is_gunicorn) and not sys.platform.lower().startswith(\"win\"):\n try:\n import gunicorn # noqa\n except ImportError:\n sys.stderr.write(\"gunicorn\u672a\u627e\u5230\uff0c\u6b63\u5728\u5c1d\u8bd5\u81ea\u52a8\u5b89\u88c5...\\n\")\n try:\n subprocess.run(\n [\"pip\", \"install\", \"gunicorn\"],\n check=True,\n stdout=subprocess.PIPE,\n stderr=subprocess.PIPE)\n sys.stderr.write(\"gunicorn\u5b89\u88c5\u6210\u529f\\n\")\n except subprocess.CalledProcessError as e:\n sys.stderr.write(f\"gunicorn\u5b89\u88c5\u5931\u8d25: {e.stderr.decode().strip()}\\n\")\n raise\n run_by_gunicorn(**kwargs)\n else:\n run_by_unicorn(**kwargs)\n\n\nif __name__ == '__main__':\n main(\n host=\"0.0.0.0\",\n port=8000,\n workers=3,\n log_level=\"debug\",\n is_reload=False, # For development environment\n is_gunicorn=False, # Not supported on Windows\n )\n",
9
9
  "app/main.py": "\"\"\"\n@author axiner\n@version v1.0.0\n@created 2024/07/29 22:22\n@abstract main\n@description\n@history\n\"\"\"\nfrom contextlib import asynccontextmanager\nfrom fastapi import FastAPI\nfrom fastapi.responses import ORJSONResponse\n\nfrom app import (\n api,\n middleware,\n)\nfrom app.initializer import g\n\ng.setup()\n# #\nopenapi_url = \"/openapi.json\"\ndocs_url = \"/docs\"\nredoc_url = \"/redoc\"\nif g.config.app_disable_docs is True:\n openapi_url, docs_url, redoc_url = None, None, None\n\n\n@asynccontextmanager\nasync def lifespan(app_: FastAPI):\n g.logger.info(f\"Application env '{g.config.app_env}'\")\n g.logger.info(f\"Application yaml '{g.config.app_yaml}'\")\n g.logger.info(f\"Application title '{g.config.app_title}'\")\n g.logger.info(f\"Application version '{g.config.app_version}'\")\n # #\n g.logger.info(\"Application server running\")\n yield\n g.logger.info(\"Application server shutdown\")\n\n\napp = FastAPI(\n title=g.config.app_title,\n summary=g.config.app_summary,\n description=g.config.app_description,\n version=g.config.app_version,\n debug=g.config.app_debug,\n openapi_url=openapi_url,\n docs_url=docs_url,\n redoc_url=redoc_url,\n lifespan=lifespan,\n default_response_class=ORJSONResponse,\n)\n# #\napi.register_routers(app)\nmiddleware.register_middlewares(app)\n",
10
10
  "app/__init__.py": "\"\"\"\n@author axiner\n@version v1.0.0\n@created 2024/07/29 22:22\n@abstract app\n@description\n@history\n\"\"\"\nfrom pathlib import Path\n\nAPP_DIR = Path(__file__).absolute().parent\n",
11
11
  "app/api/dependencies.py": "from fastapi import Depends, Security\nfrom fastapi.security import HTTPBearer, HTTPAuthorizationCredentials, APIKeyHeader\nfrom fastapi.security.utils import get_authorization_scheme_param\nfrom pydantic import BaseModel\nfrom starlette.requests import Request\n\nfrom app.api.exceptions import CustomException\nfrom app.api.status import Status\nfrom app.initializer import g\nfrom app.utils.db_async_util import sqlfetch_one\nfrom app.utils.jwt_util import verify_jwt\n\n\n# ======= jwt =======\n\nclass JWTUser(BaseModel):\n # \u4e0e\u5b9e\u9645`user`\u5bf9\u9f50\n id: str = None\n phone: str = None\n name: str = None\n age: int = None\n gender: int = None\n\n @staticmethod\n async def get_user_jwt_key(user_id: str) -> str:\n # \u5efa\u8bae\uff1ajwt_key\u8fdb\u884credis\u7f13\u5b58\n async with g.db_async_session() as session:\n sql = 'SELECT jwt_key FROM `user` WHERE id = :id' # noqa\n if session.bind.dialect.name == \"postgresql\":\n sql = 'SELECT jwt_key FROM \"user\" WHERE id = :id' # noqa\n data = await sqlfetch_one(\n session=session,\n sql=sql,\n params={\"id\": user_id},\n )\n return data.get(\"jwt_key\")\n\n\nclass JWTAuthorizationCredentials(HTTPAuthorizationCredentials):\n jwt_user: JWTUser\n\n\nclass JWTBearer(HTTPBearer):\n\n async def __call__(\n self, request: Request\n ) -> JWTAuthorizationCredentials | None:\n authorization = request.headers.get(\"Authorization\")\n scheme, credentials = get_authorization_scheme_param(authorization)\n if not (authorization and scheme and credentials):\n if self.auto_error:\n raise CustomException(\n msg=\"Not authenticated\",\n status=Status.UNAUTHORIZED_ERROR,\n )\n else:\n return None\n if scheme.lower() != \"bearer\":\n if self.auto_error:\n raise CustomException(\n msg=\"Invalid authentication credentials\",\n status=Status.UNAUTHORIZED_ERROR,\n )\n else:\n return None\n jwt_user = await self.verify_credentials(credentials)\n return JWTAuthorizationCredentials(scheme=scheme, credentials=credentials, jwt_user=jwt_user)\n\n async def verify_credentials(self, credentials: str) -> JWTUser:\n playload = await self._verify_jwt(credentials)\n if playload is None:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR)\n user_jwt_key = await JWTUser.get_user_jwt_key(playload.get(\"id\"))\n if not user_jwt_key:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR)\n await self._verify_jwt(credentials, jwt_key=user_jwt_key)\n return JWTUser(\n id=playload.get(\"id\"),\n phone=playload.get(\"phone\"),\n name=playload.get(\"name\"),\n age=playload.get(\"age\"),\n gender=playload.get(\"gender\"),\n )\n\n @staticmethod\n async def _verify_jwt(token: str, jwt_key: str = None) -> dict:\n try:\n return verify_jwt(token=token, jwt_key=jwt_key)\n except Exception as e:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR, msg=str(e))\n\n\ndef get_current_user(\n credentials: JWTAuthorizationCredentials | None = Depends(JWTBearer(auto_error=True))\n) -> JWTUser:\n if not credentials:\n return JWTUser()\n return credentials.jwt_user\n\n\n# ======= api key =======\n\n_API_KEY_HEADER = APIKeyHeader(name=\"X-API-Key\", auto_error=False)\n\n\nclass ApiKeyUser(BaseModel):\n\n @staticmethod\n def get_user_api_key(user_id: str = None) -> list:\n if user_id:\n return g.config.api_keys.get(user_id)\n return g.config.api_keys\n\n\nasync def get_current_api_key(api_key: str | None = Security(_API_KEY_HEADER)):\n if not api_key:\n raise CustomException(status=Status.FORBIDDEN_ERROR)\n user_api_key = ApiKeyUser.get_user_api_key()\n if not user_api_key:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR)\n if api_key not in user_api_key:\n raise CustomException(status=Status.UNAUTHORIZED_ERROR)\n return api_key\n",
@@ -25,8 +25,8 @@
25
25
  "app/initializer/_redis.py": "from toollib.rediser import RedisClient\n\n\ndef init_redis_client(\n host: str,\n port: int,\n db: int,\n password: str = None,\n max_connections: int = None,\n **kwargs,\n) -> RedisClient:\n if not host:\n return RedisClient()\n return RedisClient(\n host=host,\n port=port,\n db=db,\n password=password,\n max_connections=max_connections,\n **kwargs,\n )\n",
26
26
  "app/initializer/_snow.py": "import os\n\nfrom loguru import logger\nfrom toollib.guid import SnowFlake\nfrom toollib.rediser import RedisClient\nfrom toollib.utils import localip\n\n_CACHE_KEY_SNOW_WORKER_ID_INCR = \"config:snow_worker_id_incr\"\n_CACHE_KEY_SNOW_DATACENTER_ID_INCR = \"config:snow_datacenter_id_incr\"\n_CACHE_EXPIRE_SNOW = 120\n\n\ndef init_snow_client(\n redis_client: RedisClient,\n datacenter_id: int = None,\n to_str: bool = True,\n) -> SnowFlake: # \u5efa\u8bae\uff1a\u91c7\u7528\u670d\u52a1\u7684\u65b9\u5f0f\u8c03\u7528api\u83b7\u53d6\n if datacenter_id is None:\n datacenter_id = _snow_incr(redis_client, _CACHE_KEY_SNOW_DATACENTER_ID_INCR, _CACHE_EXPIRE_SNOW)\n if datacenter_id is None:\n local_ip = localip()\n if local_ip:\n ip_parts = list(map(int, local_ip.split('.')))\n ip_int = (ip_parts[0] << 24) + (ip_parts[1] << 16) + (ip_parts[2] << 8) + ip_parts[3]\n datacenter_id = ip_int % 32\n worker_id = _snow_incr(redis_client, _CACHE_KEY_SNOW_WORKER_ID_INCR, _CACHE_EXPIRE_SNOW)\n if worker_id is None:\n worker_id = os.getpid() % 32\n return SnowFlake(worker_id=worker_id, datacenter_id=datacenter_id, to_str=to_str)\n\n\ndef _snow_incr(redis_client, cache_key: str, cache_expire: int):\n incr = None\n try:\n with redis_client.connection() as r:\n resp = r.ping()\n if resp:\n lua_script = \"\"\"\n if redis.call('exists', KEYS[1]) == 1 then\n redis.call('expire', KEYS[1], ARGV[1])\n return redis.call('incr', KEYS[1])\n else\n redis.call('set', KEYS[1], 0)\n redis.call('expire', KEYS[1], ARGV[1])\n return 0\n end\n \"\"\"\n incr = r.eval(lua_script, 1, cache_key, cache_expire)\n except Exception as e:\n logger.warning(f\"snow\u521d\u59cb\u5316id\u5c06\u91c7\u7528\u672c\u5730\u65b9\u5f0f\uff0c\u7531\u4e8e\uff08{e}\uff09\")\n return incr\n",
27
27
  "app/initializer/__init__.py": "\"\"\"\n\u521d\u59cb\u5316\n\"\"\"\nimport threading\nfrom functools import cached_property\n\nfrom loguru import logger\nfrom loguru._logger import Logger # noqa\nfrom sqlalchemy.orm import sessionmaker, scoped_session\nfrom toollib.guid import SnowFlake\nfrom toollib.rediser import RedisClient\nfrom toollib.utils import Singleton\n\nfrom app.initializer._conf import Config, init_config\nfrom app.initializer._db import init_db_session, init_db_async_session\nfrom app.initializer._log import init_logger\nfrom app.initializer._redis import init_redis_client\nfrom app.initializer._snow import init_snow_client\n\n\nclass G(metaclass=Singleton):\n \"\"\"\n \u5168\u5c40\u53d8\u91cf\n \"\"\"\n _initialized = False\n _init_lock = threading.Lock()\n _init_properties = [\n 'config',\n 'logger',\n 'redis_client',\n 'snow_client',\n # 'db_session',\n 'db_async_session',\n ]\n\n def __init__(self):\n self._initialized = False\n\n @cached_property\n def config(self) -> Config:\n return init_config()\n\n @cached_property\n def logger(self) -> Logger:\n return init_logger(\n debug=self.config.app_debug,\n log_dir=self.config.app_log_dir,\n serialize=self.config.app_log_serialize,\n intercept_standard=self.config.app_log_intercept_standard,\n )\n\n @cached_property\n def redis_client(self) -> RedisClient:\n return init_redis_client(\n host=self.config.redis_host,\n port=self.config.redis_port,\n db=self.config.redis_db,\n password=self.config.redis_password,\n max_connections=self.config.redis_max_connections,\n )\n\n @cached_property\n def snow_client(self) -> SnowFlake:\n return init_snow_client(\n redis_client=self.redis_client,\n datacenter_id=self.config.snow_datacenter_id,\n )\n\n @cached_property\n def db_session(self) -> scoped_session:\n return init_db_session(\n db_url=self.config.db_url,\n db_echo=self.config.app_debug,\n is_create_tables=True,\n )\n\n @cached_property\n def db_async_session(self) -> sessionmaker:\n return init_db_async_session(\n db_url=self.config.db_async_url,\n db_echo=self.config.app_debug,\n is_create_tables=True,\n )\n\n def setup(self):\n with self._init_lock:\n if not self._initialized:\n for prop_name in self._init_properties:\n if hasattr(self, prop_name):\n getattr(self, prop_name)\n else:\n logger.warning(f\"{prop_name} not found\")\n self._initialized = True\n\n\ng = G()\n",
28
- "app/middleware/cors.py": "from fastapi.middleware.cors import CORSMiddleware\n\nfrom app.initializer import g\n\n\nclass Cors:\n middleware_class = CORSMiddleware\n allow_origins = g.config.app_allow_origins\n allow_credentials = True\n allow_methods = [\"*\"]\n allow_headers = [\"*\"]\n",
29
- "app/middleware/exceptions.py": "import traceback\n\nfrom fastapi.exceptions import RequestValidationError\nfrom starlette.exceptions import HTTPException\nfrom starlette.requests import Request\nfrom starlette.responses import JSONResponse\n\nfrom app.api.exceptions import CustomException\nfrom app.api.responses import Responses\nfrom app.api.status import Status\nfrom app.initializer import g\n\n\nclass ExceptionsHandler:\n\n @staticmethod\n async def custom_exception_handler(\n request: Request,\n exc: CustomException,\n is_traceback: bool = False,\n ) -> JSONResponse:\n lmsg = f'- \"{request.method} {request.url.path}\" {exc.code} {exc.msg}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=exc.msg,\n code=exc.code,\n data=exc.data,\n request=request,\n )\n\n @staticmethod\n async def request_validation_handler(\n request: Request,\n exc: RequestValidationError,\n is_display_all: bool = False,\n is_traceback: bool = False,\n ) -> JSONResponse:\n if is_display_all:\n msg = \", \".join([f\"'{item['loc'][1] if len(item['loc']) > 1 else item['loc'][0]}' {item['msg'].lower()}\" for item in exc.errors()]) # noqa: E501\n else:\n _first_error = exc.errors()[0]\n msg = f\"'{_first_error['loc'][1] if len(_first_error['loc']) > 1 else _first_error['loc'][0]}' {_first_error['msg'].lower()}\" # noqa: E501\n lmsg = f'- \"{request.method} {request.url.path}\" {Status.PARAMS_ERROR.code} {msg}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=msg,\n status=Status.PARAMS_ERROR,\n request=request,\n )\n\n @staticmethod\n async def http_exception_handler(\n request: Request,\n exc: HTTPException,\n is_traceback: bool = False,\n ) -> JSONResponse:\n lmsg = f'- \"{request.method} {request.url.path}\" {exc.status_code} {exc.detail}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=exc.detail,\n code=exc.status_code,\n request=request,\n )\n\n @staticmethod\n async def exception_handler(\n request: Request,\n exc: Exception,\n is_traceback: bool = True,\n ) -> JSONResponse:\n lmsg = f'- \"{request.method} {request.url.path}\" 500 {type(exc).__name__}: {exc}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=\"Internal system error, please try again later.\",\n code=500,\n request=request,\n )\n",
28
+ "app/middleware/cors.py": "from starlette.middleware.cors import CORSMiddleware\n\nfrom app.initializer import g\n\n\nclass Cors:\n middleware_class = CORSMiddleware\n allow_origins = g.config.app_allow_origins\n allow_credentials = True\n allow_methods = [\"*\"]\n allow_headers = [\"*\"]\n",
29
+ "app/middleware/exceptions.py": "import traceback\n\nfrom fastapi.exceptions import RequestValidationError\nfrom starlette.exceptions import HTTPException\nfrom starlette.requests import Request\nfrom starlette.responses import JSONResponse\n\nfrom app.api.exceptions import CustomException\nfrom app.api.responses import Responses\nfrom app.api.status import Status\nfrom app.initializer import g\n\n\nclass ExceptionsHandler:\n\n @staticmethod\n async def custom_exception_handler(\n request: Request,\n exc: CustomException,\n is_traceback: bool = False,\n ) -> JSONResponse:\n lmsg = f'- \"{request.method} {request.url.path}\" {exc.code} {exc.msg}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=exc.msg,\n code=exc.code,\n data=exc.data,\n request=request,\n )\n\n @staticmethod\n async def request_validation_handler(\n request: Request,\n exc: RequestValidationError,\n is_display_all: bool = False,\n is_traceback: bool = False,\n ) -> JSONResponse:\n if is_display_all:\n msg = \", \".join([f\"'{item['loc'][1] if len(item['loc']) > 1 else item['loc'][0]}' {item['msg'].lower()}\"\n for item in exc.errors()])\n else:\n _first_error = exc.errors()[0]\n msg = f\"'{_first_error['loc'][1] if len(_first_error['loc']) > 1 else _first_error['loc'][0]}' {_first_error['msg'].lower()}\" # noqa: E501\n lmsg = f'- \"{request.method} {request.url.path}\" {Status.PARAMS_ERROR.code} {msg}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=msg,\n status=Status.PARAMS_ERROR,\n request=request,\n )\n\n @staticmethod\n async def http_exception_handler(\n request: Request,\n exc: HTTPException,\n is_traceback: bool = False,\n ) -> JSONResponse:\n lmsg = f'- \"{request.method} {request.url.path}\" {exc.status_code} {exc.detail}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=exc.detail,\n code=exc.status_code,\n request=request,\n )\n\n @staticmethod\n async def exception_handler(\n request: Request,\n exc: Exception,\n is_traceback: bool = True,\n ) -> JSONResponse:\n lmsg = f'- \"{request.method} {request.url.path}\" 500 {type(exc).__name__}: {exc}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=\"Internal system error, please try again later.\",\n code=500,\n request=request,\n )\n",
30
30
  "app/middleware/headers.py": "import uuid\nfrom starlette.middleware.base import BaseHTTPMiddleware\nfrom starlette.requests import Request\n\nfrom app.initializer.context import request_id_ctx_var\n\n\nclass HeadersMiddleware(BaseHTTPMiddleware):\n \"\"\"\u5934\u5904\u7406\u4e2d\u95f4\u4ef6\"\"\"\n _HEADERS = {\n # \u53ef\u6dfb\u52a0\u76f8\u5173\u5934\n }\n\n async def dispatch(self, request: Request, call_next):\n request_id = self._get_or_create_request_id(request)\n request.state.request_id = request_id\n ctx_token = request_id_ctx_var.set(request_id)\n try:\n response = await call_next(request)\n response.headers[\"X-Request-ID\"] = request_id\n for key, value in self._HEADERS.items():\n if key not in response.headers:\n response.headers[key] = value\n return response\n finally:\n request_id_ctx_var.reset(ctx_token)\n\n @staticmethod\n def _get_or_create_request_id(request: Request) -> str:\n request_id = request.headers.get(\"X-Request-ID\")\n if not request_id:\n request_id = f\"req-{uuid.uuid4()}\"\n return request_id\n",
31
31
  "app/middleware/__init__.py": "\"\"\"\n\u4e2d\u95f4\u4ef6\n\"\"\"\nfrom fastapi import FastAPI\nfrom fastapi.exceptions import RequestValidationError\nfrom starlette.exceptions import HTTPException\n\nfrom app.api.exceptions import CustomException\nfrom app.middleware.cors import Cors\nfrom app.middleware.exceptions import ExceptionsHandler\nfrom app.middleware.headers import HeadersMiddleware\n\n\ndef register_middlewares(app: FastAPI):\n \"\"\"\u6ce8\u518c\u4e2d\u95f4\u4ef6\"\"\"\n app.add_middleware(\n middleware_class=Cors.middleware_class,\n allow_origins=Cors.allow_origins,\n allow_credentials=Cors.allow_credentials,\n allow_methods=Cors.allow_methods,\n allow_headers=Cors.allow_headers,\n )\n app.add_middleware(HeadersMiddleware) # type: ignore\n app.add_exception_handler(CustomException, ExceptionsHandler.custom_exception_handler) # type: ignore\n app.add_exception_handler(RequestValidationError, ExceptionsHandler.request_validation_handler) # type: ignore\n app.add_exception_handler(HTTPException, ExceptionsHandler.http_exception_handler) # type: ignore\n app.add_exception_handler(Exception, ExceptionsHandler.exception_handler)\n",
32
32
  "app/models/user.py": "from sqlalchemy import Column, BigInteger, Integer, String\nfrom toollib.utils import now2timestamp\n\nfrom app.initializer import g\nfrom app.models import DeclBase\n\n\nclass User(DeclBase):\n __tablename__ = \"user\"\n\n id = Column(String(20), primary_key=True, default=g.snow_client.gen_uid, comment=\"\u4e3b\u952e\")\n phone = Column(String(15), unique=True, index=True, nullable=False, comment=\"\u624b\u673a\u53f7\")\n password = Column(String(128), nullable=True, comment=\"\u5bc6\u7801\")\n jwt_key = Column(String(128), nullable=True, comment=\"jwtKey\")\n name = Column(String(50), nullable=True, comment=\"\u540d\u79f0\")\n age = Column(Integer, nullable=True, comment=\"\u5e74\u9f84\")\n gender = Column(Integer, nullable=True, comment=\"\u6027\u522b\")\n created_at = Column(BigInteger, default=now2timestamp, comment=\"\u521b\u5efa\u65f6\u95f4\")\n updated_at = Column(BigInteger, default=now2timestamp, onupdate=now2timestamp, comment=\"\u66f4\u65b0\u65f6\u95f4\")\n",
@@ -61,5 +61,7 @@
61
61
  "deploy/.gitkeep": "",
62
62
  "docs/.gitkeep": "",
63
63
  "logs/.gitkeep": "",
64
- "tests/__init__.py": "\"\"\"\n\u6d4b\u8bd5\n\"\"\"\n"
64
+ "tests/__init__.py": "\"\"\"\n\u6d4b\u8bd5\n\"\"\"\n",
65
+ "app/initializer.py": "\"\"\"\n\u521d\u59cb\u5316\n\"\"\"\nimport logging\nimport os\nimport sys\nimport threading\nfrom contextvars import ContextVar\nfrom functools import cached_property\nfrom pathlib import Path\n\nimport yaml\nfrom dotenv import load_dotenv\nfrom loguru import logger\nfrom loguru._logger import Logger # noqa\nfrom sqlalchemy import create_engine\nfrom sqlalchemy.ext.asyncio import create_async_engine, AsyncSession\nfrom sqlalchemy.orm import scoped_session, sessionmaker\nfrom toollib.utils import Singleton, get_cls_attrs, parse_variable\n\nfrom app import APP_DIR\n\n__all__ = [\n \"g\",\n \"request_id_ctx_var\",\n]\n\n_CONFIG_DIR = APP_DIR.parent.joinpath(\"config\")\n\nload_dotenv(dotenv_path=os.environ.setdefault(\n key=\"env_path\",\n value=str(_CONFIG_DIR.joinpath(\".env\")))\n)\n# #\napp_yaml = Path(\n os.environ.get(\"app_yaml\") or\n _CONFIG_DIR.joinpath(f\"app_{os.environ.setdefault(key='app_env', value='dev')}.yaml\")\n)\nif not app_yaml.is_file():\n raise RuntimeError(f\"\u914d\u7f6e\u6587\u4ef6\u4e0d\u5b58\u5728\uff1a{app_yaml}\")\n\nrequest_id_ctx_var: ContextVar[str] = ContextVar(\"request_id\", default=\"N/A\")\n\n\nclass Config:\n \"\"\"\u914d\u7f6e\"\"\"\n _yaml_conf: dict = None\n # from env\n app_env: str = \"dev\"\n app_yaml: str = app_yaml.name\n api_keys: list = []\n snow_datacenter_id: int = None\n # from yaml\n app_title: str = \"xApp\"\n app_summary: str = \"xxApp\"\n app_description: str = \"xxxApp\"\n app_version: str = \"1.0.0\"\n app_debug: bool = True\n app_log_dir: str = \"./logs\"\n app_log_serialize: bool = False\n app_log_intercept_standard: bool = False\n app_disable_docs: bool = True\n app_allow_origins: list = [\"*\"]\n # #\n db_url: str = None\n db_async_url: str = None\n\n def setup(self):\n self.setattr_from_env_or_yaml()\n return self\n\n def setattr_from_env_or_yaml(self):\n cls_attrs = get_cls_attrs(Config)\n for k, item in cls_attrs.items():\n v_type, v = item\n if callable(v_type):\n if k in os.environ: # \u4f18\u5148\u73af\u5883\u53d8\u91cf\n v = parse_variable(k=k, v_type=v_type, v_from=os.environ, default=v)\n else:\n v = parse_variable(k=k, v_type=v_type, v_from=self.load_yaml(), default=v)\n setattr(self, k, v)\n\n def load_yaml(self, reload: bool = False) -> dict:\n if self._yaml_conf and not reload:\n return self._yaml_conf\n with open(app_yaml, mode=\"r\", encoding=\"utf-8\") as file:\n self._yaml_conf = yaml.load(file, Loader=yaml.FullLoader)\n return self._yaml_conf\n\n\n_LOG_TEXT_FORMAT = \"{time:YYYY-MM-DD HH:mm:ss.SSS} {level} {extra[request_id]} {file}:{line} {message}\"\n_LOG_FILE_PREFIX = \"app\"\n_LOG_ROTATION = \"100 MB\"\n_LOG_RETENTION = \"15 days\"\n_LOG_COMPRESSION = None\n_LOG_ENQUEUE = True\n_LOG_BACKTRACE = False\n_LOG_DIAGNOSE = False\n_LOG_CATCH = False\n_LOG_PID = False\n\n\nclass InterceptHandler(logging.Handler):\n def emit(self, record: logging.LogRecord):\n try:\n level = logger.level(record.levelname).name\n except ValueError:\n level = record.levelno\n frame, depth = logging.currentframe(), 2\n while frame.f_code.co_filename == logging.__file__:\n frame = frame.f_back\n depth += 1\n logger.opt(depth=depth, exception=record.exc_info).log(\n level, record.getMessage()\n )\n\n\ndef init_logger(\n debug: bool,\n log_dir: str = None,\n serialize: bool = False,\n intercept_standard: bool = False,\n) -> Logger:\n logger.remove(None)\n _lever = \"DEBUG\" if debug else \"INFO\"\n if intercept_standard:\n logging.basicConfig(handlers=[InterceptHandler()], level=_lever)\n\n def _filter(record: dict) -> bool:\n record[\"extra\"][\"request_id\"] = request_id_ctx_var.get()\n return True\n\n logger.add(\n sys.stdout,\n format=_LOG_TEXT_FORMAT,\n serialize=serialize,\n level=_lever,\n enqueue=_LOG_ENQUEUE,\n backtrace=_LOG_BACKTRACE,\n diagnose=_LOG_DIAGNOSE,\n catch=_LOG_CATCH,\n filter=_filter,\n )\n if log_dir:\n _log_dir = Path(log_dir)\n _log_access_file = _log_dir.joinpath(f\"{_LOG_FILE_PREFIX}-access.log\")\n _log_error_file = _log_dir.joinpath(f\"{_LOG_FILE_PREFIX}-error.log\")\n if _LOG_PID:\n _log_access_file = str(_log_access_file).replace(\".log\", f\".{os.getpid()}.log\")\n _log_error_file = str(_log_error_file).replace(\".log\", f\".{os.getpid()}.log\")\n logger.add(\n _log_access_file,\n encoding=\"utf-8\",\n format=_LOG_TEXT_FORMAT,\n serialize=serialize,\n level=_lever,\n rotation=_LOG_ROTATION,\n retention=_LOG_RETENTION,\n compression=_LOG_COMPRESSION,\n enqueue=_LOG_ENQUEUE,\n backtrace=_LOG_BACKTRACE,\n diagnose=_LOG_DIAGNOSE,\n catch=_LOG_CATCH,\n filter=_filter,\n )\n logger.add(\n _log_error_file,\n encoding=\"utf-8\",\n format=_LOG_TEXT_FORMAT,\n serialize=serialize,\n level=\"ERROR\",\n rotation=_LOG_ROTATION,\n retention=_LOG_RETENTION,\n compression=_LOG_COMPRESSION,\n enqueue=_LOG_ENQUEUE,\n backtrace=_LOG_BACKTRACE,\n diagnose=_LOG_DIAGNOSE,\n catch=_LOG_CATCH,\n filter=_filter,\n )\n return logger\n\n\ndef init_db_session(\n db_url: str,\n db_echo: bool,\n db_pool_size: int = 10,\n db_max_overflow: int = 5,\n db_pool_recycle: int = 3600,\n) -> scoped_session:\n db_echo = db_echo or False\n kwargs = {\n \"pool_size\": db_pool_size,\n \"max_overflow\": db_max_overflow,\n \"pool_recycle\": db_pool_recycle,\n }\n if db_url.startswith(\"sqlite\"):\n kwargs = {}\n engine = create_engine(\n url=db_url,\n echo=db_echo,\n echo_pool=db_echo,\n **kwargs,\n )\n db_session = sessionmaker(engine, expire_on_commit=False)\n return scoped_session(db_session)\n\n\ndef init_db_async_session(\n db_url: str,\n db_echo: bool,\n db_pool_size: int = 10,\n db_max_overflow: int = 5,\n db_pool_recycle: int = 3600,\n) -> sessionmaker:\n db_echo = db_echo or False\n kwargs = {\n \"pool_size\": db_pool_size,\n \"max_overflow\": db_max_overflow,\n \"pool_recycle\": db_pool_recycle,\n }\n if db_url.startswith(\"sqlite\"):\n kwargs = {}\n async_engine = create_async_engine(\n url=db_url,\n echo=db_echo,\n echo_pool=db_echo,\n **kwargs,\n )\n db_async_session = sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False) # noqa\n return db_async_session\n\n\nclass G(metaclass=Singleton):\n \"\"\"\n \u5168\u5c40\u53d8\u91cf\n \"\"\"\n _initialized = False\n _init_lock = threading.Lock()\n _init_properties = [\n 'config',\n 'logger',\n # 'db_session',\n 'db_async_session',\n ]\n\n def __init__(self):\n self._initialized = False\n\n @cached_property\n def config(self) -> Config:\n return Config().setup()\n\n @cached_property\n def logger(self) -> Logger:\n return init_logger(\n debug=self.config.app_debug,\n log_dir=self.config.app_log_dir,\n serialize=self.config.app_log_serialize,\n intercept_standard=self.config.app_log_intercept_standard,\n )\n\n @cached_property\n def db_session(self) -> scoped_session:\n return init_db_session(\n db_url=self.config.db_url,\n db_echo=self.config.app_debug,\n )\n\n @cached_property\n def db_async_session(self) -> sessionmaker:\n return init_db_async_session(\n db_url=self.config.db_async_url,\n db_echo=self.config.app_debug,\n )\n\n def setup(self):\n with self._init_lock:\n if not self._initialized:\n for prop_name in self._init_properties:\n if hasattr(self, prop_name):\n getattr(self, prop_name)\n else:\n logger.warning(f\"{prop_name} not found\")\n self._initialized = True\n\n\ng = G()\n",
66
+ "app/middleware.py": "\"\"\"\n\u4e2d\u95f4\u4ef6\n\"\"\"\nimport traceback\nimport uuid\n\nfrom fastapi import FastAPI\nfrom fastapi.exceptions import RequestValidationError\nfrom starlette.exceptions import HTTPException\nfrom starlette.middleware.base import BaseHTTPMiddleware\nfrom starlette.middleware.cors import CORSMiddleware\nfrom starlette.requests import Request\nfrom starlette.responses import JSONResponse\n\nfrom app.api.exceptions import CustomException\nfrom app.api.responses import Responses\nfrom app.api.status import Status\nfrom app.initializer import g, request_id_ctx_var\n\n__all__ = [\n \"register_middlewares\",\n]\n\n\ndef register_middlewares(app: FastAPI):\n \"\"\"\u6ce8\u518c\u4e2d\u95f4\u4ef6\"\"\"\n app.add_middleware(\n middleware_class=CORSMiddleware, # type: ignore\n allow_origins=g.config.app_allow_origins,\n allow_credentials=True,\n allow_methods=[\"*\"],\n allow_headers=[\"*\"],\n )\n app.add_middleware(HeadersMiddleware) # type: ignore\n app.add_exception_handler(CustomException, ExceptionsHandler.custom_exception_handler) # type: ignore\n app.add_exception_handler(RequestValidationError, ExceptionsHandler.request_validation_handler) # type: ignore\n app.add_exception_handler(HTTPException, ExceptionsHandler.http_exception_handler) # type: ignore\n app.add_exception_handler(Exception, ExceptionsHandler.exception_handler)\n\n\nclass HeadersMiddleware(BaseHTTPMiddleware):\n \"\"\"\u5934\u5904\u7406\u4e2d\u95f4\u4ef6\"\"\"\n _HEADERS = {\n # \u53ef\u6dfb\u52a0\u76f8\u5173\u5934\n }\n\n async def dispatch(self, request: Request, call_next):\n request_id = self._get_or_create_request_id(request)\n request.state.request_id = request_id\n ctx_token = request_id_ctx_var.set(request_id)\n try:\n response = await call_next(request)\n response.headers[\"X-Request-ID\"] = request_id\n for key, value in self._HEADERS.items():\n if key not in response.headers:\n response.headers[key] = value\n return response\n finally:\n request_id_ctx_var.reset(ctx_token)\n\n @staticmethod\n def _get_or_create_request_id(request: Request) -> str:\n request_id = request.headers.get(\"X-Request-ID\")\n if not request_id:\n request_id = f\"req-{uuid.uuid4()}\"\n return request_id\n\n\nclass ExceptionsHandler:\n\n @staticmethod\n async def custom_exception_handler(\n request: Request,\n exc: CustomException,\n is_traceback: bool = False,\n ) -> JSONResponse:\n lmsg = f'- \"{request.method} {request.url.path}\" {exc.code} {exc.msg}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=exc.msg,\n code=exc.code,\n data=exc.data,\n request=request,\n )\n\n @staticmethod\n async def request_validation_handler(\n request: Request,\n exc: RequestValidationError,\n is_display_all: bool = False,\n is_traceback: bool = False,\n ) -> JSONResponse:\n if is_display_all:\n msg = \", \".join([f\"'{item['loc'][1] if len(item['loc']) > 1 else item['loc'][0]}' {item['msg'].lower()}\"\n for item in exc.errors()])\n else:\n _first_error = exc.errors()[0]\n msg = f\"'{_first_error['loc'][1] if len(_first_error['loc']) > 1 else _first_error['loc'][0]}' {_first_error['msg'].lower()}\"\n lmsg = f'- \"{request.method} {request.url.path}\" {Status.PARAMS_ERROR.code} {msg}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=msg,\n status=Status.PARAMS_ERROR,\n request=request,\n )\n\n @staticmethod\n async def http_exception_handler(\n request: Request,\n exc: HTTPException,\n is_traceback: bool = False,\n ) -> JSONResponse:\n lmsg = f'- \"{request.method} {request.url.path}\" {exc.status_code} {exc.detail}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=exc.detail,\n code=exc.status_code,\n request=request,\n )\n\n @staticmethod\n async def exception_handler(\n request: Request,\n exc: Exception,\n is_traceback: bool = True,\n ) -> JSONResponse:\n lmsg = f'- \"{request.method} {request.url.path}\" 500 {type(exc).__name__}: {exc}'\n if is_traceback:\n lmsg = traceback.format_exc()\n g.logger.error(lmsg)\n return Responses.failure(\n msg=\"Internal system error, please try again later.\",\n code=500,\n request=request,\n )\n"
65
67
  }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastapi-scaff
3
- Version: 0.3.4
3
+ Version: 0.4.0
4
4
  Summary: This is a fastapi scaff.
5
5
  Author-email: axiner <atpuxiner@163.com>
6
6
  Project-URL: Homepage, https://github.com/atpuxiner/fastapi-scaff
@@ -29,16 +29,20 @@ Dynamic: license-file
29
29
  - integrated jwt\api-key
30
30
  - integrated celery
31
31
  - ...
32
+ - about structure:
33
+ - standard
34
+ - light
35
+ - micro
32
36
  - more documents: [请点击链接](https://blog.csdn.net/atpuxiner/article/details/144291336?fromshare=blogdetail&sharetype=blogdetail&sharerId=144291336&sharerefer=PC&sharesource=atpuxiner&sharefrom=from_link)
33
37
 
34
38
  ## Project structure
35
39
 
36
- - ASM: ASM模式
40
+ - ASM: ASM mode
37
41
  - A api
38
42
  - S services(&schemas)
39
43
  - M models
40
- - 调用过程: main.py(initializer) -> (middleware) - api - services(&schemas) - (models)
41
- - 结构如下: (命名经过多次修改敲定,简洁易懂)
44
+ - Calling process: main.py(initializer) -> (middleware) - api - services(&schemas) - (models)
45
+ - Structure: (The naming has been finalized after multiple revisions, making it concise and easy to understand)
42
46
  ```
43
47
  └── fastapi-scaff
44
48
  ├── app (应用)
@@ -69,8 +73,10 @@ Dynamic: license-file
69
73
  └── runcworker.py
70
74
  └── runserver.py
71
75
  ```
72
-
73
- - 【另:**轻量版** > **完整结构** 】请自行创建查看(`new`项目时指定参数`--light`)
76
+
77
+ - 【Other structure】
78
+ - light:Please create and view (with `-e light`)
79
+ - micro:Please create and view (with `-e micro`)
74
80
 
75
81
  ## Installation
76
82
 
@@ -83,7 +89,8 @@ This package can be installed using pip (Python>=3.11):
83
89
  - `fastapi-scaff -h`
84
90
  - 2)new project
85
91
  - `fastapi-scaff new <myproj>`
86
- - *light structure*: `fastapi-scaff new <myproj> --light`
92
+ - *light structure*: `fastapi-scaff new <myproj> -e light`
93
+ - *micro structure*: `fastapi-scaff new <myproj> -e micro`
87
94
  - 3)add api
88
95
  - `cd to project root dir`
89
96
  - `fastapi-scaff add <myapi>`
@@ -95,8 +102,8 @@ This package can be installed using pip (Python>=3.11):
95
102
  - 3)`pip install -r requirements.txt`
96
103
  - 4)`python runserver.py`
97
104
  - more parameters see:
98
- - about uvicorn: [click here](https://www.uvicorn.org/)
99
- - about gunicorn: [click here](https://docs.gunicorn.org/en/stable/)
105
+ - about uvicorn: [click here](https://www.uvicorn.org/)
106
+ - about gunicorn: [click here](https://docs.gunicorn.org/en/stable/)
100
107
 
101
108
  ## License
102
109
 
@@ -0,0 +1,10 @@
1
+ fastapi_scaff/__init__.py,sha256=DFlQgjvl1rJpw-q0MlQsk6AHVCRWzrKWeo1frnBVyCQ,120
2
+ fastapi_scaff/__main__.py,sha256=RhDYmcE3zVDDzAb83dr_b45aO0OaxHD4XqZ4t-Ow3wE,19163
3
+ fastapi_scaff/_api_tpl.json,sha256=yLmeKkE-0KqWVDLs5_SuiVMw7jV9vndmkavQkXrIF2M,8590
4
+ fastapi_scaff/_project_tpl.json,sha256=SztM1zvBHAeB_s8axqwV_jYskr5e8VA1O41kvvPMkko,108113
5
+ fastapi_scaff-0.4.0.dist-info/licenses/LICENSE,sha256=A5H6q7zd1QrL3iVs1KLsBOG0ImV-t9PpPspM4x-4Ea8,1069
6
+ fastapi_scaff-0.4.0.dist-info/METADATA,sha256=wZtcuN4zdiOw4lCv2mkERgZ55ty0JDgFbE7atVStVRk,4013
7
+ fastapi_scaff-0.4.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
+ fastapi_scaff-0.4.0.dist-info/entry_points.txt,sha256=kzs28nmpRWVCmWmZav3X7u7YOIOEir3sCkLnvQKTJbY,62
9
+ fastapi_scaff-0.4.0.dist-info/top_level.txt,sha256=LeyfUxMRhdbRHcYoH37ftfdspyZ8V3Uut2YBaTCzq2k,14
10
+ fastapi_scaff-0.4.0.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- fastapi_scaff/__init__.py,sha256=5isae89DVelkTNmHys2zxIsHOg6zi4fi-pz9g-TEuFU,120
2
- fastapi_scaff/__main__.py,sha256=dxgIpbBhhuHn4WTVKHKFfO_nq7W9g03leCVX4b4S7FY,18141
3
- fastapi_scaff/_api_tpl.json,sha256=ZnH-jM4uQ6TY71pQa35G1kaV7QRiGxysjwUvSD67ADM,7719
4
- fastapi_scaff/_project_tpl.json,sha256=05nCAuqGZQtFlcRFf26YQYPBNfvMLaMB-q0D_0BvUOA,94208
5
- fastapi_scaff-0.3.4.dist-info/licenses/LICENSE,sha256=A5H6q7zd1QrL3iVs1KLsBOG0ImV-t9PpPspM4x-4Ea8,1069
6
- fastapi_scaff-0.3.4.dist-info/METADATA,sha256=zl1VRFJILt9qpCs37WNhwqClsUwzqhd3vIlWdceR_Ao,3792
7
- fastapi_scaff-0.3.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
- fastapi_scaff-0.3.4.dist-info/entry_points.txt,sha256=kzs28nmpRWVCmWmZav3X7u7YOIOEir3sCkLnvQKTJbY,62
9
- fastapi_scaff-0.3.4.dist-info/top_level.txt,sha256=LeyfUxMRhdbRHcYoH37ftfdspyZ8V3Uut2YBaTCzq2k,14
10
- fastapi_scaff-0.3.4.dist-info/RECORD,,