Skip to content

Commit

Permalink
satellite/admin/back-office: Specify router path prefix
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ifraixedes authored and jewharton committed Nov 16, 2023
1 parent 032faef commit 359c09b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 40 deletions.
2 changes: 1 addition & 1 deletion satellite/admin/back-office/api-docs.gen.md
Expand Up @@ -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:**

Expand Down
4 changes: 3 additions & 1 deletion satellite/admin/back-office/gen/main.go
Expand Up @@ -7,17 +7,19 @@ package main

import (
"os"
"path"
"path/filepath"

"storj.io/storj/private/apigen"
backoffice "storj.io/storj/satellite/admin/back-office"
)

func main() {
api := &apigen.API{
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.
Expand Down
2 changes: 1 addition & 1 deletion satellite/admin/back-office/handlers.gen.go
Expand Up @@ -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
Expand Down
37 changes: 10 additions & 27 deletions satellite/admin/back-office/server.go
Expand Up @@ -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")

Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion satellite/admin/back-office/ui/src/api/client.gen.ts
Expand Up @@ -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`;
Expand Down
12 changes: 3 additions & 9 deletions satellite/admin/server.go
Expand Up @@ -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.
Expand Down

0 comments on commit 359c09b

Please sign in to comment.