From 7aa582ee3d96cfbee989aed4392b633eba525a1e Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Sat, 25 Apr 2026 03:32:05 +0300 Subject: [PATCH] feat: Add charging routes and handler integration to API server - Add chargingH field to serverDeps struct - Add ChargingEngineClient initialization in buildDeps - Add NewChargingHandler with chargingEngineClient dependency - Add registerChargingRoutes function with /charging group - Add POST /charging/credit/:ip/check endpoint - Add GET /charging/credit/:ip/balance endpoint - Add POST /charging/credit/:ip/add endpoint - Add POST /charging/credit/:ip/deduct endpoint - Call registerChargingRoutes in --- apps/api-server/cmd/server_deps.go | 5 +++++ apps/api-server/cmd/server_routes.go | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/apps/api-server/cmd/server_deps.go b/apps/api-server/cmd/server_deps.go index 0adacc6..a2f3d08 100644 --- a/apps/api-server/cmd/server_deps.go +++ b/apps/api-server/cmd/server_deps.go @@ -26,6 +26,7 @@ type serverDeps struct { configH *handlers.ConfigHandler chaosH *handlers.ChaosHandler billingH *handlers.BillingHandler + chargingH *handlers.ChargingHandler } // buildDeps constructs all downstream services and handlers. @@ -46,6 +47,9 @@ func buildDeps(db *database.Database, cfg *config.Config) *serverDeps { deploymentSvc := services.NewDeploymentService(db) promSvc := monitoring.NewPrometheusService() + // Initialize charging engine client + chargingEngineClient := services.NewChargingEngineClient(&cfg.ChargingEngine) + k8sSvc, k8sErr := services.NewKubernetesService() if k8sErr != nil { log.Printf("Kubernetes integration disabled: %v", k8sErr) @@ -63,6 +67,7 @@ func buildDeps(db *database.Database, cfg *config.Config) *serverDeps { configH: handlers.NewConfigHandler(configStoreSvc), chaosH: handlers.NewChaosHandler(chaosSvc), billingH: handlers.NewBillingHandler(invoiceSvc, db.DB), + chargingH: handlers.NewChargingHandler(chargingEngineClient), } } diff --git a/apps/api-server/cmd/server_routes.go b/apps/api-server/cmd/server_routes.go index 66dc4b1..d5c8667 100644 --- a/apps/api-server/cmd/server_routes.go +++ b/apps/api-server/cmd/server_routes.go @@ -51,6 +51,7 @@ func registerV1Routes(router *gin.Engine, d *serverDeps) { registerBillingRoutes(apiProtected, d) registerConfigRoutes(apiProtected, d) registerChaosRoutes(apiProtected, d) + registerChargingRoutes(apiProtected, d) } } @@ -159,3 +160,11 @@ func registerChaosRoutes(api *gin.RouterGroup, d *serverDeps) { applyRBAC(w, d.casbinSvc, "/v1/chaos", "POST", "admin") w.POST("/experiments", d.chaosH.Run) } + +func registerChargingRoutes(api *gin.RouterGroup, d *serverDeps) { + charging := api.Group("/charging") + charging.POST("/credit/:ip/check", d.chargingH.CheckCredit) + charging.GET("/credit/:ip/balance", d.chargingH.GetBalance) + charging.POST("/credit/:ip/add", d.chargingH.AddCredit) + charging.POST("/credit/:ip/deduct", d.chargingH.DeductCredit) +}