From 359c09b57f2d8d61c2b22cb2ca88f6af6e0ebfdb Mon Sep 17 00:00:00 2001 From: Ivan Fraixedes Date: Wed, 15 Nov 2023 16:21:22 +0100 Subject: [PATCH] satellite/admin/back-office: Specify router path prefix For convenience of not having to modify the API generator to contemplate the path prefix that we are adding to the back office server, we define the path prefix in a constant than the admin server and the definition of the API uses to adapt the router and the generated code. Change-Id: Ic557b0e6e88e930e03647835759bb34e06e8bb48 --- satellite/admin/back-office/api-docs.gen.md | 2 +- satellite/admin/back-office/gen/main.go | 4 +- satellite/admin/back-office/handlers.gen.go | 2 +- satellite/admin/back-office/server.go | 37 +++++-------------- .../back-office/ui/src/api/client.gen.ts | 2 +- satellite/admin/server.go | 12 ++---- 6 files changed, 19 insertions(+), 40 deletions(-) diff --git a/satellite/admin/back-office/api-docs.gen.md b/satellite/admin/back-office/api-docs.gen.md index e8052ec1d..2745decc6 100644 --- a/satellite/admin/back-office/api-docs.gen.md +++ b/satellite/admin/back-office/api-docs.gen.md @@ -13,7 +13,7 @@ Get a list with the names of the all available examples -`GET /api/v1/example/examples` +`GET /back-office/api/v1/example/examples` **Response body:** diff --git a/satellite/admin/back-office/gen/main.go b/satellite/admin/back-office/gen/main.go index 73f68349a..85a03ec2d 100644 --- a/satellite/admin/back-office/gen/main.go +++ b/satellite/admin/back-office/gen/main.go @@ -7,9 +7,11 @@ package main import ( "os" + "path" "path/filepath" "storj.io/storj/private/apigen" + backoffice "storj.io/storj/satellite/admin/back-office" ) func main() { @@ -17,7 +19,7 @@ func main() { PackageName: "admin", PackagePath: "storj.io/storj/satellite/admin/back-office", Version: "v1", - BasePath: "/api", + BasePath: path.Join(backoffice.PathPrefix, "/api"), } // This is an example and must be deleted when we define the first real endpoint. diff --git a/satellite/admin/back-office/handlers.gen.go b/satellite/admin/back-office/handlers.gen.go index cb212f2e2..8e6702f94 100644 --- a/satellite/admin/back-office/handlers.gen.go +++ b/satellite/admin/back-office/handlers.gen.go @@ -38,7 +38,7 @@ func NewExample(log *zap.Logger, mon *monkit.Scope, service ExampleService, rout auth: auth, } - exampleRouter := router.PathPrefix("/api/v1/example").Subrouter() + exampleRouter := router.PathPrefix("/back-office/api/v1/example").Subrouter() exampleRouter.HandleFunc("/examples", handler.handleGetExamples).Methods("GET") return handler diff --git a/satellite/admin/back-office/server.go b/satellite/admin/back-office/server.go index 132717eac..26a91cf9e 100644 --- a/satellite/admin/back-office/server.go +++ b/satellite/admin/back-office/server.go @@ -24,6 +24,10 @@ import ( ui "storj.io/storj/satellite/admin/back-office/ui" ) +// PathPrefix is the path that will be prefixed to the router passed to the NewServer constructor. +// This is temporary until this server will replace the storj.io/storj/satellite/admin/server.go. +const PathPrefix = "/back-office/" + // Error is the error class that wraps all the errors returned by this package. var Error = errs.Class("satellite-admin") @@ -43,55 +47,34 @@ type Server struct { config Config } -// ParentRouter is mux.Router with its full path prefix. -type ParentRouter struct { - Router *mux.Router - // PathPrefix is the full path prefix of Router. - PathPrefix string -} - // NewServer creates a satellite administration server instance with the provided dependencies and // configurations. // // When listener is nil, Server.Run is a noop. -// -// When parentRouter is nil it creates a new Router to attach the server endpoints, otherwise , it -// attaches them to the provided one, allowing to expose its functionality through another server. -func NewServer(log *zap.Logger, listener net.Listener, parentRouter *ParentRouter, config Config) *Server { +func NewServer(log *zap.Logger, listener net.Listener, root *mux.Router, config Config) *Server { server := &Server{ log: log, listener: listener, config: config, } - if parentRouter == nil { - parentRouter = &ParentRouter{} - } - - root := parentRouter.Router if root == nil { root = mux.NewRouter() } // API endpoints. - // api := root.PathPrefix("/api/").Subrouter() + // API generator already add the PathPrefix. + // _ := NewExample(log, mon, nil, root, nil) + root = root.PathPrefix(PathPrefix).Subrouter() // Static assets for the web interface. // This handler must be the last one because it uses the root as prefix, otherwise, it will serve // all the paths defined by the handlers set after this one. var staticHandler http.Handler if config.StaticDir == "" { - if parentRouter.PathPrefix != "" { - staticHandler = http.StripPrefix(parentRouter.PathPrefix, http.FileServer(http.FS(ui.Assets))) - } else { - staticHandler = http.FileServer(http.FS(ui.Assets)) - } + staticHandler = http.StripPrefix(PathPrefix, http.FileServer(http.FS(ui.Assets))) } else { - if parentRouter.PathPrefix != "" { - staticHandler = http.StripPrefix(parentRouter.PathPrefix, http.FileServer(http.Dir(config.StaticDir))) - } else { - staticHandler = http.FileServer(http.Dir(config.StaticDir)) - } + staticHandler = http.StripPrefix(PathPrefix, http.FileServer(http.Dir(config.StaticDir))) } root.PathPrefix("/").Handler(staticHandler).Methods("GET") diff --git a/satellite/admin/back-office/ui/src/api/client.gen.ts b/satellite/admin/back-office/ui/src/api/client.gen.ts index 5d959f0ed..999bcc194 100644 --- a/satellite/admin/back-office/ui/src/api/client.gen.ts +++ b/satellite/admin/back-office/ui/src/api/client.gen.ts @@ -14,7 +14,7 @@ class APIError extends Error { export class ExampleHttpApiV1 { private readonly http: HttpClient = new HttpClient(); - private readonly ROOT_PATH: string = '/api/v1/example'; + private readonly ROOT_PATH: string = '/back-office/api/v1/example'; public async getExamples(): Promise { const fullPath = `${this.ROOT_PATH}/examples`; diff --git a/satellite/admin/server.go b/satellite/admin/server.go index 61208bd52..e5cb58704 100644 --- a/satellite/admin/server.go +++ b/satellite/admin/server.go @@ -176,15 +176,9 @@ func NewServer( limitUpdateAPI.HandleFunc("/projects/{project}/limit", server.getProjectLimit).Methods("GET") limitUpdateAPI.HandleFunc("/projects/{project}/limit", server.putProjectLimit).Methods("PUT", "POST") - _ = backoffice.NewServer( - log.Named("back-office"), - nil, - &backoffice.ParentRouter{ - Router: root.PathPrefix("/back-office/").Subrouter(), - PathPrefix: "/back-office", - }, - config.BackOffice, - ) + // NewServer adds the backoffice.PahtPrefix for the static assets, but not for the API because the + // generator already add the PathPrefix to router when the API handlers are hooked. + _ = backoffice.NewServer(log.Named("back-office"), nil, root, config.BackOffice) // This handler must be the last one because it uses the root as prefix, // otherwise will try to serve all the handlers set after this one.