Skip to content

Commit

Permalink
move static web files from rakyll/statik to go:embed
Browse files Browse the repository at this point in the history
There is no need for the rakyll/statik package starting with Go 1.16,
which provides us with tools for embedding files
without third-party libraries.
  • Loading branch information
paskal committed Aug 3, 2022
1 parent 86d059b commit 8cadfe4
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 568 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ target
/logs/
/target/
/var/
/web/
debug
debug.test
.vscode
Expand All @@ -18,7 +17,7 @@ remark42
/bin/
/backend/var/
/backend/app/var/
/backend/web/
/backend/app/cmd/web/
/backend/*.html.tmpl
compose-private-backend.yml
compose-private-frontend.yml
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ ARG SKIP_BACKEND_TEST
ARG BACKEND_TEST_TIMEOUT

ADD backend /build/backend
# to embed the frontend files statically into Remark42 binary
COPY --from=build-frontend /srv/frontend/apps/remark42/public/ /build/backend/app/cmd/web/
RUN find /build/backend/app/cmd/web/ -regex '.*\.\(html\|js\|mjs\)$' -print -exec sed -i "s|{% REMARK_URL %}|http://127.0.0.1:8080|g" {} \;
WORKDIR /build/backend

# install gcc in order to be able to go test package with -race
Expand Down Expand Up @@ -108,6 +111,5 @@ RUN ln -s /srv/remark42 /usr/bin/remark42
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s CMD curl --fail http://localhost:8080/ping || exit 1


RUN chmod +x /srv/init.sh
CMD ["/srv/remark42", "server"]
9 changes: 2 additions & 7 deletions Dockerfile.artifacts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@ ADD backend /build/backend
ADD README.md /build/
ADD LICENSE /build/

COPY --from=build-frontend /srv/frontend/apps/remark42/public/ /build/backend/web/
COPY --from=build-frontend /srv/frontend/apps/remark42/public/ /build/backend/app/cmd/web/

RUN \
export WEB_ROOT=/build/backend/web && \
find . -regex '.*\.\(html\|js\|mjs\)$' -print -exec sed -i "s|{% REMARK_URL %}|http://127.0.0.1:8080|g" {} \; && \
statik --src=${WEB_ROOT} --dest=/build/backend/app/rest -p api -f && \
ls -la /build/backend/app/rest/api/statik.go && \
ls -la /build/backend/web/
RUN find /build/backend/app/cmd/web/ -regex '.*\.\(html\|js\|mjs\)$' -print -exec sed -i "s|{% REMARK_URL %}|http://127.0.0.1:8080|g" {} \;

RUN \
version=$("/script/version.sh") && echo "version=${version}" && \
Expand Down
5 changes: 5 additions & 0 deletions backend/app/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"embed"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -41,6 +42,9 @@ import (
"github.com/umputun/remark42/backend/app/templates"
)

//go:embed web
var webFS embed.FS

// ServerCommand with command line flags and env
type ServerCommand struct {
Store StoreGroup `group:"store" namespace:"store" env-namespace:"STORE"`
Expand Down Expand Up @@ -556,6 +560,7 @@ func (s *ServerCommand) newServerApp(ctx context.Context) (*serverApp, error) {
Version: s.Revision,
DataService: dataService,
WebRoot: s.WebRoot,
WebFS: webFS,
RemarkURL: s.RemarkURL,
ImageProxy: imgProxy,
CommentFormatter: commentFormatter,
Expand Down
1 change: 1 addition & 0 deletions backend/app/cmd/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This stub page would be replaced by the frontend statically built HTML during the Docker image build.
39 changes: 18 additions & 21 deletions backend/app/rest/api/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package api
import (
"bytes"
"context"
"embed"
"encoding/json"
"fmt"
"io/fs"
"net/http"
"net/mail"
"os"
"regexp"
"strings"
"sync"
Expand All @@ -23,7 +26,6 @@ import (
log "github.com/go-pkgz/lgr"
R "github.com/go-pkgz/rest"
"github.com/go-pkgz/rest/logger"
"github.com/rakyll/statik/fs"

"github.com/umputun/remark42/backend/app/notify"
"github.com/umputun/remark42/backend/app/rest"
Expand All @@ -49,6 +51,7 @@ type Rest struct {

AnonVote bool
WebRoot string
WebFS embed.FS
RemarkURL string
ReadOnlyAge int
SharedSecret string
Expand Down Expand Up @@ -351,8 +354,8 @@ func (s *Rest) routes() chi.Router {
rroot.Post("/email/unsubscribe.html", s.privRest.emailUnsubscribeCtrl)
})

// file server for static content from /web
addFileServer(router, "/web", http.Dir(s.WebRoot), s.Version)
// file server for static content from s.WebRoot on path /web
addFileServer(router, s.WebFS, s.WebRoot, s.Version)
return router
}

Expand Down Expand Up @@ -463,34 +466,28 @@ func (s *Rest) configCtrl(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, cnf)
}

// serves static files from /web or embedded by statik
func addFileServer(r chi.Router, path string, root http.FileSystem, version string) {
// serves static files from the webRoot directory or files embedded into the compiled binary if that directory is absent
func addFileServer(r chi.Router, embedFS embed.FS, webRoot, version string) {
var webFS http.Handler

statikFS, err := fs.New()
if err != nil {
log.Printf("[DEBUG] no embedded assets loaded, %s", err)
log.Printf("[INFO] run file server for %s, path %s", root, path)
webFS = http.FileServer(root)
if _, err := os.Stat(webRoot); err == nil {
log.Printf("[INFO] run file server from %s from the disk", webRoot)
webFS = http.FileServer(http.Dir(webRoot))
} else {
log.Printf("[INFO] run file server for %s, embedded", root)
webFS = http.FileServer(statikFS)
log.Printf("[INFO] run file server, embedded")
var contentFS, _ = fs.Sub(embedFS, "web")
webFS = http.FileServer(http.FS(contentFS))
}

origPath := path
webFS = http.StripPrefix(path, webFS)
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
path += "/"
}
path += "*"
webFS = http.StripPrefix("/web", webFS)
r.Get("/web", http.RedirectHandler("/web/", http.StatusMovedPermanently).ServeHTTP)

r.With(tollbooth_chi.LimitHandler(tollbooth.NewLimiter(20, nil)),
middleware.Timeout(10*time.Second),
cacheControl(time.Hour, version),
).Get(path, func(w http.ResponseWriter, r *http.Request) {
).Get("/web/*", func(w http.ResponseWriter, r *http.Request) {
// don't show dirs, just serve files
if strings.HasSuffix(r.URL.Path, "/") && len(r.URL.Path) > 1 && r.URL.Path != (origPath+"/") {
if strings.HasSuffix(r.URL.Path, "/") && len(r.URL.Path) > 1 && r.URL.Path != ("/web/") {
http.NotFound(w, r)
return
}
Expand Down
1 change: 0 additions & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/jessevdk/go-flags v1.5.0
github.com/kyokomi/emoji/v2 v2.2.9
github.com/microcosm-cc/bluemonday v1.0.19
github.com/rakyll/statik v0.1.7
github.com/rs/xid v1.4.0
github.com/russross/blackfriday/v2 v2.1.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
Expand Down
2 changes: 0 additions & 2 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=
Expand Down
202 changes: 0 additions & 202 deletions backend/vendor/github.com/rakyll/statik/LICENSE

This file was deleted.

Loading

0 comments on commit 8cadfe4

Please sign in to comment.