Skip to content

Commit

Permalink
ui: v0.15.0, use ui http.Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Jun 16, 2023
1 parent 58ff5b5 commit 1bfce3b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 76 deletions.
3 changes: 2 additions & 1 deletion cmd/hostd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"go.sia.tech/hostd/api"
"go.sia.tech/hostd/build"
"go.sia.tech/jape"
"go.sia.tech/web/hostd"
"go.uber.org/zap"
"golang.org/x/term"
)
Expand Down Expand Up @@ -172,7 +173,7 @@ func main() {
web := http.Server{
Handler: webRouter{
api: auth(api.NewServer(hostKey.PublicKey(), node.a, node.g, node.cm, node.tp, node.contracts, node.storage, node.metrics, node.store, node.settings, node.w, logger.Named("api"))),
ui: createUIHandler(),
ui: hostd.Handler(),
},
ReadTimeout: 30 * time.Second,
}
Expand Down
72 changes: 0 additions & 72 deletions cmd/hostd/web.go
Original file line number Diff line number Diff line change
@@ -1,83 +1,11 @@
package main

import (
"errors"
"fmt"
"io"
"io/fs"
"net/http"
_ "net/http/pprof"
"path"
"path/filepath"
"strings"
"time"

"go.sia.tech/web/hostd/ui"
)

type uiRouter struct {
fs fs.FS
}

func (cr *uiRouter) serveErrorPage(status int, w http.ResponseWriter, r *http.Request) {
errorPath := fmt.Sprintf("%d.html", status)

errorPage, err := cr.fs.Open(errorPath)
if err != nil {
http.Error(w, http.StatusText(status), status)
return
}
defer errorPage.Close()

w.WriteHeader(status)
io.Copy(w, errorPage)
}

func (cr *uiRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fp := strings.Trim(r.URL.Path, "/")
if fp == "" {
fp = "index.html" // serve index.html at /
}

// If the path is a file, it is served directly. If the file does not exist,
// it will check the path with .html appended.
//
// If the path is a directory, it will try to serve index.html file in that
// directory. If an index.html file does not exist in the directory, it will
// try to serve index.html file in the parent directory. Does not traverse
// the filesystem more than one level to enable serving 404 pages.
tryFiles := []string{
fp, // try the exact name first
fp + ".html", // try the exact name with .html appended (/contracts serves /contracts.html)
filepath.Join(fp, "index.html"), // try the name as a directory (/volumes serves /volumes/index.html)
filepath.Join(path.Dir(fp), "index.html"), // try the parent directory, (supports path params e.g. /volumes/:id)
}

// try each file in order
for _, try := range tryFiles {
f, err := cr.fs.Open(try)
switch {
case err == nil:
defer f.Close()
http.ServeContent(w, r, try, time.Time{}, f.(io.ReadSeeker))
return
case !errors.Is(err, fs.ErrNotExist):
cr.serveErrorPage(http.StatusInternalServerError, w, r)
return
}
}
// no matching file found
cr.serveErrorPage(http.StatusNotFound, w, r)
}

func createUIHandler() http.Handler {
assets, err := fs.Sub(ui.Assets, "assets")
if err != nil {
panic(err)
}
return &uiRouter{assets}
}

type webRouter struct {
ui http.Handler
api http.Handler
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
go.sia.tech/jape v0.9.1-0.20230525021720-ecf031ecbffb
go.sia.tech/renterd v0.0.0-20230322105544-b8424a111b76
go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca
go.sia.tech/web/hostd v0.14.0
go.sia.tech/web/hostd v0.15.0
go.uber.org/zap v1.24.0
golang.org/x/sys v0.7.0
golang.org/x/term v0.7.0
Expand Down Expand Up @@ -67,6 +67,7 @@ require (
go.opentelemetry.io/otel/trace v1.13.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.sia.tech/mux v1.2.0 // indirect
go.sia.tech/web v0.0.0-20230616170703-7ed0b639fb22 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.5.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,10 @@ go.sia.tech/renterd v0.0.0-20230322105544-b8424a111b76 h1:W+xv7IP1bjt+jtRCIgP5Ci
go.sia.tech/renterd v0.0.0-20230322105544-b8424a111b76/go.mod h1:TUw/Zn68MYQZbP5MU7wQy6K9aLi8uCoIVCNVVBQ+cF4=
go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca h1:aZMg2AKevn7jKx+wlusWQfwSM5pNU9aGtRZme29q3O4=
go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca/go.mod h1:h/1afFwpxzff6/gG5i1XdAgPK7dEY6FaibhK7N5F86Y=
go.sia.tech/web/hostd v0.14.0 h1:km2eM+JyucTARFg7nnYVXZyuYYr3TY4WYuA0bJNHTJ8=
go.sia.tech/web/hostd v0.14.0/go.mod h1:TZMNvx5bM/hABia/9SmXFINTaInp+mCkhGUZinCl03Y=
go.sia.tech/web v0.0.0-20230616170703-7ed0b639fb22 h1:8R5XuEh1rfWYzWiZ3vWJ6q8be3XoDZ+OD+ryxSKM/w0=
go.sia.tech/web v0.0.0-20230616170703-7ed0b639fb22/go.mod h1:RKODSdOmR3VtObPAcGwQqm4qnqntDVFylbvOBbWYYBU=
go.sia.tech/web/hostd v0.15.0 h1:AMN8bLkbiiUgI0hgJ1+9CgG5P/XdPoe4u4fMgZaXoRs=
go.sia.tech/web/hostd v0.15.0/go.mod h1:nZf2Ubbd5ecUjEzlZPlwIc7ZIf+iVosgmLDBymQtzTM=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
Expand Down

0 comments on commit 1bfce3b

Please sign in to comment.