diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..506de87 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,57 @@ +name: goreleaser + +on: + push: + # run only against tags + tags: + - '*' + +permissions: + contents: write + # packages: write + # issues: write + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - + name: Checkout + 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 + - + name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.18 + - + name: Run GoReleaser + uses: goreleaser/goreleaser-action@v2 + with: + # either 'goreleaser' (default) or 'goreleaser-pro' + distribution: goreleaser + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # Your GoReleaser Pro key, if you are using the 'goreleaser-pro' distribution + # GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }} diff --git a/.gitignore b/.gitignore index 66fd13c..25dfd5a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +dist/ +resources/webui diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..aa10aab --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,44 @@ +# This is an example .goreleaser.yml file with some sensible defaults. +# Make sure to check the documentation at https://goreleaser.com +before: + hooks: + # You may remove this if you don't use go modules. + - go mod tidy + # you may remove this if you don't need go generate + - go generate ./... +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + main: ./main + +archives: + - replacements: + darwin: Darwin + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ incpatch .Version }}-next" +changelog: + sort: asc + groups: + - title: Features + regexp: "^.*feat[(\\w)]*:+.*$" + order: 0 + - title: 'Bug fixes' + regexp: "^.*fix[(\\w)]*:+.*$" + order: 1 + - title: Others + order: 999 + filters: + exclude: + - '^docs:' + - '^test:' + 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