Skip to content

Commit

Permalink
Fix serving doc files (#28)
Browse files Browse the repository at this point in the history
* Fix swagger doc paths

* Fix FS routing and add unit tests

* Fix helpres
  • Loading branch information
crabio authored Sep 1, 2021
1 parent 4424ab2 commit b799df5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 24 deletions.
21 changes: 14 additions & 7 deletions pkg/swagger/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ func createFsHandler(fileSystem embed.FS, folder string) (http.Handler, error) {
}

func (s *Server) createHomePageHandler() (http.Handler, error) {
swaggerFilePaths := []string{
"health/v1/health_v1.swagger.json",
var swaggerFilePaths []string

// Parse and add pkg's swagger files
pkgSwaggerFilePaths, err := GetOpenAPIFilesPaths(docs.Swagger, "swagger", pkgDocsPrefix)
if err != nil {
return nil, err
}
swaggerFilePaths = append(swaggerFilePaths, pkgSwaggerFilePaths...)

// Parse and add user's swagger files
userSwaggerFilePaths, err := GetOpenAPIFilesPaths(docs.Swagger, "swagger")
if err != nil {
s.log.WithError(err).Error("No user's swagger files found")
} else {
swaggerFilePaths = append(swaggerFilePaths, userSwaggerFilePaths...)
if s.config.Docs.DocsFS != nil {
userSwaggerFilePaths, err := GetOpenAPIFilesPaths(*s.config.Docs.DocsFS, s.config.Docs.DocsRootFolder, userDocsPrefix)
if err != nil {
s.log.WithError(err).Error("No user's swagger files found")
} else {
swaggerFilePaths = append(swaggerFilePaths, userSwaggerFilePaths...)
}
}

swaggerHomeTmpl, err := template.ParseFS(docs_swagger.SwaggerTmpl, "*")
Expand Down
9 changes: 7 additions & 2 deletions pkg/swagger/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// Internal
)

func GetOpenAPIFilesPaths(fileSystem embed.FS, dirName string) ([]string, error) {
func GetOpenAPIFilesPaths(fileSystem embed.FS, dirName string, prefix string) ([]string, error) {
var filesPaths []string

libRegEx, err := regexp.Compile(`^.+\.(json)$`)
Expand All @@ -27,7 +27,12 @@ func GetOpenAPIFilesPaths(fileSystem embed.FS, dirName string) ([]string, error)
}
// Check regexp
if libRegEx.MatchString(d.Name()) {
filesPaths = append(filesPaths, path)
// Remove root dir from path
pathWithoutRoot := path[len(dirName)+1:]
// Add file path prefix
pathWithPrefix := prefix + pathWithoutRoot

filesPaths = append(filesPaths, pathWithPrefix)
}
return nil
}); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/swagger/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
)

func TestGetOpenAPIFilesPaths(t *testing.T) {
filePaths, err := swagger.GetOpenAPIFilesPaths(docs.Swagger, "swagger")
filePaths, err := swagger.GetOpenAPIFilesPaths(docs.Swagger, "swagger", "/doc/swagger/")
assert.NoError(t, err)
assert.Equal(t, 3, len(filePaths))
assert.Equal(t, "/doc/swagger/google/api/annotations.swagger.json", filePaths[0])
assert.Equal(t, "/doc/swagger/google/api/http.swagger.json", filePaths[1])
assert.Equal(t, "/doc/swagger/health/v1/health_v1.swagger.json", filePaths[2])
}
30 changes: 18 additions & 12 deletions pkg/swagger/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
"github.com/iakrevetkho/archaeopteryx/pkg/helpers"
)

const (
mainPagePath = "/doc/swagger/"
pkgDocsPrefix = "/doc/swagger/"
userDocsPrefix = "/doc"
)

type Server struct {
log *logrus.Entry
config *config.Config
Expand Down Expand Up @@ -52,27 +58,27 @@ func New(config *config.Config) (*Server, error) {
return s, nil
}

const (
mainPagePath = "/doc/swagger"
pkgDocsPrefix = "/doc/achaeopteryx"
userDocsPrefix = "/doc"
)

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == mainPagePath {
s.log.Debug("Serve main page")
s.hpHandler.ServeHTTP(w, r)
if strings.HasPrefix(r.URL.Path, pkgDocsPrefix) {
if r.URL.Path == mainPagePath {
s.log.Debug("Serve main page")
s.hpHandler.ServeHTTP(w, r)
} else {
s.log.WithField("path", r.URL.Path).Debug("Serve pkg docs")
// Remove FS prefix from URL
r.URL.Path = r.URL.Path[len(pkgDocsPrefix):]
s.pkgFsHandler.ServeHTTP(w, r)
}

} else if strings.HasPrefix(r.URL.Path, userDocsPrefix) {
s.log.WithField("path", r.URL.Path).Debug("Serve user docs")
if s.userFsHandler != nil {
// Remove FS prefix from URL
r.URL.Path = r.URL.Path[len(userDocsPrefix):]
s.userFsHandler.ServeHTTP(w, r)
} else {
s.log.Error("user fs handler is not inited. Add user swagger docs FS")
}

} else if strings.HasPrefix(r.URL.Path, pkgDocsPrefix) {
s.log.WithField("path", r.URL.Path).Debug("Serve pkg docs")
s.pkgFsHandler.ServeHTTP(w, r)
}
}
50 changes: 48 additions & 2 deletions pkg/swagger/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import (
"testing"

"github.com/jinzhu/configor"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

// Internal
"github.com/iakrevetkho/archaeopteryx/config"
"github.com/iakrevetkho/archaeopteryx/docs"
"github.com/iakrevetkho/archaeopteryx/pkg/helpers"
"github.com/iakrevetkho/archaeopteryx/pkg/swagger"
)

Expand All @@ -32,7 +35,7 @@ func TestServeMainPage(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, s)

req, err := http.NewRequest("GET", "/", nil)
req, err := http.NewRequest("GET", "/doc/swagger", nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -45,12 +48,55 @@ func TestServeMainPage(t *testing.T) {
func TestServeJS(t *testing.T) {
conf := new(config.Config)
assert.NoError(t, configor.Load(conf))
conf.Log.Level = logrus.DebugLevel
helpers.InitLogger(conf)

s, err := swagger.New(conf)
assert.NoError(t, err)
assert.NotNil(t, s)

req, err := http.NewRequest("GET", "/swagger-ui-bundle.js", nil)
req, err := http.NewRequest("GET", "/doc/swagger/swagger-ui-bundle.js", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()

s.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
}

func TestServePkgDocs(t *testing.T) {
conf := new(config.Config)
assert.NoError(t, configor.Load(conf))
conf.Log.Level = logrus.DebugLevel
helpers.InitLogger(conf)

s, err := swagger.New(conf)
assert.NoError(t, err)
assert.NotNil(t, s)

req, err := http.NewRequest("GET", "/doc/swagger/health/v1/health_v1.swagger.json", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()

s.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
}
func TestServeUsrDocs(t *testing.T) {
conf := new(config.Config)
assert.NoError(t, configor.Load(conf))
conf.Log.Level = logrus.DebugLevel
helpers.InitLogger(conf)
conf.Docs.DocsFS = &docs.Swagger
conf.Docs.DocsRootFolder = "swagger"

s, err := swagger.New(conf)
assert.NoError(t, err)
assert.NotNil(t, s)

req, err := http.NewRequest("GET", "/doc/health/v1/health_v1.swagger.json", nil)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit b799df5

Please sign in to comment.