Skip to main content

扩展Fastapi的微服务框架, 支持 Class Route、Class View、配置注入装饰器

Project description

remit-service

remit-service是基于FastApi框架扩展出支持多配置文件、配置注入的框架

  • 类试图
  • 类路由
  • 蓝图路由
  • 服务管理
  • 多配置文件
  • 配置注入

配置介绍

单体项目配置文件介绍

所有的配置都是已yaml支持

主配置文件project/resources/config.yaml

resources文件目录这是必须存在的, 这是存储资源文件的必须目录, 加载配置文件首先会定位resources目录

config:
  # 激活环境, 获取ENV环境变量,如果不存在赋值env
  active: !env ${ENV, dev}

环境配置文件project/resources/config.dev.yaml

service:
  ip: 0.0.0.0           # 项目启动ID
  port: 9045            # 项目启动端口
  service_name: test    # 服务启动名称

多服务项目配置文件介绍

主配置文件project/resources/config.yaml

config:
  # 激活环境, 获取ENV环境变量,如果不存在赋值env
  active: !env ${ENV, dev}
  # 跟随启动服务的配置文件
  include: !config ${service:True}
  • include:引入配置文件
    • !config ${service:True}: 加载当前启动服务的配置文件

服务配置文件project/a_service/resources/config.dev.yaml

service:
  ip: 0.0.0.0           # 项目启动ID
  port: 9045            # 项目启动端口
  service_name: a       # 服务启动名称

项目启动

定义视图

视图文件project/view/a.py

from remin_service.base.controller import Controller, RequestGet

@Controller(prefix="", tags=["测试接口"])
class DocsView:

  @RequestGet(path="/a")
  async def get_documentation(
          self,
  ):
    pass

路由注册project/init_router.py

from remin_service.base.load_router import register_nestable_blueprint_for_log

def init_routes(fastapi_app):
  # 此处必须用__name__, 否则会自动引入两次
  register_nestable_blueprint_for_log(
    fastapi_app, __name__, api_name='a'
  )

项目启动文件project/main.py

import os
import uvicorn
from remin_service.app import FastSkeletonApp
from init_router import init_routes
resources_path = os.path.dirname(__file__)

app = FastSkeletonApp(
    init_routes,
    __file__,
    resources_path=resources_path
).app

if __name__ == '__main__':
  if __name__ == '__main__':
    uvicorn.run(app=app, host=app.HOST, port=app.PORT)

配置注入

视图文件project/a_service/resources/config.dev.yaml

service:
  ip: 0.0.0.0           # 项目启动ID
  port: 9045            # 项目启动端口
  service_name: a       # 服务启动名称

test:
  a: a
  b: b

注入文件project/**/config.py

from pydantic import BaseModel
from remin_service.config import ImportConfig

@ImportConfig("test")
class Test(BaseModel):
  a: str
  b: str

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

remit_service-1.2.4.tar.gz (728.9 kB view details)

Uploaded Source

File details

Details for the file remit_service-1.2.4.tar.gz.

File metadata

  • Download URL: remit_service-1.2.4.tar.gz
  • Upload date:
  • Size: 728.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.16

File hashes

Hashes for remit_service-1.2.4.tar.gz
Algorithm Hash digest
SHA256 9c61ac28dbfca6e566174d8f644f81b344514b986d040fe7d70ae50580a01972
MD5 f0315b3c4b49420dea3fb146a90afed0
BLAKE2b-256 bb48ba7e7b214057312909edf0d4c47c1fd6bfe0548c47eb0eb1ecd7d042e099

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page