# zy7y/mini-rbac architecture

> Architecture diagram of the GitHub repository zy7y/mini-rbac, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zy7y/mini-rbac
- Repository: https://github.com/zy7y/mini-rbac

## Overview

1. **Overall shape**
   This repository is a two-part RBAC admin system: a **FastAPI backend** under `backend/` and a **Vue 3 frontend** under `frontend/`. The backend exposes authentication and CRUD APIs; the frontend renders the admin UI and enforces client-side route/menu/button visibility based on the logged-in user’s role and permissions.

2. **Backend runtime and persistence**
   The backend is a single Python API service built with **FastAPI** and run via **Uvicorn**. It uses **Tortoise ORM** for data access and stores data in a local SQLite database (`backend/mini.db`), which makes the project self-contained for local/demo use. `docker-compose.yml` and `backend/Dockerfile` indicate it can also be containerized as part of a simple multi-service setup.

3. **Backend layering**
   The backend is organized into clear layers:
   - `backend/router/` for HTTP endpoints
   - `backend/service/` for business logic
   - `backend/models/` for database entities
   - `backend/schemas/` for request/response validation
   - `backend/core/` for cross-cutting infrastructure

   This is a classic controller/service/model split, with routers delegating almost everything to service classes rather than embedding logic in endpoints.

4. **Backend cross-cutting infrastructure**
   The `backend/core/` package holds the shared plumbing that shapes the whole API:
   - `security.py` for password hashing, token/JWT handling, and permission checks
   - `middleware.py` for request-level behavior such as auth or logging interception
   - `exceptions.py` for standardized API errors
   - `log.py` for logging setup
   - `dbhelper.py`, `service.py`, and `utils.py` for shared helpers and base abstractions
   - `events.py` for lifecycle hooks such as startup initialization

5. **Backend domain model**
   The core data model is small and purpose-built for RBAC:
   - `user`
   - `role`
   - `menu`
   - `relation` (to connect roles and menus, and likely users/roles depending on implementation)
   - `common` for shared fields/base schema patterns

   This mirrors the product scope: users log in, are assigned roles, and roles determine accessible menus and actions.

6. **Backend API surface**
   The routers are split by domain:
   - `auth.py` handles login/logout/current-user/session actions
   - `user.py` handles user management
   - `role.py` handles role management
   - `menu.py` handles menu management

   These endpoints likely back the frontend’s admin screens and also enforce server-side authorization, which the README explicitly calls out as one of the main features.

7. **Authorization flow**
   The architecture is centered on RBAC at multiple layers:
   - **Authentication**: login returns a token/session identity
   - **Role resolution**: the backend determines the user’s active role
   - **Permission projection**: menu trees and button permissions are derived from the role
   - **API enforcement**: backend routes are protected so unauthorized requests are rejected even if the UI is bypassed

   The README’s “switch role” feature suggests the active role can be changed without changing the account, which then updates the visible UI and permitted APIs.

8. **Frontend runtime and state**
   The frontend is a **Vue 3 + Vite** application using **Ant Design Vue** for UI, **Pinia** for state, and `pinia-plugin-persistedstate` for keeping auth/user state across refreshes. `vue-router` handles navigation, and `axios`-based request utilities wrap backend API calls.

9. **Frontend structure**
   The frontend is organized by responsibility:
   - `src/views/` contains page-level screens
   - `src/components/` contains reusable layout/table/chart/modal/markdown pieces
   - `src/service/` contains API clients for user/role/menu
   - `src/stores/` contains persisted application state
   - `src/utils/` contains shared helpers, request setup, directives, and dynamic component loading
   - `src/router/` defines route structure and guards

   This separation suggests the app is built around a dynamic admin shell rather than static pages.

10. **Frontend permission-driven UI**
   The UI is not just a consumer of data; it is part of the permission system. The presence of:
   - `sider-menu.vue`
   - `select-role.vue`
   - route guards
   - directive helpers
   - menu/role/user stores

   indicates the frontend constructs its navigation and button availability from backend-provided role/menu data. That supports the repo’s stated goals of menu, route, and button permission control.

