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
This commit is contained in:
2026-04-27 23:06:06 +08:00
parent c0c34eb914
commit 7fb125ea87
15 changed files with 469 additions and 32 deletions

View File

@@ -23,23 +23,26 @@ Rules:
| Layer | Package | Purpose | Status |
|-------|---------|---------|--------|
| **CLI** | `cmd` | Cobra root command | ✅ skeleton |
| | `cmd/serve.go` | `mygo serve` — wire deps, start HTTP | ⬜ plan |
| | `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) | ⬜ plan |
| **HTTP** | `internal/server` | Gin engine init, route registration, middleware chain | ⬜ plan |
| | `internal/handler` | HTTP handlers (auth, file, admin, webdav...) | ⬜ plan |
| | `internal/middleware` | Gin middleware (requestid, logger, cors, auth) | ⬜ 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` | JSON response writer, error code 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
@@ -62,6 +65,15 @@ DELETE /api/v1/admin/users/:id
## Middleware Chain
Applied globally: requestid → logger → cors
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.