Skip to content

Commit

Permalink
api: Read basePath from spec/rest.yaml
Browse files Browse the repository at this point in the history
This helps to avoid changing the path v1 in several places
if we plan to change the version in spec/rest.yaml.

Signed-off-by: Tatiana Nesterenko <tatiana@nspcc.io>
  • Loading branch information
tatiana-nspcc committed Jan 25, 2024
1 parent f88f861 commit c29ab7a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,5 @@ require (
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gopkg.in/yaml.v3 v3.0.1
)
30 changes: 29 additions & 1 deletion handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package handlers
import (
"context"
"fmt"
"gopkg.in/yaml.v3"

Check failure on line 6 in handlers/api.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
"net/http"
"os"
"strings"
"time"

Expand Down Expand Up @@ -62,6 +64,10 @@ type SessionToken struct {
// ContextKey is used for context.Context value. The value requires a key that is not primitive type.
type ContextKey string

type swaggerSpec struct {
BasePath string `yaml:"basePath"`
}

const (
// BearerPrefix is the prefix for authorization token.
BearerPrefix = "Bearer "
Expand Down Expand Up @@ -197,13 +203,35 @@ func (a *API) docMiddleware(handler http.Handler) http.Handler {
if strings.HasPrefix(r.URL.Path, docsPrefix) {
fh.ServeHTTP(w, r)
} else if r.URL.Path == "" || r.URL.Path == "/" {
http.Redirect(w, r, "/v1/docs/", http.StatusFound)
// Get basePath from spec
basePath, err := getBasePath("spec/rest.yaml")
if err != nil {
a.log.Info("cannot get basePath from spec/rest.yaml", zap.Error(err))
handler.ServeHTTP(w, r)
}
http.Redirect(w, r, basePath+"/docs/", http.StatusFound)

Check warning on line 212 in handlers/api.go

View check run for this annotation

Codecov / codecov/patch

handlers/api.go#L205-L212

Added lines #L205 - L212 were not covered by tests
} else {
handler.ServeHTTP(w, r)
}
})
}

func getBasePath(path string) (string, error) {
// Read the YAML file
yamlFile, err := os.ReadFile(path)
if err != nil {
return "", err
}

Check warning on line 224 in handlers/api.go

View check run for this annotation

Codecov / codecov/patch

handlers/api.go#L219-L224

Added lines #L219 - L224 were not covered by tests

// Unmarshal the YAML into swaggerSpec struct
var spec swaggerSpec
err = yaml.Unmarshal(yamlFile, &spec)
if err != nil {
return "", err
}
return spec.BasePath, nil

Check warning on line 232 in handlers/api.go

View check run for this annotation

Codecov / codecov/patch

handlers/api.go#L227-L232

Added lines #L227 - L232 were not covered by tests
}

func (a *API) logAndGetErrorResponse(msg string, err error, fields ...zap.Field) *models.ErrorResponse {
fields = append(fields, zap.Error(err))
a.log.Error(msg, fields...)
Expand Down

0 comments on commit c29ab7a

Please sign in to comment.