11. **Main user-facing modules**
   The primary screens are:
   - `login/` for authentication
   - `main/main.vue` as the authenticated shell
   - `dashboard/` for overview content and charts
   - `system/user`, `system/role`, `system/menu` for the core CRUD admin functions
   - `error/404.vue` and related back/navigation screens for routing fallback
   - `test/debug/` for internal verification or demo tooling

   The layout components in `src/components/layout/` provide the persistent header, breadcrumb, and side navigation around these pages.

12. **Deployment and packaging**
   The repo is designed for both local and containerized execution:
   - separate Dockerfiles for backend and frontend
   - `frontend/nginx.conf` for serving the built SPA
   - `.env.development` / `.env.production` for environment-specific API targets
   - `docker-compose.yml` for end-to-end startup

   This means the architecture is split at runtime into a browser client and a backend API service, with Nginx likely serving the compiled frontend in production.

13. **Operational and quality boundaries**
   The backend includes `tests/` and a `pytest` setup, which suggests API/service validation is part of the repo’s structure even if the suite is small. Shared exception/logging/middleware modules indicate that request handling, error translation, and observability are treated as platform concerns rather than being duplicated in each endpoint.

14. **How the main data flow works**
   In practice, the flow is:
   - user opens the frontend and logs in
   - frontend calls the auth API
   - backend validates credentials and returns identity/permission data
   - frontend stores auth state and builds routes/menu visibility
   - CRUD screens call user/role/menu APIs through the service layer
   - backend persists changes through ORM models and enforces access rules on every request

   That is the key architectural loop to capture in a diagram: **browser UI ↔ FastAPI API ↔ ORM/SQLite**, with RBAC logic crossing both layers.

