From 91c977fc16ba6ae6e724f8aefa76a461aad8454b Mon Sep 17 00:00:00 2001 From: qiangyanwen <508737091@qq.com> Date: Thu, 26 May 2022 09:36:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 6 ++++ config/__init__.py | 19 +++++++++++++ entity/__init__.py | 5 ++++ entity/setting_entity.py | 60 ++++++++++++++++++++++++++++++++++++++++ utils/system.py | 34 +++++++++++++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 entity/__init__.py create mode 100644 entity/setting_entity.py create mode 100644 utils/system.py diff --git a/app.py b/app.py index 61a9ab9..3b70c6e 100644 --- a/app.py +++ b/app.py @@ -5,4 +5,10 @@ # @File :app.py from fastapi import FastAPI from router import routers +from fastapi import status +from fastapi.responses import JSONResponse +from fastapi.encoders import jsonable_encoder +import uvicorn +from config import Debug + diff --git a/config/__init__.py b/config/__init__.py index 29c4c27..ad40e1e 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -3,3 +3,22 @@ # @Email :508737091@qq.com # @Author :qiangyanwen # @File :__init__.py.py +import yaml, os +from yaml import CLoader as Loader + +from utils.system import system +from entity.setting_entity import SettingModel +from config.log import logger +Debug = os.getenv("DEBUG", True) +platforms = ["darwin", "win32", "win64"] +if system in platforms: + # 启动开发环境 + logger.info("start the automation service development environment") + config_file = "application_dev.yaml" +else: + # 启动生产环境 + logger.info("start the automation service production environment") + config_file = "application_prod.yaml" +logger.info("loading environment configuration file") +project_path = os.path.dirname(os.path.realpath(__file__)) +settings = SettingModel(**yaml.load(open(os.path.join(project_path, config_file)), Loader)) \ No newline at end of file diff --git a/entity/__init__.py b/entity/__init__.py new file mode 100644 index 0000000..f0caae5 --- /dev/null +++ b/entity/__init__.py @@ -0,0 +1,5 @@ +# _*_ coding: utf-8 _*_ +# @Time :2022/5/26 09:31 +# @Email :508737091@qq.com +# @Author :qiangyanwen +# @File :__init__.py.py diff --git a/entity/setting_entity.py b/entity/setting_entity.py new file mode 100644 index 0000000..c3478cb --- /dev/null +++ b/entity/setting_entity.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- + +""" +------------------------------------------------- + File Name: setting_entity + Description : + Author : qiangyanwen + date: 2022/1/14 +------------------------------------------------- +""" +from datetime import datetime + +from pydantic import BaseModel +from typing import List + + +class MysqlModel(BaseModel): + USERNAME: str + PASSWORD: str + HOST: str + PORT: int + DATABASE: str + + +class RedisModel(BaseModel): + HOST: str + PASSWORD: str + DB: int + PORT: int + TIMEOUT: int + + +class ProjectModel(BaseModel): + DEBUG: bool + PORT: int + BASE_PATH: str + + +class DataBaseModel(BaseModel): + Mysql: MysqlModel + + +class SwaggerModel(BaseModel): + TITLE: str + DESCRIPTION: str + DOCS_URL: str + REDOC_URL: str + + +class AccessModel(BaseModel): + ACCESS_TOKEN_EXPIRE_MINUTES: int + ALGORITHM: str + SECRET_KEY: str + + +class SettingModel(BaseModel): + PROJECT: ProjectModel + SWAGGER: SwaggerModel + ACCESS: AccessModel + DATABASE: DataBaseModel diff --git a/utils/system.py b/utils/system.py new file mode 100644 index 0000000..785d126 --- /dev/null +++ b/utils/system.py @@ -0,0 +1,34 @@ +# _*_ coding: utf-8 _*_ +# @Time :2022/5/26 09:33 +# @Email :508737091@qq.com +# @Author :qiangyanwen +# @File :system.py +import socket +import sys +from _yaml import yaml + + +def get_host_ip(): + """ + 查询本机ip地址 + :return: + """ + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + s.connect(('8.8.8.8', 80)) + ip = s.getsockname()[0] + finally: + s.close() + + return ip + + +def current_platform(): + return sys.platform + + +system = current_platform() +host = get_host_ip() + + +__all__ = ["system", "host"] \ No newline at end of file