Skip to content

Commit

Permalink
feat: allow configuring apiRootPrefix (#109)
Browse files Browse the repository at this point in the history
* feat: allow configuring apiRootPrefix

* asd
  • Loading branch information
dbarrosop authored Aug 29, 2022
1 parent 0f597c1 commit 85989f4
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 27 deletions.
10 changes: 8 additions & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

const (
publicURLFlag = "public-url"
apiRootPrefixFlag = "api-root-prefix"
bindFlag = "bind"
trustedProxiesFlag = "trusted-proxies"
hasuraEndpointFlag = "hasura-endpoint"
Expand Down Expand Up @@ -70,6 +71,7 @@ func ginLogger(logger *logrus.Logger) gin.HandlerFunc {

func getGin(
publicURL string,
apiRootPrefix string,
hasuraAdminSecret string,
metadataStorage controller.MetadataStorage,
contentStorage controller.ContentStorage,
Expand All @@ -82,7 +84,9 @@ func getGin(
gin.SetMode(gin.ReleaseMode)
}

ctrl := controller.New(publicURL, hasuraAdminSecret, metadataStorage, contentStorage, imageTransformer, logger)
ctrl := controller.New(
publicURL, apiRootPrefix, hasuraAdminSecret, metadataStorage, contentStorage, imageTransformer, logger,
)

middlewares := []gin.HandlerFunc{
ginLogger(logger),
Expand All @@ -94,7 +98,7 @@ func getGin(
middlewares = append(middlewares, fastly.New(fastlyService, viper.GetString(fastlyKeyFlag), logger))
}

return ctrl.SetupRouter(trustedProxies, middlewares...) // nolint: wrapcheck
return ctrl.SetupRouter(trustedProxies, apiRootPrefix, middlewares...) // nolint: wrapcheck
}

func getMetadataStorage(endpoint string) *metadata.Hasura {
Expand Down Expand Up @@ -154,6 +158,7 @@ func init() {

{
addStringFlag(serveCmd.Flags(), publicURLFlag, "http://localhost:8000", "public URL of the service")
addStringFlag(serveCmd.Flags(), apiRootPrefixFlag, "/v1", "API root prefix")
addStringFlag(serveCmd.Flags(), bindFlag, ":8000", "bind the service to this address")
addStringArrayFlag(
serveCmd.Flags(),
Expand Down Expand Up @@ -260,6 +265,7 @@ var serveCmd = &cobra.Command{
)
router, err := getGin(
viper.GetString(publicURLFlag),
viper.GetString(apiRootPrefixFlag),
viper.GetString(hasuraAdminSecretFlag),
metadataStorage,
contentStorage,
Expand Down
9 changes: 7 additions & 2 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type ContentStorage interface {

type Controller struct {
publicURL string
apiRootPrefix string
hasuraAdminSecret string
metadataStorage MetadataStorage
contentStorage ContentStorage
Expand All @@ -87,6 +88,7 @@ type Controller struct {

func New(
publicURL string,
apiRootPrefix string,
hasuraAdminSecret string,
metadataStorage MetadataStorage,
contentStorage ContentStorage,
Expand All @@ -95,6 +97,7 @@ func New(
) *Controller {
return &Controller{
publicURL,
apiRootPrefix,
hasuraAdminSecret,
metadataStorage,
contentStorage,
Expand All @@ -103,7 +106,9 @@ func New(
}
}

func (ctrl *Controller) SetupRouter(trustedProxies []string, middleware ...gin.HandlerFunc) (*gin.Engine, error) {
func (ctrl *Controller) SetupRouter(
trustedProxies []string, apiRootPrefix string, middleware ...gin.HandlerFunc,
) (*gin.Engine, error) {
router := gin.New()
if err := router.SetTrustedProxies(trustedProxies); err != nil {
return nil, fmt.Errorf("problem setting trusted proxies: %w", err)
Expand Down Expand Up @@ -132,7 +137,7 @@ func (ctrl *Controller) SetupRouter(trustedProxies []string, middleware ...gin.H

router.GET("/healthz", ctrl.Health)

apiRoot := router.Group("/v1")
apiRoot := router.Group(apiRootPrefix)
{
apiRoot.GET("/openapi.yaml", ctrl.OpenAPI)
apiRoot.GET("/version", ctrl.Version)
Expand Down
4 changes: 2 additions & 2 deletions controller/delete_broken_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func TestDeleteBrokenMetadata(t *testing.T) {
gomock.Any(), "e6aad336-ad79-4df7-a09b-5782f71948f4", gomock.Any(),
).Return(nil)

ctrl := controller.New("http://asd", "asdasd", metadataStorage, contentStorage, nil, logger)
ctrl := controller.New("http://asd", "/v1", "asdasd", metadataStorage, contentStorage, nil, logger)

router, _ := ctrl.SetupRouter(nil, ginLogger(logger))
router, _ := ctrl.SetupRouter(nil, "/v1", ginLogger(logger))

responseRecorder := httptest.NewRecorder()

Expand Down
4 changes: 2 additions & 2 deletions controller/delete_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func TestDeleteFile(t *testing.T) {
nil,
)

ctrl := controller.New("http://asd", "asdasd", metadataStorage, contentStorage, nil, logger)
ctrl := controller.New("http://asd", "/v1", "asdasd", metadataStorage, contentStorage, nil, logger)

router, _ := ctrl.SetupRouter(nil, ginLogger(logger))
router, _ := ctrl.SetupRouter(nil, "/v1", ginLogger(logger))

responseRecorder := httptest.NewRecorder()

Expand Down
4 changes: 2 additions & 2 deletions controller/delete_orphans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ func TestDeleteOrphans(t *testing.T) {
contentStorage.EXPECT().DeleteFile("default/garbage").Return(nil)
contentStorage.EXPECT().DeleteFile("bucket2/7dc0b0d0-b100-4667-89f1-0434942d9c15").Return(nil)

ctrl := controller.New("http://asd", "asdasd", metadataStorage, contentStorage, nil, logger)
ctrl := controller.New("http://asd", "/v1", "asdasd", metadataStorage, contentStorage, nil, logger)

router, _ := ctrl.SetupRouter(nil, ginLogger(logger))
router, _ := ctrl.SetupRouter(nil, "/v1", ginLogger(logger))

responseRecorder := httptest.NewRecorder()

Expand Down
4 changes: 2 additions & 2 deletions controller/get_file_information_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ func TestGetFileInfo(t *testing.T) {
CacheControl: "max-age=3600",
}, nil)

ctrl := controller.New("http://asd", "asdasd", metadataStorage, contentStorage, image.NewTransformer(), logger)
ctrl := controller.New("http://asd", "/v1", "asdasd", metadataStorage, contentStorage, image.NewTransformer(), logger)

router, _ := ctrl.SetupRouter(nil, ginLogger(logger))
router, _ := ctrl.SetupRouter(nil, "/v1", ginLogger(logger))

responseRecorder := httptest.NewRecorder()

Expand Down
5 changes: 4 additions & 1 deletion controller/get_file_presigned_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ func (ctrl *Controller) getFilePresignedURL(ctx *gin.Context) (GetFilePresignedU
apiErr.ExtendError(fmt.Sprintf("problem creating presigned URL for file %s", fileMetadata.Name))
}

url := fmt.Sprintf("%s/v1/files/%s/presignedurl/content?%s", ctrl.publicURL, fileMetadata.ID, signature)
url := fmt.Sprintf(
"%s%s/files/%s/presignedurl/content?%s",
ctrl.publicURL, ctrl.apiRootPrefix, fileMetadata.ID, signature,
)
return GetFilePresignedURLResponse{nil, url, bucketMetadata.DownloadExpiration}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions controller/get_file_presigned_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ func TestGetFilePresignedURL(t *testing.T) {
controller.ErrFileNotFound)
}

ctrl := controller.New("http://asd", "asdasd", metadataStorage, contentStorage, nil, logger)
ctrl := controller.New("http://asd", "/v1", "asdasd", metadataStorage, contentStorage, nil, logger)

router, _ := ctrl.SetupRouter(nil, ginLogger(logger))
router, _ := ctrl.SetupRouter(nil, "/v1", ginLogger(logger))

responseRecorder := httptest.NewRecorder()

Expand Down
4 changes: 2 additions & 2 deletions controller/get_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func TestGetFile(t *testing.T) {
nil,
)

ctrl := controller.New("http://asd", "asdasd", metadataStorage, contentStorage, nil, logger)
ctrl := controller.New("http://asd", "/v1", "asdasd", metadataStorage, contentStorage, nil, logger)

router, _ := ctrl.SetupRouter(nil, ginLogger(logger))
router, _ := ctrl.SetupRouter(nil, "/v1", ginLogger(logger))

responseRecorder := httptest.NewRecorder()

Expand Down
4 changes: 2 additions & 2 deletions controller/list_broken_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ func TestListBrokenMetadata(t *testing.T) {
}, nil,
)

ctrl := controller.New("http://asd", "asdasd", metadataStorage, contentStorage, nil, logger)
ctrl := controller.New("http://asd", "/v1", "asdasd", metadataStorage, contentStorage, nil, logger)

router, _ := ctrl.SetupRouter(nil, ginLogger(logger))
router, _ := ctrl.SetupRouter(nil, "/v1", ginLogger(logger))

responseRecorder := httptest.NewRecorder()

Expand Down
4 changes: 2 additions & 2 deletions controller/list_not_uploaded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func TestListNotUploaded(t *testing.T) {
}, nil,
)

ctrl := controller.New("http://asd", "asdasd", metadataStorage, contentStorage, nil, logger)
ctrl := controller.New("http://asd", "/v1", "asdasd", metadataStorage, contentStorage, nil, logger)

router, _ := ctrl.SetupRouter(nil, ginLogger(logger))
router, _ := ctrl.SetupRouter(nil, "/v1", ginLogger(logger))

responseRecorder := httptest.NewRecorder()

Expand Down
4 changes: 2 additions & 2 deletions controller/list_orphans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func TestListOrphans(t *testing.T) {
}, nil,
)

ctrl := controller.New("http://asd", "asdasd", metadataStorage, contentStorage, nil, logger)
ctrl := controller.New("http://asd", "/v1", "asdasd", metadataStorage, contentStorage, nil, logger)

router, _ := ctrl.SetupRouter(nil, ginLogger(logger))
router, _ := ctrl.SetupRouter(nil, "/v1", ginLogger(logger))

responseRecorder := httptest.NewRecorder()

Expand Down
4 changes: 2 additions & 2 deletions controller/update_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ func TestUpdateFile(t *testing.T) {
},
nil)

ctrl := controller.New("http://asd", "asdasd", metadataStorage, contentStorage, nil, logger)
ctrl := controller.New("http://asd", "/v1", "asdasd", metadataStorage, contentStorage, nil, logger)

router, _ := ctrl.SetupRouter(nil, ginLogger(logger))
router, _ := ctrl.SetupRouter(nil, "/v1", ginLogger(logger))

body, contentType := createUpdateMultiForm(t, file)

Expand Down
4 changes: 2 additions & 2 deletions controller/upload_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ func TestUploadFile(t *testing.T) {
nil)
}

ctrl := controller.New("http://asd", "asdasd", metadataStorage, contentStorage, nil, logger)
ctrl := controller.New("http://asd", "/v1", "asdasd", metadataStorage, contentStorage, nil, logger)

router, _ := ctrl.SetupRouter(nil, ginLogger(logger))
router, _ := ctrl.SetupRouter(nil, "/v1", ginLogger(logger))

body, contentType := createMultiForm(t, files...)

Expand Down

0 comments on commit 85989f4

Please sign in to comment.