From c19f9d0eb27ac12aa61c893b39e6bc54ad8e4dea Mon Sep 17 00:00:00 2001 From: sneak Date: Wed, 28 Oct 2020 16:46:22 -0700 Subject: [PATCH] seems to embed files --- .gitignore | 2 +- Makefile | 5 ++++- go.mod | 2 ++ go.sum | 8 ++++++++ httpserver/embed.go | 7 +++++++ httpserver/handleindex.go | 9 +++++++-- httpserver/main.go | 3 +++ httpserver/web/index.html | 12 ++++++++++++ 8 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 httpserver/embed.go create mode 100644 httpserver/web/index.html diff --git a/.gitignore b/.gitignore index bc8d7f1..f2d18cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /httpd debug.log /.env - +binclude.go diff --git a/Makefile b/Makefile index 5014ac0..d9aeef5 100644 --- a/Makefile +++ b/Makefile @@ -42,4 +42,7 @@ docker: docker build --progress plain . ./$(FN)d: cmd/$(FN)d/main.go */*.go - cd cmd/$(FN)d && go build -o ../../$(FN)d $(GOFLAGS) . + cd httpserver && \ + go generate && \ + cd ../cmd/$(FN)d && \ + go build -o ../../$(FN)d $(GOFLAGS) . diff --git a/go.mod b/go.mod index abc52d8..5c9935c 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,8 @@ 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/markbates/pkger v0.17.1 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 280f7a3..7a0e9b5 100644 --- a/go.sum +++ b/go.sum @@ -102,6 +102,8 @@ github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTM github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI= +github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM= github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= @@ -218,9 +220,13 @@ 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= +github.com/markbates/pkger v0.17.1 h1:/MKEtWqtc0mZvu9OinB9UzVN9iYCwLWuyUv4Bw+PCno= +github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= @@ -304,6 +310,7 @@ 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= @@ -548,6 +555,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= diff --git a/httpserver/embed.go b/httpserver/embed.go new file mode 100644 index 0000000..4b4e4e4 --- /dev/null +++ b/httpserver/embed.go @@ -0,0 +1,7 @@ +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 ba07e6f..d83a12a 100644 --- a/httpserver/handleindex.go +++ b/httpserver/handleindex.go @@ -1,12 +1,17 @@ package httpserver import ( - "fmt" "net/http" + + "github.com/rs/zerolog/log" ) func (s *server) handleIndex() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "hello world") + content, err := s.staticFiles.ReadFile("web/index.html") + if err != nil { + log.Fatal().Err(err).Send() + } + w.Write(content) } } diff --git a/httpserver/main.go b/httpserver/main.go index 11da37b..63e3323 100644 --- a/httpserver/main.go +++ b/httpserver/main.go @@ -10,6 +10,7 @@ import ( "syscall" "time" + "github.com/lu4p/binclude" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/viper" @@ -41,6 +42,7 @@ type server struct { cancelFunc context.CancelFunc httpServer *http.Server router *chi.Mux + staticFiles *binclude.FileSystem } func NewServer(options ...func(s *server)) *server { @@ -61,6 +63,7 @@ func Run(appname, version, buildarch string) int { i.version = version } i.buildarch = buildarch + i.staticFiles = BinFS }) // this does nothing if SENTRY_DSN is unset in env. diff --git a/httpserver/web/index.html b/httpserver/web/index.html new file mode 100644 index 0000000..8735e61 --- /dev/null +++ b/httpserver/web/index.html @@ -0,0 +1,12 @@ + + + + + Title + + + +

Hello, world!

+ + +