This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
package httpserver
|
||||
|
||||
import "github.com/lu4p/binclude"
|
||||
|
||||
//go:generate binclude -gzip
|
||||
|
||||
var embeddedAssets = binclude.Include("web")
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
14
httpserver/handlestatic.go
Normal file
14
httpserver/handlestatic.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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())
|
||||
|
||||
3
httpserver/static/css/style.css
Normal file
3
httpserver/static/css/style.css
Normal file
@@ -0,0 +1,3 @@
|
||||
body {
|
||||
background: purple;
|
||||
}
|
||||
Reference in New Issue
Block a user