both production and development builds now working with vite.js (#221)

This commit is contained in:
Michael Quigley 2024-10-04 11:43:54 -04:00
parent 24ed2ec172
commit 99a0eea5d1
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
5 changed files with 81 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/openziti/zrok/agent/agentGrpc"
"github.com/openziti/zrok/agent/agentUi"
"github.com/openziti/zrok/agent/proctree"
"github.com/openziti/zrok/environment/env_core"
"github.com/openziti/zrok/sdk/golang/sdk"
@ -99,7 +100,7 @@ func (a *Agent) gateway() {
logrus.Fatalf("unable to register gateway: %v", err)
}
if err := http.ListenAndServe(":8888", cors(mux)); err != nil {
if err := http.ListenAndServe(":8888", agentUi.Middleware(mux)); err != nil {
logrus.Error(err)
}
}

6
agent/agentUi/embed.go Normal file
View File

@ -0,0 +1,6 @@
package agentUi
import "embed"
//go:embed dist
var FS embed.FS

View File

@ -0,0 +1,59 @@
package agentUi
import (
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
)
func Middleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/v1") {
handler.ServeHTTP(w, r)
return
}
staticPath := "dist"
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.gohtml
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)
}
})
}

View File

@ -11,10 +11,14 @@ function App() {
{
name: 'Token',
selector: row => row.token
},
{
name: 'Share Mode',
selector: row => row.shareMode
}
];
let api = new AgentApi(new ApiClient("http://localhost:8888"));
let api = new AgentApi(new ApiClient(window.location.protocol+'//'+window.location.host));
useEffect(() => {
let mounted = true;

View File

@ -4,4 +4,13 @@ import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
proxy: {
'/v1': {
target: 'http://localhost:8888',
changeOrigin: true,
}
}
}
})