Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #20 from guanzo/webui
Browse files Browse the repository at this point in the history
Include Webui
  • Loading branch information
guanzo authored Jun 7, 2022
2 parents 3ff87e7 + fa288f0 commit fb1bd39
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: 0
-
name: Include web UI
uses: robinraju/[email protected]
with:
repository: filecoin-project/saturn-l2-webui
# Update tag to deploy new web UI.
tag: v0.0.6
fileName: saturn-l2-webui.tar.gz
out-file-path: resources/webui
-
name: Unpack web UI archive
run: |
cd resources/webui
tar zxvf saturn-l2-webui.tar.gz
rm saturn-l2-webui.tar.gz
-
name: Fetch all tags
run: git fetch --force --tags
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
# vendor/

dist/
resources/webui
32 changes: 27 additions & 5 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"net/http"
"os"
"strconv"
"strings"

"github.com/gorilla/mux"

"github.com/filecoin-project/saturn-l2/resources"
)

func main() {
Expand All @@ -24,7 +27,7 @@ func main() {
}

m := mux.NewRouter()
m.Handle("/webui", http.HandlerFunc(webuiIndex))
m.PathPrefix("/webui").Handler(http.HandlerFunc(webuiHandler))
srv := &http.Server{
Handler: m,
}
Expand All @@ -48,8 +51,27 @@ func main() {
}
}

func webuiIndex(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, "<html><head><title>Saturn L2 Node</title></head><body>Status: running</body></html>")
func webuiHandler(w http.ResponseWriter, r *http.Request) {
rootDir := "webui"
path := strings.TrimPrefix(r.URL.Path, "/")

_, err := resources.WebUI.Open(path)
if path == rootDir || os.IsNotExist(err) {
// file does not exist, serve index.html
index, err := resources.WebUI.ReadFile(rootDir + "/index.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(index)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// otherwise, use http.FileServer to serve the static dir
http.FileServer(http.FS(resources.WebUI)).ServeHTTP(w, r)
}
9 changes: 9 additions & 0 deletions resources/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package resources

import "embed"

// webui folder is empty during local development, embed resources.go
// so go doesn't complain about "no embeddable files"
//
//go:embed webui resources.go
var WebUI embed.FS

0 comments on commit fb1bd39

Please sign in to comment.