diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a5cc12e..506de87 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -20,6 +20,21 @@ jobs: uses: actions/checkout@v2 with: fetch-depth: 0 + - + name: Include web UI + uses: robinraju/release-downloader@v1.3 + 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 diff --git a/.gitignore b/.gitignore index 5800750..25dfd5a 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ # vendor/ dist/ +resources/webui diff --git a/main/main.go b/main/main.go index 536de2d..0a79603 100644 --- a/main/main.go +++ b/main/main.go @@ -6,8 +6,11 @@ import ( "net/http" "os" "strconv" + "strings" "github.com/gorilla/mux" + + "github.com/filecoin-project/saturn-l2/resources" ) func main() { @@ -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, } @@ -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, "Saturn L2 NodeStatus: running") +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) } diff --git a/resources/resources.go b/resources/resources.go new file mode 100644 index 0000000..fd37505 --- /dev/null +++ b/resources/resources.go @@ -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