Skip to content

Commit e2934e8

Browse files
authored
Uses Go1.16 embed feature (amir20#1092)
* Uses Go1.16 embed feature * Fixes tests * Removes packr and fixes tests
1 parent 34259c4 commit e2934e8

File tree

9 files changed

+113
-94
lines changed

9 files changed

+113
-94
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ max_line_length = 120
1313
indent_style = tab
1414
indent_size = 4
1515

16+
[Makefile]
17+
indent_style = tab
18+
1619
[package.json]
1720
indent_size = 1

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Checkout code
2525
uses: actions/checkout@v2
2626
- name: Run Go Tests with Coverage
27-
run: go test -cover ./...
27+
run: make test SKIP_ASSET=1
2828
int-test:
2929
name: Integration Tests
3030
runs-on: ubuntu-latest

Makefile

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
TAG := $(shell git describe --tags)
22
PLATFROMS := linux/amd64,linux/arm/v7,linux/arm64/v8
33

4+
45
.PHONY: publish
56
publish:
67
docker buildx build --build-arg TAG=$(TAG) --platform $(PLATFROMS) -t amir20/dozzle:latest -t amir20/dozzle:$(TAG) --push .
8+
9+
.PHONY: clean
10+
clean:
11+
@rm -rf static dozzle
12+
13+
static: $(shell find assets -type f)
14+
ifdef SKIP_ASSET
15+
@echo 'Skipping yarn build'
16+
@mkdir -p static
17+
@touch static/index.html
18+
else
19+
yarn build
20+
endif
21+
22+
.PHONY: test
23+
test: static
24+
go test -cover ./...
25+
26+
build: static
27+
CGO_ENABLED=0 go build -ldflags "-s -w"

go.mod

+2-7
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,19 @@ require (
1010
github.com/docker/go-units v0.4.0 // indirect
1111
github.com/dustin/go-humanize v1.0.0
1212
github.com/fsnotify/fsnotify v1.4.9 // indirect
13-
github.com/gobuffalo/envy v1.9.0 // indirect
14-
github.com/gobuffalo/packd v1.0.0 // indirect
15-
github.com/gobuffalo/packr v1.30.1
1613
github.com/gogo/protobuf v1.3.2 // indirect
1714
github.com/golang/protobuf v1.4.3 // indirect
1815
github.com/gorilla/mux v1.8.0
19-
github.com/magefile/mage v1.11.0 // indirect
2016
github.com/magiconair/properties v1.8.4
2117
github.com/mitchellh/mapstructure v1.4.1 // indirect
2218
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
2319
github.com/morikuni/aec v1.0.0 // indirect
2420
github.com/opencontainers/go-digest v1.0.0 // indirect
2521
github.com/opencontainers/image-spec v1.0.1 // indirect
2622
github.com/pelletier/go-toml v1.8.1 // indirect
27-
github.com/rogpeppe/go-internal v1.7.0 // indirect
2823
github.com/sergi/go-diff v1.1.0 // indirect
2924
github.com/sirupsen/logrus v1.8.1
30-
github.com/spf13/afero v1.5.1 // indirect
25+
github.com/spf13/afero v1.6.0
3126
github.com/spf13/cast v1.3.1 // indirect
3227
github.com/spf13/jwalterweatherman v1.1.0 // indirect
3328
github.com/spf13/pflag v1.0.5
@@ -44,4 +39,4 @@ require (
4439
gotest.tools/v3 v3.0.3 // indirect
4540
)
4641

47-
go 1.14
42+
go 1.16

go.sum

+2-46
Large diffs are not rendered by default.

main.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main
22

33
import (
44
"context"
5+
"embed"
6+
"io/fs"
57
"net/url"
68
"os"
79
"os/signal"
@@ -11,7 +13,6 @@ import (
1113
"github.com/amir20/dozzle/docker"
1214
"github.com/amir20/dozzle/web"
1315

14-
"github.com/gobuffalo/packr"
1516
log "github.com/sirupsen/logrus"
1617
"github.com/spf13/pflag"
1718
"github.com/spf13/viper"
@@ -26,9 +27,11 @@ var (
2627
version = "dev"
2728
)
2829

30+
//go:embed static
31+
var content embed.FS
32+
2933
type handler struct {
3034
client docker.Client
31-
box packr.Box
3235
}
3336

3437
func init() {
@@ -80,15 +83,19 @@ func main() {
8083
log.Fatalf("Could not connect to Docker Engine: %v", err)
8184
}
8285

83-
box := packr.NewBox("./static")
84-
8586
config := web.Config{
8687
Addr: addr,
8788
Base: base,
8889
Version: version,
8990
TailSize: tailSize,
9091
}
91-
srv := web.CreateServer(dockerClient, box, config)
92+
93+
static, err := fs.Sub(content, "static")
94+
if err != nil {
95+
log.Fatalf("Could not open embedded static folder: %v", err)
96+
}
97+
98+
srv := web.CreateServer(dockerClient, static, config)
9299

93100
go func() {
94101
log.Infof("Accepting connections on %s", srv.Addr)

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
"version": "3.2.3",
44
"description": "Realtime log viewer for docker containers. ",
55
"scripts": {
6-
"prestart": "yarn clean",
76
"watch": "npm-run-all -p watch:*",
87
"watch:assets": "webpack --mode=development --watch",
98
"watch:server": "reflex -c .reflex",
109
"dev": "npm-run-all -p dev-server watch:server",
1110
"dev-server": "webpack serve --mode=development",
1211
"prebuild": "yarn clean",
1312
"build": "yarn webpack --mode=production",
14-
"clean": "rm -rf static/ a_main-packr.go",
13+
"clean": "rm -rf static",
1514
"release": "release-it",
1615
"test": "TZ=UTC jest",
1716
"integration": "docker-compose -f integration/docker-compose.test.yml up --build --force-recreate --exit-code-from integration",

web/routes.go

+28-14
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import (
44
"bufio"
55
"compress/gzip"
66
"context"
7+
78
"encoding/json"
89
"fmt"
910
"html/template"
1011
"io"
12+
"io/fs"
13+
"io/ioutil"
1114
"net/http"
1215
_ "net/http/pprof"
1316
"runtime"
@@ -17,7 +20,7 @@ import (
1720

1821
"github.com/amir20/dozzle/docker"
1922
"github.com/dustin/go-humanize"
20-
"github.com/gobuffalo/packr"
23+
2124
"github.com/gorilla/mux"
2225
log "github.com/sirupsen/logrus"
2326
)
@@ -31,17 +34,18 @@ type Config struct {
3134
}
3235

3336
type handler struct {
34-
client docker.Client
35-
box packr.Box
36-
config *Config
37+
client docker.Client
38+
content fs.FS
39+
config *Config
40+
fileServer http.Handler
3741
}
3842

3943
// CreateServer creates a service for http handler
40-
func CreateServer(c docker.Client, b packr.Box, config Config) *http.Server {
44+
func CreateServer(c docker.Client, content fs.FS, config Config) *http.Server {
4145
handler := &handler{
42-
client: c,
43-
box: b,
44-
config: &config,
46+
client: c,
47+
content: content,
48+
config: &config,
4549
}
4650
return &http.Server{Addr: config.Addr, Handler: createRouter(handler)}
4751
}
@@ -66,7 +70,13 @@ func createRouter(h *handler) *mux.Router {
6670
s.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux)
6771
}
6872

69-
s.PathPrefix("/").Handler(http.StripPrefix(base, http.HandlerFunc(h.index)))
73+
if base != "/" {
74+
s.PathPrefix("/").Handler(http.StripPrefix(base+"/", http.HandlerFunc(h.index)))
75+
} else {
76+
s.PathPrefix("/").Handler(http.StripPrefix(base, http.HandlerFunc(h.index)))
77+
}
78+
79+
h.fileServer = http.FileServer(http.FS(h.content))
7080

7181
return r
7282
}
@@ -79,15 +89,19 @@ func setCSPHeaders(next http.Handler) http.Handler {
7989
}
8090

8191
func (h *handler) index(w http.ResponseWriter, req *http.Request) {
82-
fileServer := http.FileServer(h.box)
83-
if h.box.Has(req.URL.Path) && req.URL.Path != "" && req.URL.Path != "/" {
84-
fileServer.ServeHTTP(w, req)
92+
_, err := h.content.Open(req.URL.Path)
93+
if err == nil && req.URL.Path != "" && req.URL.Path != "/" {
94+
h.fileServer.ServeHTTP(w, req)
8595
} else {
86-
text, err := h.box.FindString("index.html")
96+
file, err := h.content.Open("index.html")
97+
if err != nil {
98+
panic(err)
99+
}
100+
bytes, err := ioutil.ReadAll(file)
87101
if err != nil {
88102
panic(err)
89103
}
90-
tmpl, err := template.New("index.html").Parse(text)
104+
tmpl, err := template.New("index.html").Parse(string(bytes))
91105
if err != nil {
92106
panic(err)
93107
}

web/routes_test.go

+43-19
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515

1616
"github.com/amir20/dozzle/docker"
1717
"github.com/beme/abide"
18-
"github.com/gobuffalo/packr"
1918
"github.com/stretchr/testify/mock"
2019
"github.com/stretchr/testify/require"
20+
21+
"github.com/spf13/afero"
2122
)
2223

2324
type MockedClient struct {
@@ -238,10 +239,13 @@ func Test_handler_streamEvents_error_request(t *testing.T) {
238239

239240
func Test_createRoutes_index(t *testing.T) {
240241
mockedClient := new(MockedClient)
241-
box := packr.NewBox("./virtual")
242-
require.NoError(t, box.AddString("index.html", "index page"), "AddString should have no error.")
243-
244-
handler := createRouter(&handler{mockedClient, box, &Config{Base: "/"}})
242+
fs := afero.NewMemMapFs()
243+
require.NoError(t, afero.WriteFile(fs, "index.html", []byte("index page"), 0644), "WriteFile should have no error.")
244+
handler := createRouter(&handler{
245+
client: mockedClient,
246+
content: afero.NewIOFS(fs),
247+
config: &Config{Base: "/"},
248+
})
245249
req, err := http.NewRequest("GET", "/", nil)
246250
require.NoError(t, err, "NewRequest should not return an error.")
247251
rr := httptest.NewRecorder()
@@ -252,9 +256,14 @@ func Test_createRoutes_index(t *testing.T) {
252256

253257
func Test_createRoutes_redirect(t *testing.T) {
254258
mockedClient := new(MockedClient)
255-
box := packr.NewBox("./virtual")
256-
257-
handler := createRouter(&handler{mockedClient, box, &Config{Base: "/foobar"}})
259+
fs := afero.NewMemMapFs()
260+
require.NoError(t, afero.WriteFile(fs, "index.html", []byte("index page"), 0644), "WriteFile should have no error.")
261+
262+
handler := createRouter(&handler{
263+
client: mockedClient,
264+
content: afero.NewIOFS(fs),
265+
config: &Config{Base: "/foobar"},
266+
})
258267
req, err := http.NewRequest("GET", "/foobar", nil)
259268
require.NoError(t, err, "NewRequest should not return an error.")
260269
rr := httptest.NewRecorder()
@@ -265,10 +274,14 @@ func Test_createRoutes_redirect(t *testing.T) {
265274

266275
func Test_createRoutes_foobar(t *testing.T) {
267276
mockedClient := new(MockedClient)
268-
box := packr.NewBox("./virtual")
269-
require.NoError(t, box.AddString("index.html", "foo page"), "AddString should have no error.")
270-
271-
handler := createRouter(&handler{mockedClient, box, &Config{Base: "/foobar"}})
277+
fs := afero.NewMemMapFs()
278+
require.NoError(t, afero.WriteFile(fs, "index.html", []byte("foo page"), 0644), "WriteFile should have no error.")
279+
280+
handler := createRouter(&handler{
281+
client: mockedClient,
282+
content: afero.NewIOFS(fs),
283+
config: &Config{Base: "/foobar"},
284+
})
272285
req, err := http.NewRequest("GET", "/foobar/", nil)
273286
require.NoError(t, err, "NewRequest should not return an error.")
274287
rr := httptest.NewRecorder()
@@ -279,10 +292,16 @@ func Test_createRoutes_foobar(t *testing.T) {
279292

280293
func Test_createRoutes_foobar_file(t *testing.T) {
281294
mockedClient := new(MockedClient)
282-
box := packr.NewBox("./virtual")
283-
require.NoError(t, box.AddString("/test", "test page"), "AddString should have no error.")
284-
285-
handler := createRouter(&handler{mockedClient, box, &Config{Base: "/foobar"}})
295+
fs := afero.NewMemMapFs()
296+
require.NoError(t, afero.WriteFile(fs, "index.html", []byte("index page"), 0644), "WriteFile should have no error.")
297+
require.NoError(t, afero.WriteFile(fs, "test", []byte("test page"), 0644), "WriteFile should have no error.")
298+
299+
handler := createRouter(&handler{
300+
client: mockedClient,
301+
content: afero.NewIOFS(fs),
302+
config: &Config{Base: "/foobar"},
303+
fileServer: nil,
304+
})
286305
req, err := http.NewRequest("GET", "/foobar/test", nil)
287306
require.NoError(t, err, "NewRequest should not return an error.")
288307
rr := httptest.NewRecorder()
@@ -293,9 +312,14 @@ func Test_createRoutes_foobar_file(t *testing.T) {
293312

294313
func Test_createRoutes_version(t *testing.T) {
295314
mockedClient := new(MockedClient)
296-
box := packr.NewBox("./virtual")
297-
298-
handler := createRouter(&handler{mockedClient, box, &Config{Base: "/", Version: "dev"}})
315+
fs := afero.NewMemMapFs()
316+
require.NoError(t, afero.WriteFile(fs, "index.html", []byte("index page"), 0644), "WriteFile should have no error.")
317+
318+
handler := createRouter(&handler{
319+
client: mockedClient,
320+
content: afero.NewIOFS(fs),
321+
config: &Config{Base: "/", Version: "dev"},
322+
})
299323
req, err := http.NewRequest("GET", "/version", nil)
300324
require.NoError(t, err, "NewRequest should not return an error.")
301325
rr := httptest.NewRecorder()

0 commit comments

Comments
 (0)