From 7cc2628b056801bf787b5be9a57a26594ebfdea3 Mon Sep 17 00:00:00 2001 From: sneak Date: Wed, 28 Oct 2020 17:47:37 -0700 Subject: [PATCH] now uses rice --- .gitignore | 1 - Makefile | 10 ++++++++-- go.mod | 2 +- go.sum | 17 ++++++++++++++--- httpserver/embed.go | 7 ------- httpserver/handleindex.go | 12 +++++++----- httpserver/handlestatic.go | 14 ++++++++++++++ httpserver/main.go | 8 +++++--- httpserver/routes.go | 2 +- httpserver/static/css/style.css | 3 +++ httpserver/{web => templates}/index.html | 0 11 files changed, 53 insertions(+), 23 deletions(-) delete mode 100644 httpserver/embed.go create mode 100644 httpserver/handlestatic.go create mode 100644 httpserver/static/css/style.css rename httpserver/{web => templates}/index.html (100%) diff --git a/.gitignore b/.gitignore index f2d18cd..09c78c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ /httpd debug.log /.env -binclude.go diff --git a/Makefile b/Makefile index d9aeef5..976b1e9 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,12 @@ docker: ./$(FN)d: cmd/$(FN)d/main.go */*.go cd httpserver && \ - go generate && \ cd ../cmd/$(FN)d && \ - go build -o ../../$(FN)d $(GOFLAGS) . + go build -o ../../$(FN)d $(GOFLAGS) . && \ + cd ../.. && \ + rice append -i ./httpserver --exec ./httpd + +tools: + go get -v github.com/golangci/golangci-lint/cmd/golangci-lint@v1.31.0 + go get -v mvdan.cc/gofumpt/gofumports + go get -v github.com/GeertJohan/go.rice/rice diff --git a/go.mod b/go.mod index 77309ea..32c2f43 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.15 require ( github.com/99designs/basicauth-go v0.0.0-20160802081356-2a93ba0f464d + github.com/GeertJohan/go.rice v1.0.0 github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/getsentry/sentry-go v0.7.0 github.com/go-chi/chi v4.1.2+incompatible @@ -12,7 +13,6 @@ require ( github.com/google/go-cmp v0.5.2 // indirect github.com/joho/godotenv v1.3.0 github.com/kr/text v0.2.0 // indirect - github.com/lu4p/binclude v0.0.0-20200904145119-ea41c9610f22 github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.6.0 diff --git a/go.sum b/go.sum index 9393a67..df8335a 100644 --- a/go.sum +++ b/go.sum @@ -20,11 +20,17 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/CloudyKit/fastprinter v0.0.0-20170127035650-74b38d55f37a/go.mod h1:EFZQ978U7x8IRnstaskI3IysnWY5Ao3QgZUKOXlsAdw= github.com/CloudyKit/jet v2.1.3-0.20180809161101-62edd43e4f88+incompatible/go.mod h1:HPYO+50pSWkPoj9Q/eq0aRGByCL6ScRlUmiEX5Zgm+w= +github.com/GeertJohan/go.incremental v1.0.0 h1:7AH+pY1XUgQE4Y1HcXYaMqAI0m9yrFqo/jt0CW30vsg= +github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0= +github.com/GeertJohan/go.rice v1.0.0 h1:KkI6O9uMaQU3VEKaj01ulavtF7o1fWT7+pk/4voiMLQ= +github.com/GeertJohan/go.rice v1.0.0/go.mod h1:eH6gbSOAUv07dQuZVnBmoDP8mgsM1rtixis4Tib9if0= github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY= github.com/Joker/jade v1.0.1-0.20190614124447-d475f43051e7/go.mod h1:6E6s8o2AE4KhCrqr6GRJjdC/gNfTdxkIXvuGZZda2VM= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0= github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= +github.com/akavel/rsrc v0.8.0 h1:zjWn7ukO9Kc5Q62DOJCcxGpXC18RawVtYAGdz2aLlfw= +github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= @@ -57,6 +63,8 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7 github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/daaku/go.zipexe v1.0.0 h1:VSOgZtH418pH9L16hC/JrgSNJbbAL26pj7lmD1+CGdY= +github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -181,6 +189,8 @@ github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbk github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0= github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62A0xJL6I+umB2YTlFRwWXaDFA0jy+5HzGiJjqI= github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw= +github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= @@ -218,8 +228,6 @@ github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvf github.com/labstack/echo/v4 v4.1.16/go.mod h1:awO+5TzAjvL8XpibdsfXxPgHr+orhtXZJZIQCVjogKI= github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= -github.com/lu4p/binclude v0.0.0-20200904145119-ea41c9610f22 h1:9sP18Im+BpVri6LSXbcmjELACb4U6JEC60D5Dorspjc= -github.com/lu4p/binclude v0.0.0-20200904145119-ea41c9610f22/go.mod h1:VSEp4ypMheHdOuVGdsSyLuzpKmL0D1wTD43hsN+u4YY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= @@ -260,6 +268,8 @@ github.com/nats-io/nkeys v0.0.2/go.mod h1:dab7URMsZm6Z/jp9Z5UGa87Uutgc2mVpXLC4B7 github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229 h1:E2B8qYyeSgv5MXpmzZXRNp8IAQ4vjxIjhpAf5hv/tAg= +github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -306,7 +316,6 @@ github.com/prometheus/procfs v0.0.11/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4 github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs= github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo= @@ -358,9 +367,11 @@ github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVM github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.6.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w= github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= +github.com/valyala/fasttemplate v1.1.0 h1:RZqt0yGBsps8NGvLSGW804QQqCUYYLsaOjTVHy1Ocw4= github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= diff --git a/httpserver/embed.go b/httpserver/embed.go deleted file mode 100644 index 4b4e4e4..0000000 --- a/httpserver/embed.go +++ /dev/null @@ -1,7 +0,0 @@ -package httpserver - -import "github.com/lu4p/binclude" - -//go:generate binclude -gzip - -var embeddedAssets = binclude.Include("web") diff --git a/httpserver/handleindex.go b/httpserver/handleindex.go index d83a12a..4cb4379 100644 --- a/httpserver/handleindex.go +++ b/httpserver/handleindex.go @@ -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) } } diff --git a/httpserver/handlestatic.go b/httpserver/handlestatic.go new file mode 100644 index 0000000..9e6cbfb --- /dev/null +++ b/httpserver/handlestatic.go @@ -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) + } +} diff --git a/httpserver/main.go b/httpserver/main.go index 63e3323..9b79ae7 100644 --- a/httpserver/main.go +++ b/httpserver/main.go @@ -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. diff --git a/httpserver/routes.go b/httpserver/routes.go index 861bafa..9e828ec 100644 --- a/httpserver/routes.go +++ b/httpserver/routes.go @@ -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()) diff --git a/httpserver/static/css/style.css b/httpserver/static/css/style.css new file mode 100644 index 0000000..e062825 --- /dev/null +++ b/httpserver/static/css/style.css @@ -0,0 +1,3 @@ +body { + background: purple; +} diff --git a/httpserver/web/index.html b/httpserver/templates/index.html similarity index 100% rename from httpserver/web/index.html rename to httpserver/templates/index.html