Skip to content

Commit

Permalink
both production and development builds now working with vite.js (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Oct 4, 2024
1 parent 24ed2ec commit 99a0eea
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
3 changes: 2 additions & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
Expand Down
6 changes: 6 additions & 0 deletions agent/agentUi/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package agentUi

import "embed"

//go:embed dist

Check failure on line 5 in agent/agentUi/embed.go

View workflow job for this annotation

GitHub Actions / Build Linux AMD64 CLI

pattern dist: no matching files found
var FS embed.FS
59 changes: 59 additions & 0 deletions agent/agentUi/middleware.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
6 changes: 5 additions & 1 deletion agent/agentUi/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions agent/agentUi/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
})

0 comments on commit 99a0eea

Please sign in to comment.