Main entry points: [`backend/main.py`](https://github.com/zy7y/mini-rbac/blob/master/backend/main.py) (API entry), [`backend/router`](https://github.com/zy7y/mini-rbac/tree/master/backend/router) (Routers), [`backend/service`](https://github.com/zy7y/mini-rbac/tree/master/backend/service) (Services), [`backend/core`](https://github.com/zy7y/mini-rbac/tree/master/backend/core) (Core)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend"]
  node_backend_main["API entry<br/>FastAPI app<br/>[main.py]"]
  node_backend_router["Routers<br/>HTTP endpoints"]
  node_backend_service["Services<br/>business logic"]
  node_backend_models[("Models<br/>ORM entities")]
  node_backend_schemas["Schemas<br/>validation DTOs"]
  node_backend_core{{"Core<br/>shared infra"}}
  node_backend_security{{"Security<br/>authn/authz<br/>[security.py]"}}
  node_backend_events(("Events<br/>lifecycle hooks<br/>[events.py]"))
  node_backend_db[("SQLite DB<br/>Tortoise store<br/>[mini.db]")]
end

subgraph group_frontend["Frontend"]
  node_frontend_app["Vue app<br/>SPA shell<br/>[main.js]"]
  node_frontend_router["Router<br/>navigation<br/>[index.js]"]
  node_frontend_stores["Stores<br/>Pinia state"]
  node_frontend_request["API client<br/>axios layer<br/>[request.js]"]
  node_frontend_services["Services<br/>API modules"]
  node_frontend_layout["Admin shell<br/>layout components"]
  node_frontend_views["Views<br/>screens"]
  node_frontend_ui{{"Permission UI<br/>menu/route control<br/>[directive.js]"}}
end

subgraph group_deploy["Deployment"]
  node_deploy_compose["Compose<br/>multi-service setup<br/>[docker-compose.yml]"]
  node_deploy_backend["Backend Docker<br/>container image"]
  node_deploy_frontend["Frontend Docker<br/>container image"]
end

node_backend_main -->|"registers"| node_backend_router
node_backend_main -->|"uses"| node_backend_core
node_backend_main -->|"connects"| node_backend_db
node_backend_router -->|"delegates"| node_backend_service
node_backend_service -->|"persists via"| node_backend_models
node_backend_service -->|"validates"| node_backend_schemas
node_backend_core -->|"contains"| node_backend_security
node_backend_core -->|"contains"| node_backend_events
node_backend_security -->|"protects"| node_backend_router
node_frontend_app -->|"mounts"| node_frontend_router
node_frontend_app -->|"hydrates"| node_frontend_stores
node_frontend_router -->|"renders"| node_frontend_views
node_frontend_router -->|"guards"| node_frontend_ui
node_frontend_views -->|"composes"| node_frontend_layout
node_frontend_layout -->|"consumes"| node_frontend_ui
node_frontend_views -->|"calls"| node_frontend_services
node_frontend_services -->|"wraps"| node_frontend_request
node_frontend_request -->|"requests"| node_backend_router
node_frontend_stores -->|"drives"| node_frontend_ui
node_deploy_compose -->|"builds"| node_deploy_backend
node_deploy_compose -->|"builds"| node_deploy_frontend
node_deploy_frontend -->|"serves"| node_frontend_app
node_deploy_backend -->|"runs"| node_backend_main

click node_backend_main "https://github.com/zy7y/mini-rbac/blob/master/backend/main.py"
click node_backend_router "https://github.com/zy7y/mini-rbac/tree/master/backend/router"
click node_backend_service "https://github.com/zy7y/mini-rbac/tree/master/backend/service"
click node_backend_models "https://github.com/zy7y/mini-rbac/tree/master/backend/models"
click node_backend_schemas "https://github.com/zy7y/mini-rbac/tree/master/backend/schemas"
click node_backend_core "https://github.com/zy7y/mini-rbac/tree/master/backend/core"
click node_backend_security "https://github.com/zy7y/mini-rbac/blob/master/backend/core/security.py"
click node_backend_events "https://github.com/zy7y/mini-rbac/blob/master/backend/core/events.py"
click node_backend_db "https://github.com/zy7y/mini-rbac/blob/master/backend/mini.db"
click node_frontend_app "https://github.com/zy7y/mini-rbac/blob/master/frontend/src/main.js"
click node_frontend_router "https://github.com/zy7y/mini-rbac/blob/master/frontend/src/router/index.js"
click node_frontend_stores "https://github.com/zy7y/mini-rbac/tree/master/frontend/src/stores"
click node_frontend_request "https://github.com/zy7y/mini-rbac/blob/master/frontend/src/utils/request.js"
click node_frontend_services "https://github.com/zy7y/mini-rbac/tree/master/frontend/src/service"
click node_frontend_layout "https://github.com/zy7y/mini-rbac/tree/master/frontend/src/components/layout"
click node_frontend_views "https://github.com/zy7y/mini-rbac/tree/master/frontend/src/views"
click node_frontend_ui "https://github.com/zy7y/mini-rbac/blob/master/frontend/src/utils/directive.js"
click node_deploy_compose "https://github.com/zy7y/mini-rbac/blob/master/docker-compose.yml"
click node_deploy_backend "https://github.com/zy7y/mini-rbac/tree/master/backend/Dockerfile"
click node_deploy_frontend "https://github.com/zy7y/mini-rbac/tree/master/frontend/Dockerfile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_backend_main,node_backend_router,node_backend_service,node_backend_models,node_backend_schemas,node_backend_core,node_backend_security,node_backend_events,node_backend_db toneBlue
class node_frontend_app,node_frontend_router,node_frontend_stores,node_frontend_request,node_frontend_services,node_frontend_layout,node_frontend_views,node_frontend_ui toneAmber
class node_deploy_compose,node_deploy_backend,node_deploy_frontend toneMint
```

## Components

### Backend

FastAPI RBAC API, domain services, and persistence

- [API entry](https://github.com/zy7y/mini-rbac/blob/master/backend/main.py): FastAPI app · Application bootstrap and server wiring · `backend/main.py`
- [Routers](https://github.com/zy7y/mini-rbac/tree/master/backend/router): HTTP endpoints · Auth and CRUD route handlers · `backend/router`
- [Services](https://github.com/zy7y/mini-rbac/tree/master/backend/service): business logic · Domain rules and orchestration · `backend/service`
- [Models](https://github.com/zy7y/mini-rbac/tree/master/backend/models): ORM entities · RBAC database entities and relations · `backend/models`
- [Schemas](https://github.com/zy7y/mini-rbac/tree/master/backend/schemas): validation DTOs · Request and response contracts · `backend/schemas`
- [Core](https://github.com/zy7y/mini-rbac/tree/master/backend/core): shared infra · Security, middleware, logging, events, and helpers · `backend/core`
- [Security](https://github.com/zy7y/mini-rbac/blob/master/backend/core/security.py): authn/authz · Password hashing, tokens, and permission checks · `backend/core/security.py`
- [Events](https://github.com/zy7y/mini-rbac/blob/master/backend/core/events.py): lifecycle hooks · Startup and initialization hooks · `backend/core/events.py`
- [SQLite DB](https://github.com/zy7y/mini-rbac/blob/master/backend/mini.db): Tortoise store · Local mini database backing the admin system · `backend/mini.db`

### Frontend

Vue 3 admin UI, routing, state, and permission-driven screens

- [Vue app](https://github.com/zy7y/mini-rbac/blob/master/frontend/src/main.js): SPA shell · Client entry and root application · `frontend/src/main.js`
- [Router](https://github.com/zy7y/mini-rbac/blob/master/frontend/src/router/index.js): navigation · Route structure and guards · `frontend/src/router/index.js`
- [Stores](https://github.com/zy7y/mini-rbac/tree/master/frontend/src/stores): Pinia state · Persisted auth, role, and menu state · `frontend/src/stores`
- [API client](https://github.com/zy7y/mini-rbac/blob/master/frontend/src/utils/request.js): axios layer · Backend request wrapper and service calls · `frontend/src/utils/request.js`
- [Services](https://github.com/zy7y/mini-rbac/tree/master/frontend/src/service): API modules · User, role, and menu API clients · `frontend/src/service`
- [Admin shell](https://github.com/zy7y/mini-rbac/tree/master/frontend/src/components/layout): layout components · Header, sider menu, role switcher, and breadcrumb · `frontend/src/components/layout`
- [Views](https://github.com/zy7y/mini-rbac/tree/master/frontend/src/views): screens · Login, dashboard, system CRUD, and error pages · `frontend/src/views`
- [Permission UI](https://github.com/zy7y/mini-rbac/blob/master/frontend/src/utils/directive.js): menu/route control · Client-side visibility and dynamic screen composition · `frontend/src/utils/directive.js`

### Deployment

Container and environment wiring for local or bundled startup

- [Compose](https://github.com/zy7y/mini-rbac/blob/master/docker-compose.yml): multi-service setup · End-to-end local startup wiring · `docker-compose.yml`
- [Backend Docker](https://github.com/zy7y/mini-rbac/tree/master/backend/Dockerfile): container image · Container build for the API service · `backend/Dockerfile`
- [Frontend Docker](https://github.com/zy7y/mini-rbac/tree/master/frontend/Dockerfile): container image · Container build and Nginx packaging for the SPA · `frontend/Dockerfile`

## Connections

- API entry → Routers: registers · Bootstraps the HTTP API surface
- API entry → Core: uses · Applies shared middleware, logging, and startup hooks
- API entry → SQLite DB: connects · Initializes persistence on startup
- Routers → Services: delegates · Endpoints forward work to domain services
- Services → Models: persists via · Reads and writes RBAC entities
- Services → Schemas: validates · Uses request and response contracts
- Core → Security: contains · Security is the main cross-cutting concern
- Core → Events: contains · Lifecycle hooks live in core infrastructure
- Security → Routers: protects · Route authorization depends on shared security checks
- Vue app → Router: mounts · Client app uses route-driven navigation
- Vue app → Stores: hydrates · Application state is persisted across refreshes
- Router → Views: renders · Routes map to page-level screens
- Router → Permission UI: guards · Route access is filtered by permissions
- Views → Admin shell: composes · Authenticated screens use the admin shell
- Admin shell → Permission UI: consumes · Menu, role switcher, and button visibility depend on permissions
- Views → Services: calls · Screens load and mutate domain data through API modules
- Services → API client: wraps · Domain clients use the shared HTTP layer
- API client → Routers: requests · Browser client talks to FastAPI endpoints
- Stores → Permission UI: drives · Stored role and menu state shape visible UI
- Compose → Backend Docker: builds · Compose includes the API container
- Compose → Frontend Docker: builds · Compose includes the frontend container
- Frontend Docker → Vue app: serves · Nginx packages the compiled SPA
- Backend Docker → API entry: runs · Container starts the FastAPI service
