mirror of
https://github.com/openziti/zrok.git
synced 2024-11-22 08:03:49 +01:00
go-swagger and zrok ui coexisting (#16)
This commit is contained in:
parent
9f2c243e3d
commit
7a97a89083
@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func newZrokClient() *rest_client_zrok.Zrok {
|
||||
transport := httptransport.New(endpoint, "", nil)
|
||||
transport := httptransport.New(endpoint, "/api/v1", nil)
|
||||
transport.Producers["application/zrok.v1+json"] = runtime.JSONProducer()
|
||||
transport.Consumers["application/zrok.v1+json"] = runtime.JSONConsumer()
|
||||
return rest_client_zrok.New(transport, strfmt.Default)
|
||||
|
@ -24,7 +24,7 @@ const (
|
||||
DefaultHost string = "localhost"
|
||||
// DefaultBasePath is the default BasePath
|
||||
// found in Meta (info) section of spec file
|
||||
DefaultBasePath string = "/"
|
||||
DefaultBasePath string = "/api/v1"
|
||||
)
|
||||
|
||||
// DefaultSchemes are the default schemes found in Meta (info) section of spec file
|
||||
|
@ -4,6 +4,7 @@ package rest_server_zrok
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"github.com/openziti-test-kitchen/zrok/ui"
|
||||
"github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
|
||||
@ -52,5 +53,6 @@ func setupMiddlewares(handler http.Handler) http.Handler {
|
||||
// The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
|
||||
// So this is a good place to plug in a panic handling middleware, logging and metrics.
|
||||
func setupGlobalMiddleware(handler http.Handler) http.Handler {
|
||||
return handler
|
||||
logrus.Infof("configuring")
|
||||
return ui.StaticBuilder(handler)
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
// Schemes:
|
||||
// http
|
||||
// Host: localhost
|
||||
// BasePath: /
|
||||
// BasePath: /api/v1
|
||||
// Version: 1.0.0
|
||||
//
|
||||
// Consumes:
|
||||
|
@ -33,6 +33,7 @@ func init() {
|
||||
"title": "zrok",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"basePath": "/api/v1",
|
||||
"paths": {
|
||||
"/account": {
|
||||
"post": {
|
||||
@ -305,6 +306,7 @@ func init() {
|
||||
"title": "zrok",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"basePath": "/api/v1",
|
||||
"paths": {
|
||||
"/account": {
|
||||
"post": {
|
||||
|
@ -38,6 +38,9 @@ func (o *CreateAccountURL) Build() (*url.URL, error) {
|
||||
var _path = "/account"
|
||||
|
||||
_basePath := o._basePath
|
||||
if _basePath == "" {
|
||||
_basePath = "/api/v1"
|
||||
}
|
||||
_result.Path = golangswaggerpaths.Join(_basePath, _path)
|
||||
|
||||
return &_result, nil
|
||||
|
@ -38,6 +38,9 @@ func (o *EnableURL) Build() (*url.URL, error) {
|
||||
var _path = "/enable"
|
||||
|
||||
_basePath := o._basePath
|
||||
if _basePath == "" {
|
||||
_basePath = "/api/v1"
|
||||
}
|
||||
_result.Path = golangswaggerpaths.Join(_basePath, _path)
|
||||
|
||||
return &_result, nil
|
||||
|
@ -38,6 +38,9 @@ func (o *VersionURL) Build() (*url.URL, error) {
|
||||
var _path = "/version"
|
||||
|
||||
_basePath := o._basePath
|
||||
if _basePath == "" {
|
||||
_basePath = "/api/v1"
|
||||
}
|
||||
_result.Path = golangswaggerpaths.Join(_basePath, _path)
|
||||
|
||||
return &_result, nil
|
||||
|
@ -38,6 +38,9 @@ func (o *TunnelURL) Build() (*url.URL, error) {
|
||||
var _path = "/tunnel"
|
||||
|
||||
_basePath := o._basePath
|
||||
if _basePath == "" {
|
||||
_basePath = "/api/v1"
|
||||
}
|
||||
_result.Path = golangswaggerpaths.Join(_basePath, _path)
|
||||
|
||||
return &_result, nil
|
||||
|
@ -38,6 +38,9 @@ func (o *UntunnelURL) Build() (*url.URL, error) {
|
||||
var _path = "/untunnel"
|
||||
|
||||
_basePath := o._basePath
|
||||
if _basePath == "" {
|
||||
_basePath = "/api/v1"
|
||||
}
|
||||
_result.Path = golangswaggerpaths.Join(_basePath, _path)
|
||||
|
||||
return &_result, nil
|
||||
|
@ -3,6 +3,8 @@ info:
|
||||
title: zrok
|
||||
version: 1.0.0
|
||||
|
||||
basePath: /api/v1
|
||||
|
||||
securityDefinitions:
|
||||
key:
|
||||
type: apiKey
|
||||
|
64
ui/static.go
Normal file
64
ui/static.go
Normal file
@ -0,0 +1,64 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func StaticBuilder(handler http.Handler) http.Handler {
|
||||
logrus.Infof("building")
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasPrefix(r.URL.Path, "/api/v1") {
|
||||
logrus.Debugf("directing '%v' to api handler", r.URL.Path)
|
||||
handler.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
logrus.Debugf("directing '%v' to static handler", r.URL.Path)
|
||||
|
||||
staticPath := "build"
|
||||
indexPath := "index.html"
|
||||
|
||||
// get the absolute path to prevent directory traversal
|
||||
path, err := filepath.Abs(r.URL.Path)
|
||||
if err != nil {
|
||||
// if we failed to get the absolute path respond with a 400 bad request and stop
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// prepend the path with the path to the static directory
|
||||
path = filepath.Join(staticPath, path)
|
||||
|
||||
_, err = FS.Open(path)
|
||||
if os.IsNotExist(err) {
|
||||
// file does not exist, serve index.html
|
||||
index, err := FS.ReadFile(filepath.Join(staticPath, indexPath))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
_, _ = w.Write(index)
|
||||
return
|
||||
|
||||
} else if err != nil {
|
||||
// if we got an error (that wasn't that the file doesn't exist) stating the
|
||||
// file, return a 500 internal server error and stop
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// get the subdirectory of the static dir
|
||||
if statics, err := fs.Sub(FS, staticPath); err == nil {
|
||||
// otherwise, use http.FileServer to serve the static dir
|
||||
http.FileServer(http.FS(statics)).ServeHTTP(w, r)
|
||||
} else {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user