Files
mygo/docs/architecture.md
Huxley 7fb125ea87 Implement web API foundation
Add application container, Gin router, graceful shutdown handler,
and version endpoint. This establishes the skeleton for the WebDisk
HTTP API as described in the architecture.

- Add internal/app/WebApp for runtime dependencies and version
- Add internal/server/router with GET /api/v1/version route
- Add graceful shutdown runner with signal handling in cmd/serve
- Add internal/api/ErrorResponse for standard HTTP error body
- Update roadmap, architecture, and decisions documentation
2026-04-27 23:06:06 +08:00

80 lines
3.0 KiB
Markdown

# Architecture
## Layered Design
```
Handler (Gin handlers) ← translates HTTP ↔ Service calls
Service (business logic) ← orchestrates, authorizes, validates
↓ ↓
Repository (GORM data access) Storage (file I/O)
↓ ↓
[SQLite / PostgreSQL] [Local FS / S3]
```
Rules:
- Handler has no business logic — parse request, call service, write response.
- Service has no HTTP awareness — operates on domain models and interfaces.
- Repository abstracts the database; Storage abstracts where bytes live.
- `internal/server` is the composition root — wires all dependencies together.
## Package Map
| Layer | Package | Purpose | Status |
|-------|---------|---------|--------|
| **CLI** | `cmd` | Cobra root command | ✅ skeleton |
| | `cmd/serve.go` | `mygo serve` — wire deps, start HTTP | ✅ skeleton |
| | `cmd/config.go` | `mygo config` — config subcommand | ⬜ plan |
| | `cmd/status.go` | `mygo status` — health check | ⬜ plan |
| **Config** | `internal/config` | Viper load (YAML + env + flags) | ✅ skeleton |
| **App** | `internal/app` | Runtime dependency container and build metadata | ✅ skeleton |
| **HTTP** | `internal/server` | Gin router init, route registration, graceful shutdown | ✅ skeleton |
| | `internal/handler` | HTTP handlers (auth, file, admin, webdav...) | ✅ skeleton |
| | `internal/middleware` | Gin middleware (logger, cors, auth) | ⬜ plan |
| **Business** | `internal/service` | Business logic (auth, file, admin) | ⬜ plan |
| | `internal/model` | Domain types (User, File, errors) | ⬜ plan |
| **Data** | `internal/repository` | Repository interfaces + GORM implementations | ⬜ plan |
| | `internal/storage` | Storage backend interface + local disk impl | ⬜ plan |
| **Util** | `internal/auth` | JWT sign/verify, context helpers | ⬜ plan |
| | `internal/api` | Error body helpers | ✅ skeleton |
## API Routes (v0)
```
GET /api/v1/version
POST /api/v1/auth/register
POST /api/v1/auth/login
POST /api/v1/auth/refresh
GET /api/v1/users/me
PATCH /api/v1/users/me
GET /api/v1/files
POST /api/v1/files
GET /api/v1/files/:id
GET /api/v1/files/:id/content
PUT /api/v1/files/:id
DELETE /api/v1/files/:id
GET /api/v1/admin/users
GET /api/v1/admin/users/:id
PUT /api/v1/admin/users/:id
DELETE /api/v1/admin/users/:id
```
## Middleware Chain
Applied globally by `gin.Default()`: logger → recovery
Planned globally: cors
Applied to protected groups: auth (JWT validation, inject user into gin.Context)
## Server Responsibilities
- `cmd/serve.go` loads config, creates `app.WebApp`, builds the router, and starts the HTTP server.
- `app.WebApp` carries runtime dependencies and build metadata needed to assemble handlers.
- `internal/server` owns Gin router setup, `/api/v1` route groups, and HTTP server lifecycle.
- `RunWithGracefulShutdown` stops accepting new requests on termination and gives in-flight requests time to finish.