Skip to content

Commit

Permalink
fix ci
Browse files Browse the repository at this point in the history
  • Loading branch information
prasunanand committed Dec 15, 2024
1 parent 4d24fab commit 565de4a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 78 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/electron-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ jobs:
brew install zeromq
- name: Build
run: go build -o ui/public/zasper app.go

run: go build -o ui/public/zasper
- name: Set up Node.js
uses: actions/setup-node@v4
with:
Expand Down
50 changes: 16 additions & 34 deletions spa.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,42 @@
//go:build webapp
// +build webapp
//go:build !webapp
// +build !webapp

package main

import (
"embed"
"io/fs"
"net/http"
"os"
"path/filepath"
)

//go:embed ui/build/*
var staticFiles embed.FS

type spaHandler struct {
staticFS embed.FS
staticPath string
indexPath string
}

func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// 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)
// Join internally call path.Clean to prevent directory traversal
path := filepath.Join(h.staticPath, r.URL.Path)

// check whether a file exists or is a directory at the given path
fi, err := os.Stat(path)
if os.IsNotExist(err) || fi.IsDir() {
// file does not exist or path is a directory, serve index.html
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
return
}
// prepend the path with the path to the static directory
path = filepath.Join(h.staticPath, path)

_, err = h.staticFS.Open(path)
if os.IsNotExist(err) {
// file does not exist, serve index.html
index, err := h.staticFS.ReadFile(filepath.Join(h.staticPath, h.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 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
statics, err := fs.Sub(h.staticFS, h.staticPath)
// otherwise, use http.FileServer to serve the static dir
http.FileServer(http.FS(statics)).ServeHTTP(w, r)
// otherwise, use http.FileServer to serve the static file
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
}

func getSpaHandler() http.Handler {
return spaHandler{staticFS: staticFiles, staticPath: "ui/build", indexPath: "index.html"}
return spaHandler{staticPath: "./ui/build", indexPath: "index.html"}
}
42 changes: 0 additions & 42 deletions spa_electron.go

This file was deleted.

61 changes: 61 additions & 0 deletions spa_webapp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//go:build webapp
// +build webapp

package main

import (
"embed"
"io/fs"
"net/http"
"os"
"path/filepath"
)

//go:embed ui/build/*
var staticFiles embed.FS

type spaHandler struct {
staticFS embed.FS
staticPath string
indexPath string
}

func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// 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(h.staticPath, path)

_, err = h.staticFS.Open(path)
if os.IsNotExist(err) {
// file does not exist, serve index.html
index, err := h.staticFS.ReadFile(filepath.Join(h.staticPath, h.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
statics, err := fs.Sub(h.staticFS, h.staticPath)
// otherwise, use http.FileServer to serve the static dir
http.FileServer(http.FS(statics)).ServeHTTP(w, r)
}

func getSpaHandler() http.Handler {
return spaHandler{staticFS: staticFiles, staticPath: "ui/build", indexPath: "index.html"}
}

0 comments on commit 565de4a

Please sign in to comment.