3
0

Stabilise API docs serving, move it under API root

This commit is contained in:
Denis Arh
2020-11-08 18:25:02 +01:00
parent 725f7de2b8
commit 65bc428406
8 changed files with 53 additions and 38 deletions

View File

@@ -9,6 +9,7 @@ import (
"github.com/cortezaproject/corteza-server/pkg/webapp"
systemRest "github.com/cortezaproject/corteza-server/system/rest"
"github.com/go-chi/chi"
"go.uber.org/zap"
"strings"
"sync"
)
@@ -56,10 +57,17 @@ func (app *CortezaApp) mountHttpRoutes(r chi.Router) {
messagingRest.MountRoutes(r)
app.WsServer.ApiServerRoutes(r)
})
r.HandleFunc("/docs*", server.ServeDocs("/"+apiBaseUrl+"/docs"))
})
}
if app.Opt.HTTPServer.WebappEnabled {
app.Log.Debug(
"serving web applications",
zap.String("baseUrl", webappBaseUrl),
zap.String("baseDir", app.Opt.HTTPServer.WebappBaseDir),
)
r.Route("/"+webappBaseUrl, webapp.MakeWebappServer(app.Opt.HTTPServer))
}
}

View File

@@ -2,6 +2,8 @@
WARNING: Work in progress
WARNING: Any changes to these files here require `make docs`
This is the first step in migration towards full OpenAPI standards support.`
## Original/starting state (pre `2020.9`; deprecated):
@@ -14,4 +16,4 @@ definition. There is a converter available in https://github.com/cortezaproject/
that takes those custom YAML files and converts them to OpenAPI format.
## Goal (target `2020.12`)
Generate HTTP handlers and all auxilary code and documentation from YAML files in OpenAPI 3.0 format
Generate HTTP handlers and all auxiliary code and documentation from YAML files in OpenAPI 3.0 format

View File

@@ -40,9 +40,9 @@
// https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/
window.ui = SwaggerUIBundle({
urls: [
{ url: "/docs/system.yaml", name: 'System' },
{ url: "/docs/compose.yaml", name: 'Compose' },
{ url: "/docs/messaging.yaml", name: 'Messaging' },
{ url: "system.yaml", name: 'System' },
{ url: "compose.yaml", name: 'Compose' },
{ url: "messaging.yaml", name: 'Messaging' },
],
dom_id: '#swagger-ui',
deepLinking: true,

File diff suppressed because one or more lines are too long

View File

@@ -1,18 +0,0 @@
package api
import (
"fmt"
"github.com/cortezaproject/corteza-server/docs"
"github.com/goware/statik/fs"
"net/http"
)
func serveDocs(w http.ResponseWriter, r *http.Request) {
root, err := fs.New(docs.Asset)
if err != nil {
http.Error(w, fmt.Sprintf("could not read embeded filesystem: %v", err.Error()), http.StatusInternalServerError)
return
}
http.StripPrefix("/docs", http.FileServer(root)).ServeHTTP(w, r)
}

View File

@@ -7,12 +7,14 @@ import (
"net/http"
)
func serveDocs(w http.ResponseWriter, r *http.Request) {
root, err := fs.New(docs.Asset)
if err != nil {
http.Error(w, fmt.Sprintf("could not read embeded filesystem: %v", err.Error()), http.StatusInternalServerError)
return
}
func ServeDocs(base string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
root, err := fs.New(docs.Asset)
if err != nil {
http.Error(w, fmt.Sprintf("could not read embeded filesystem: %v", err.Error()), http.StatusInternalServerError)
return
}
http.StripPrefix("/docs", http.FileServer(root)).ServeHTTP(w, r)
http.StripPrefix(base, http.FileServer(root)).ServeHTTP(w, r)
}
}

View File

@@ -143,7 +143,4 @@ func (s server) bindMiscRoutes(router chi.Router) {
if s.httpOpt.EnableHealthcheckRoute {
router.Get("/healthcheck", healthcheck.HttpHandler())
}
//router.Group(serveDocs2)
router.HandleFunc("/docs*", serveDocs)
}

View File

@@ -2,8 +2,10 @@ package webapp
import (
"fmt"
"github.com/cortezaproject/corteza-server/pkg/logger"
"github.com/cortezaproject/corteza-server/pkg/options"
"github.com/go-chi/chi"
"go.uber.org/zap"
"net/http"
"os"
"path"
@@ -18,26 +20,48 @@ func MakeWebappServer(opt options.HTTPServerOpt) func(r chi.Router) {
for _, app := range strings.Split(opt.WebappList, ",") {
basedir := path.Join(opt.WebappBaseUrl, app)
serveConfig(r, basedir, opt.ApiBaseUrl)
r.Get(basedir+"*", serveIndex(opt.WebappBaseDir, basedir+"/index.html", fileserver))
r.Get(basedir+"*", serveIndex(opt.WebappBaseDir, basedir, fileserver))
}
serveConfig(r, opt.WebappBaseUrl, opt.ApiBaseUrl)
r.Get(opt.WebappBaseUrl+"*", serveIndex(opt.WebappBaseDir, opt.WebappBaseUrl+"/index.html", fileserver))
r.Get(opt.WebappBaseUrl+"*", serveIndex(opt.WebappBaseDir, opt.WebappBaseUrl, fileserver))
}
}
// Serves index.html in case the requested file isn't found (or some other os.Stat error)
func serveIndex(assetPath string, indexPath string, serve http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
indexPage := path.Join(assetPath, indexPath)
indexPage := path.Join(assetPath, indexPath, "index.html")
requestedPage := path.Join(assetPath, r.URL.Path)
_, err := os.Stat(requestedPage)
if err != nil {
if err == nil {
if strings.HasSuffix(r.URL.String(), "/") {
// If request ends with a slash we want to prevent
// serving of directory index (list of fileS)
http.ServeFile(w, r, indexPage)
return
}
// Serve the file requested
serve.ServeHTTP(w, r)
return
}
if os.IsNotExist(err) {
// Forcefully serve index page on whatever
http.ServeFile(w, r, indexPage)
return
}
serve.ServeHTTP(w, r)
logger.Default().WithOptions(zap.AddStacktrace(zap.PanicLevel)).Error(
"failed to serve static file",
zap.Error(err),
zap.Stringer("url", r.URL),
)
http.Error(w, "internal server error", http.StatusInternalServerError)
}
}