amisadmin / fastapi-config

FastAPI-Config is a visual dynamic configuration management extension based on FastAPI-Amis-Admin.
10 stars 2 forks source link
fastapi fastapi-amis-admin

简体中文 | English

FastAPI-Config

Package version Downloads Chat on Gitter 229036692

Project Introduction

Fast API-Config is a visual dynamic configuration management extension package based on FastAPI-Amis-Admin.

Install

pip install fastapi-config

Simple example

main.py:

from fastapi import FastAPI
from fastapi_amis_admin import amis
from fastapi_amis_admin.admin import Settings, AdminSite
from fastapi_amis_admin.models import Field
from fastapi_config import ConfigModelAdmin, DbConfigStore, ConfigAdmin
from sqlmodel import SQLModel
from pydantic import BaseModel
from typing import List

# Create a `FastAPI` application
app = FastAPI()

# Create `AdminSite` instance
site = AdminSite(settings=Settings(database_url_async='sqlite+aiosqlite:///amisadmin.db'))

# Create a configuration repository
dbconfig = DbConfigStore(site.db)

# Register Admin (optional)
site.register_admin(ConfigModelAdmin)

class ContactCfg(BaseModel):
    name: str = Field("", title="Name")
    qq: List[str] = Field("", title="QQ")

class SiteCfg(BaseModel):
    name: str = Field(..., title="Site Name")
    logo: str = Field("", title="Site LOGO", amis_form_item=amis.InputImage())
    contacts: List[ContactCfg] = Field([], title="Contact list")
    domains: List[str] = Field([], title='Domain list')

class SiteCfgAdmin(ConfigAdmin):
    page_schema = amis.PageSchema(label='Site Config')
    schema = SiteCfg

site.register_admin(SiteCfgAdmin)

@app.get('/config')
async def read_config():
    return await dbconfig.get(SiteCfg)

@app.on_event("startup")
async def startup():
    # Mount the site to the FastAPI instance
    site.mount_app(app)
    # Create database tables (required for first run)
    await site.db.async_run_sync(SQLModel.metadata.create_all, is_session=False)

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, debug=True)

Interface/UI Preview

SchedulerAdmin

Dependent project

License

The project follows the Apache2.0 license agreement.