Skip to content

Commit

Permalink
refactor: using Go's embed package
Browse files Browse the repository at this point in the history
  • Loading branch information
iczc committed Aug 4, 2021
1 parent e823d6a commit 2fdbdf6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web/node_modules
web/public/build
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ RUN apk add --no-cache gcc musl-dev linux-headers
WORKDIR /backend-build

COPY . .
COPY --from=frontend /frontend-build/public ./web/public

RUN go build -o eth-faucet -ldflags "-w -s"

Expand All @@ -23,7 +24,6 @@ FROM alpine
RUN apk add --no-cache ca-certificates
WORKDIR /app

COPY --from=frontend /frontend-build/public ./web/public
COPY --from=backend /backend-build/eth-faucet .

EXPOSE 8080
Expand Down
6 changes: 4 additions & 2 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/julienschmidt/httprouter"
log "github.com/sirupsen/logrus"
"github.com/urfave/negroni"

"github.com/chainflag/eth-faucet/web"
)

type server struct {
Expand All @@ -27,11 +29,11 @@ func (s server) Run(port int) {
r.HandlerFunc("GET", "/api/info", s.infoHandler)

n := negroni.New(negroni.NewRecovery(), negroni.NewLogger())
n.Use(negroni.NewStatic(http.Dir("web/public")))
n.Use(negroni.NewStatic(web.Dist()))
n.UseHandler(r)

log.Infof("Starting http server %d", port)
http.ListenAndServe(":"+strconv.Itoa(port), n)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), n))
}

func (s server) claimHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
19 changes: 19 additions & 0 deletions web/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package web

import (
"embed"
"io/fs"
"net/http"
)

//go:embed public
var static embed.FS

func Dist() http.FileSystem {
fsys, err := fs.Sub(static, "public")
if err != nil {
panic(err)
}

return http.FS(fsys)
}

0 comments on commit 2fdbdf6

Please sign in to comment.