From 226762f8e1e70bfa1865be50e055d96556ad9be1 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Fri, 25 Sep 2020 02:53:41 +0200 Subject: [PATCH] Embed static admin resources on build, add `goreleaser` config --- .dockerignore | 1 + .gitignore | 2 ++ .goreleaser.yml | 32 ++++++++++++++++++++++++++++++++ Dockerfile | 5 ++--- Makefile | 20 ++++++++++++++++++++ cmd/{ => hetty}/main.go | 35 +++++++++++++++++++++-------------- go.mod | 3 ++- go.sum | 17 +++++++++++++++++ modd.conf | 2 +- 9 files changed, 98 insertions(+), 19 deletions(-) create mode 100644 .gitignore create mode 100644 .goreleaser.yml create mode 100644 Makefile rename cmd/{ => hetty}/main.go (69%) diff --git a/.dockerignore b/.dockerignore index a25e622..d037211 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,4 @@ +**/rice-box.go /admin/.env /admin/.next /admin/dist diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5f72d24 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +**/rice-box.go +dist \ No newline at end of file diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..899ac85 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,32 @@ +before: + hooks: + - make clean + - go mod download + - go generate ./... +builds: + - main: ./cmd/hetty + env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + hooks: + pre: make embed +archives: + - replacements: + darwin: Darwin + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 +checksum: + name_template: "checksums.txt" +snapshot: + name_template: "{{ .Tag }}-next" +changelog: + sort: asc + filters: + exclude: + - "^docs:" + - "^test:" diff --git a/Dockerfile b/Dockerfile index 1244f49..3754ec7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,8 +8,7 @@ COPY go.mod go.sum ./ RUN go mod download COPY cmd ./cmd COPY pkg ./pkg -COPY gqlgen.yml . -RUN go build -o hetty ./cmd/ +RUN go build ./cmd/hetty FROM node:${NODE_VERSION}-alpine AS node-builder WORKDIR /app @@ -24,6 +23,6 @@ WORKDIR /app COPY --from=go-builder /app/hetty . COPY --from=node-builder /app/dist admin -ENTRYPOINT ["./hetty"] +ENTRYPOINT ["./hetty", "-adminPath=./admin"] EXPOSE 80 \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7455569 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +setup: + go mod download + go generate ./... +.PHONY: setup + +embed: + go install github.com/GeertJohan/go.rice/rice + cd cmd/hetty && rice embed-go +.PHONY: embed + +build: embed + go build ./cmd/hetty +.PHONY: build + +clean: + rm -rf cmd/hetty/rice-box.go +.PHONY: clean + +release: + goreleaser -p 1 \ No newline at end of file diff --git a/cmd/main.go b/cmd/hetty/main.go similarity index 69% rename from cmd/main.go rename to cmd/hetty/main.go index 369e3b9..086648c 100644 --- a/cmd/main.go +++ b/cmd/hetty/main.go @@ -1,4 +1,4 @@ -package main +package hetty import ( "crypto/tls" @@ -10,6 +10,7 @@ import ( "os" "strings" + rice "github.com/GeertJohan/go.rice" "github.com/dstotijn/hetty/pkg/api" "github.com/dstotijn/hetty/pkg/proxy" "github.com/dstotijn/hetty/pkg/reqlog" @@ -20,17 +21,20 @@ import ( ) var ( - caCertFile = flag.String("cert", "", "CA certificate file path") - caKeyFile = flag.String("key", "", "CA private key file path") - dev = flag.Bool("dev", false, "Run in development mode") - addr = flag.String("addr", ":80", "TCP address to listen on, in the form \"host:port\"") - adminPath = flag.String("adminPath", "./admin/dist", "File path to admin build") + caCertFile string + caKeyFile string + addr string + adminPath string ) func main() { + flag.StringVar(&caCertFile, "cert", "", "CA certificate file path") + flag.StringVar(&caKeyFile, "key", "", "CA private key file path") + flag.StringVar(&addr, "addr", ":80", "TCP address to listen on, in the form \"host:port\"") + flag.StringVar(&adminPath, "adminPath", "", "File path to admin build") flag.Parse() - tlsCA, err := tls.LoadX509KeyPair(*caCertFile, *caKeyFile) + tlsCA, err := tls.LoadX509KeyPair(caCertFile, caKeyFile) if err != nil { log.Fatalf("[FATAL] Could not load CA key pair: %v", err) } @@ -51,12 +55,15 @@ func main() { p.UseResponseModifier(reqLogService.ResponseModifier) var adminHandler http.Handler - - if !*dev { - if *adminPath == "" { - *adminPath = "./admin/dist" + if adminPath == "" { + // Use + box, err := rice.FindBox("../../admin/dist") + if err != nil { + log.Fatalf("[FATAL] Could not find embedded admin resources: %v", err) } - adminHandler = http.FileServer(http.Dir(*adminPath)) + adminHandler = http.FileServer(box.HTTPBox()) + } else { + adminHandler = http.FileServer(http.Dir(adminPath)) } router := mux.NewRouter().SkipClean(true) @@ -80,12 +87,12 @@ func main() { router.PathPrefix("").Handler(p) s := &http.Server{ - Addr: *addr, + Addr: addr, Handler: router, TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){}, // Disable HTTP/2 } - log.Printf("[INFO] Running server on %v ...", *addr) + log.Printf("[INFO] Running server on %v ...", addr) err = s.ListenAndServe() if err != nil && err != http.ErrServerClosed { log.Fatalf("[FATAL] HTTP server closed: %v", err) diff --git a/go.mod b/go.mod index 4caed08..ec29099 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,10 @@ module github.com/dstotijn/hetty -go 1.13 +go 1.15 require ( github.com/99designs/gqlgen v0.11.3 + github.com/GeertJohan/go.rice v1.0.0 github.com/google/uuid v1.1.2 github.com/gorilla/mux v1.7.4 github.com/vektah/gqlparser/v2 v2.0.1 diff --git a/go.sum b/go.sum index 51b04f2..72fd71e 100644 --- a/go.sum +++ b/go.sum @@ -1,16 +1,24 @@ github.com/99designs/gqlgen v0.11.3 h1:oFSxl1DFS9X///uHV3y6CEfpcXWrDUxVblR4Xib2bs4= github.com/99designs/gqlgen v0.11.3/go.mod h1:RgX5GRRdDWNkh4pBrdzNpNPFVsdoUFY2+adM6nb1N+4= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +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/agnivade/levenshtein v1.0.1 h1:3oJU7J3FGFmyhn8KHjmVaZCN5hxTr7GxgRue+sxIXdQ= github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM= github.com/agnivade/levenshtein v1.0.3 h1:M5ZnqLOoZR8ygVq0FfkXsNOKzMCk0xRiow0R5+5VkQ0= github.com/agnivade/levenshtein v1.0.3/go.mod h1:4SFRZbbXWLF4MU1T9Qg0pGgH3Pjs+t6ie5efyrwRJXs= +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/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q= github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +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= @@ -20,6 +28,7 @@ github.com/go-chi/chi v3.3.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxm github.com/gogo/protobuf v1.0.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/goreleaser/goreleaser v0.143.0 h1:O/oQFIdg/nGso0iQ7/W4J4aqsERWvkDhnFzcVcpIBOs= github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= github.com/gorilla/mux v1.6.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= @@ -28,6 +37,8 @@ github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTM github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +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/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -41,6 +52,8 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mitchellh/mapstructure v0.0.0-20180203102830-a4e142e9c047 h1:zCoDWFD5nrJJVjbXiDZcVhOBSzKn3o9LgRLLMRNuru8= github.com/mitchellh/mapstructure v0.0.0-20180203102830-a4e142e9c047/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +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/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= @@ -62,6 +75,10 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +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/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8= +github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e h1:+w0Zm/9gaWpEAyDlU1eKOuk5twTjAjuevXqcJJw8hrg= github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e/go.mod h1:/HUdMve7rvxZma+2ZELQeNh88+003LL7Pf/CZ089j8U= github.com/vektah/gqlparser/v2 v2.0.1 h1:xgl5abVnsd4hkN9rk65OJID9bfcLSMuTaTcZj777q1o= diff --git a/modd.conf b/modd.conf index 6a4f4ae..9c24fce 100644 --- a/modd.conf +++ b/modd.conf @@ -4,7 +4,7 @@ @addr = :8080 **/*.go { - daemon +sigterm: go run ./cmd \ + daemon +sigterm: go run ./cmd/hetty \ -cert=@cert \ -key=@key \ -dev=@dev \