|
| 1 | +# Console API Web MVC Implementation |
| 2 | + |
| 3 | +This reference explains how dubbo-admin exposes backend HTTP APIs through Gin handlers, typed models, console services, resource managers, and stores. |
| 4 | + |
| 5 | +## End-to-end call chain |
| 6 | + |
| 7 | +```text |
| 8 | +runtime.RegisterComponent(&consoleWebServer{}) |
| 9 | + -> Bootstrap initializes consoleWebServer.Init |
| 10 | + -> Init creates gin.Engine, middleware, static UI, health route |
| 11 | + -> Runtime starts consoleWebServer.Start |
| 12 | + -> Start creates consolectx.NewConsoleContext(coreRt) |
| 13 | + -> router.InitRouter(c.Engine, c.cs) |
| 14 | + -> HTTP request under /api/v1 |
| 15 | + -> handler binds query/path/body |
| 16 | + -> handler calls service function |
| 17 | + -> service uses consolectx.Context and ctx.ResourceManager() |
| 18 | + -> manager/store reads or writes resources |
| 19 | + -> service returns typed model response |
| 20 | + -> handler wraps with model.NewSuccessResp or util.Handle*Error |
| 21 | +``` |
| 22 | + |
| 23 | +## Console component |
| 24 | + |
| 25 | +Key file: `pkg/console/component.go` |
| 26 | + |
| 27 | +Console is registered as a runtime component and depends on `runtime.ResourceManager`: |
| 28 | + |
| 29 | +```go |
| 30 | +func init() { |
| 31 | + runtime.RegisterComponent(&consoleWebServer{}) |
| 32 | +} |
| 33 | + |
| 34 | +func (c *consoleWebServer) RequiredDependencies() []runtime.ComponentType { |
| 35 | + return []runtime.ComponentType{runtime.ResourceManager} |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +`Init` sets up Gin: |
| 40 | + |
| 41 | +- embedded admin UI mounted at `/admin` |
| 42 | +- SPA fallback for `/admin/**` |
| 43 | +- `/health` endpoint |
| 44 | +- cookie session store |
| 45 | +- auth middleware |
| 46 | +- zap logging and recovery middleware |
| 47 | +- Gin mode from config |
| 48 | + |
| 49 | +`Start` creates console context and registers `/api/v1` routes: |
| 50 | + |
| 51 | +```go |
| 52 | +c.cs = consolectx.NewConsoleContext(coreRt) |
| 53 | +router.InitRouter(c.Engine, c.cs) |
| 54 | +httpServer := c.startHttpServer(errChan) |
| 55 | +``` |
| 56 | + |
| 57 | +On stop, the HTTP server shuts down with `Shutdown(context.Background())`. |
| 58 | + |
| 59 | +## Auth behavior |
| 60 | + |
| 61 | +`authMiddleware` skips paths ending in `/login`. Other requests require a session value named `user`. Missing user returns HTTP 401 with `model.NewBizErrorResp`. |
| 62 | + |
| 63 | +This means many API handlers assume authentication already passed. |
| 64 | + |
| 65 | +## Console context |
| 66 | + |
| 67 | +Key file: `pkg/console/context/context.go` |
| 68 | + |
| 69 | +`consolectx.Context` wraps runtime access: |
| 70 | + |
| 71 | +```go |
| 72 | +type Context interface { |
| 73 | + ResourceManager() manager.ResourceManager |
| 74 | + CounterManager() counter.CounterManager |
| 75 | + Config() app.AdminConfig |
| 76 | + AppContext() context.Context |
| 77 | + LockManager() lock.Lock |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +`ResourceManager()` retrieves the runtime ResourceManager component and returns its manager: |
| 82 | + |
| 83 | +```go |
| 84 | +rmc, _ := c.coreRt.GetComponent(runtime.ResourceManager) |
| 85 | +return rmc.(manager.ResourceManagerComponent).ResourceManager() |
| 86 | +``` |
| 87 | + |
| 88 | +CounterManager and LockManager are optional and may return nil. |
| 89 | + |
| 90 | +## Route registration |
| 91 | + |
| 92 | +Key file: `pkg/console/router/router.go` |
| 93 | + |
| 94 | +All Console API routes are grouped under `/api/v1`: |
| 95 | + |
| 96 | +```go |
| 97 | +router := r.Group("/api/v1") |
| 98 | +``` |
| 99 | + |
| 100 | +Common groups include: |
| 101 | + |
| 102 | +- `/auth` |
| 103 | +- `/instance` |
| 104 | +- `/application` |
| 105 | +- `/service` |
| 106 | +- `/configurator` |
| 107 | +- `/condition-rule` |
| 108 | +- `/tag-rule` |
| 109 | +- global `/search`, `/overview`, `/metadata`, `/meshes` |
| 110 | + |
| 111 | +When adding an endpoint, register the route in the existing group that matches frontend URL structure. |
| 112 | + |
| 113 | +## Handler pattern |
| 114 | + |
| 115 | +Handlers usually close over `consolectx.Context` and return `gin.HandlerFunc`: |
| 116 | + |
| 117 | +```go |
| 118 | +func GetApplicationDetail(ctx consolectx.Context) gin.HandlerFunc { |
| 119 | + return func(c *gin.Context) { |
| 120 | + req := &model.ApplicationDetailReq{} |
| 121 | + if err := c.ShouldBindQuery(req); err != nil { |
| 122 | + util.HandleArgumentError(c, err) |
| 123 | + return |
| 124 | + } |
| 125 | + resp, err := service.GetApplicationDetail(ctx, req) |
| 126 | + if err != nil { |
| 127 | + util.HandleServiceError(c, err) |
| 128 | + return |
| 129 | + } |
| 130 | + c.JSON(http.StatusOK, model.NewSuccessResp(resp)) |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +Read endpoints commonly use `ShouldBindQuery`. Mutation endpoints commonly use `ShouldBindJSON` or path parameters depending on existing handler style. |
| 136 | + |
| 137 | +## Error and response model |
| 138 | + |
| 139 | +Key files: |
| 140 | + |
| 141 | +- `pkg/console/model/common.go` |
| 142 | +- `pkg/console/util/error.go` |
| 143 | + |
| 144 | +Success response: |
| 145 | + |
| 146 | +```go |
| 147 | +model.NewSuccessResp(data) |
| 148 | +``` |
| 149 | + |
| 150 | +Service errors are normalized to `bizerror.Error` and returned with HTTP 200: |
| 151 | + |
| 152 | +```go |
| 153 | +func HandleServiceError(ctx *gin.Context, err error) { |
| 154 | + var e bizerror.Error |
| 155 | + if !errors.As(err, &e) { |
| 156 | + e = bizerror.New(bizerror.UnknownError, err.Error()) |
| 157 | + } |
| 158 | + ctx.JSON(http.StatusOK, model.NewBizErrorResp(e)) |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +Argument errors use `bizerror.InvalidArgument`. Auth middleware is an exception and returns HTTP 401. |
| 163 | + |
| 164 | +## Service layer |
| 165 | + |
| 166 | +Key directory: `pkg/console/service/` |
| 167 | + |
| 168 | +Services receive `consolectx.Context` and typed request models. They usually access resources through generic manager helpers or `ctx.ResourceManager()` directly. |
| 169 | + |
| 170 | +Typical resource query: |
| 171 | + |
| 172 | +```go |
| 173 | +resources, err := manager.ListByIndexes[*meshresource.ServiceProviderMetadataResource]( |
| 174 | + ctx.ResourceManager(), |
| 175 | + meshresource.ServiceProviderMetadataKind, |
| 176 | + []index.IndexCondition{...}, |
| 177 | +) |
| 178 | +``` |
| 179 | + |
| 180 | +Do not put store/index details into handlers. Keep HTTP binding in handlers and resource logic in services. |
| 181 | + |
| 182 | +## Frontend contract |
| 183 | + |
| 184 | +Frontend API callers live under `ui-vue3/src/api/`. If a response field, request parameter, or endpoint path changes, check both Console model structs and frontend caller usage. |
| 185 | + |
| 186 | +## Common failure modes |
| 187 | + |
| 188 | +- Route registered under wrong group or duplicate service group block. |
| 189 | +- Handler binds query when frontend sends JSON, or vice versa. |
| 190 | +- Model tags do not match frontend parameter names. |
| 191 | +- Service returns raw resource shape not expected by frontend view. |
| 192 | +- Error returned directly instead of through `util.HandleServiceError`. |
| 193 | +- Optional managers such as CounterManager or LockManager are nil. |
| 194 | + |
| 195 | +## Review checklist |
| 196 | + |
| 197 | +- Route path and HTTP method match frontend usage. |
| 198 | +- Handler binding matches request source. |
| 199 | +- Request/response models have correct tags and JSON field names. |
| 200 | +- Service owns business logic and uses ResourceManager/store helpers. |
| 201 | +- Response uses `CommonResp` consistently. |
| 202 | +- Frontend API client is updated when contract changes. |
0 commit comments