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
This commit is contained in:
Ivan Fraixedes 2023-11-15 16:21:22 +01:00 committed by Jeremy Wharton
parent 032faefa4b
commit 359c09b57f
6 changed files with 19 additions and 40 deletions

View File

@ -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:**

View File

@ -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.

View File

@ -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

View File

@ -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")

View File

@ -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<string[]> {
const fullPath = `${this.ROOT_PATH}/examples`;

View File

@ -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.