now uses rice
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-10-28 17:47:37 -07:00
parent 48d663e53d
commit 7cc2628b05
11 changed files with 53 additions and 23 deletions

View File

@@ -1,7 +0,0 @@
package httpserver
import "github.com/lu4p/binclude"
//go:generate binclude -gzip
var embeddedAssets = binclude.Include("web")

View File

@@ -1,17 +1,19 @@
package httpserver
import (
"html/template"
"log"
"net/http"
"github.com/rs/zerolog/log"
)
func (s *server) handleIndex() http.HandlerFunc {
indexTemplate := template.Must(template.New("index").Parse(s.templateFiles.MustString("index.html")))
return func(w http.ResponseWriter, r *http.Request) {
content, err := s.staticFiles.ReadFile("web/index.html")
err := indexTemplate.ExecuteTemplate(w, "index", nil)
if err != nil {
log.Fatal().Err(err).Send()
log.Println(err.Error())
http.Error(w, http.StatusText(500), 500)
}
w.Write(content)
}
}

View File

@@ -0,0 +1,14 @@
package httpserver
import (
"net/http"
)
func (s *server) staticHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// sadly, the route prefix needs to be reproduced here, so it knows
// how to convert the incoming request path to the appropriate
// embedded file path
http.StripPrefix("/s", http.FileServer(s.staticFiles.HTTPBox())).ServeHTTP(w, r)
}
}

View File

@@ -10,7 +10,7 @@ import (
"syscall"
"time"
"github.com/lu4p/binclude"
rice "github.com/GeertJohan/go.rice"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
@@ -42,7 +42,8 @@ type server struct {
cancelFunc context.CancelFunc
httpServer *http.Server
router *chi.Mux
staticFiles *binclude.FileSystem
staticFiles *rice.Box
templateFiles *rice.Box
}
func NewServer(options ...func(s *server)) *server {
@@ -63,7 +64,8 @@ func Run(appname, version, buildarch string) int {
i.version = version
}
i.buildarch = buildarch
i.staticFiles = BinFS
i.staticFiles = rice.MustFindBox("static")
i.templateFiles = rice.MustFindBox("templates")
})
// this does nothing if SENTRY_DSN is unset in env.

View File

@@ -58,7 +58,7 @@ func (s *server) routes() {
s.router.Get("/", s.handleIndex())
s.router.Get("/", s.handleIndex())
s.router.Mount("/s", s.staticHandler())
s.router.Route("/api/v1", func(r chi.Router) {
r.Get("/now", s.handleNow())

View File

@@ -0,0 +1,3 @@
body {
background: purple;
}