Skip to content

Commit 4fc52b6

Browse files
authored
Replace go.rice with native go embedding (ngoduykhanh#331)
1 parent e3e3639 commit 4fc52b6

File tree

10 files changed

+60
-70
lines changed

10 files changed

+60
-70
lines changed

.github/workflows/release.yml

+1-9
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,12 @@ jobs:
4040
node-version: '14'
4141
registry-url: 'https://registry.npmjs.org'
4242

43-
# prepare assets for go rice
43+
# prepare assets
4444
- name: Prepare assets
4545
run: |
4646
chmod +x ./prepare_assets.sh
4747
./prepare_assets.sh
4848
49-
# get go rice tool
50-
- name: Get go rice tool
51-
run: go get github.com/GeertJohan/go.rice/rice
52-
53-
# run go rice embed
54-
- name: Run go rice embed
55-
run: ${HOME}/go/bin/rice embed-go
56-
5749
# build and make the releases
5850
- name: Build and make the releases
5951
uses: wangyoucao577/go-release-action@master

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ wireguard-ui
1616
vendor/
1717
assets/
1818
node_modules/
19-
rice-box.go
2019

2120
# IDEs
2221
.vscode

Dockerfile

+1-6
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,14 @@ RUN mkdir -p assets/plugins && \
4343
/build/node_modules/jquery-tags-input/ \
4444
assets/plugins/
4545

46-
# Get go modules and build tool
47-
RUN go mod download && \
48-
go get github.com/GeertJohan/go.rice/rice
49-
5046
# Add sources
5147
COPY . /build
5248

5349
# Move custom assets
5450
RUN cp -r /build/custom/ assets/
5551

5652
# Build
57-
RUN rice embed-go && \
58-
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags="-X main.gitCommit=${COMMIT}" -a -o wg-ui .
53+
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags="-X main.gitCommit=${COMMIT}" -a -o wg-ui .
5954

6055
# Release stage
6156
FROM alpine:3.16

README.md

+1-10
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,9 @@ Prepare the assets directory
211211
./prepare_assets.sh
212212
```
213213

214-
Then you can embed resources by generating Go source code
215-
216-
```sh
217-
rice embed-go
218-
go build -o wireguard-ui
219-
```
220-
221-
Or, append resources to executable as zip file
222-
214+
Then build your executable
223215
```sh
224216
go build -o wireguard-ui
225-
rice append --exec wireguard-ui
226217
```
227218

228219
## License

go.mod

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
module github.com/ngoduykhanh/wireguard-ui
22

3-
go 1.14
3+
go 1.16
44

55
require (
6-
github.com/GeertJohan/go.rice v1.0.2
76
github.com/glendc/go-external-ip v0.0.0-20170425150139-139229dcdddd
87
github.com/go-playground/universal-translator v0.17.0 // indirect
98
github.com/gorilla/sessions v1.2.0

go.sum

-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0=
2-
github.com/GeertJohan/go.rice v1.0.2 h1:PtRw+Tg3oa3HYwiDBZyvOJ8LdIyf6lAovJJtr7YOAYk=
3-
github.com/GeertJohan/go.rice v1.0.2/go.mod h1:af5vUNlDNkCjOZeSGFgIJxDje9qdjsO6hshx0gTmZt4=
41
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
5-
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
62
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
73
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
84
github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw=
@@ -12,8 +8,6 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
128
github.com/casbin/casbin/v2 v2.0.0/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
139
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
1410
github.com/coreos/bbolt v1.3.1-coreos.6.0.20180223184059-4f5275f4ebbf/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
15-
github.com/daaku/go.zipexe v1.0.0 h1:VSOgZtH418pH9L16hC/JrgSNJbbAL26pj7lmD1+CGdY=
16-
github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E=
1711
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1812
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1913
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -52,7 +46,6 @@ github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/z
5246
github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 h1:EFT6MH3igZK/dIVqgGbTqWVvkZ7wJ5iGN03SVtvvdd8=
5347
github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25/go.mod h1:sWkGw/wsaHtRsT9zGQ/WyJCotGWG/Anow/9hsAcBWRw=
5448
github.com/jessevdk/go-flags v0.0.0-20150816100521-1acbbaff2f34/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
55-
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
5649
github.com/josharian/native v0.0.0-20200817173448-b6b71def0850 h1:uhL5Gw7BINiiPAo24A2sxkcDI0Jt/sqp1v5xQCniEFA=
5750
github.com/josharian/native v0.0.0-20200817173448-b6b71def0850/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
5851
github.com/jsimonetti/rtnetlink v0.0.0-20190606172950-9527aa82566a/go.mod h1:Oz+70psSo5OFh8DBl0Zv2ACw7Esh6pPUphlvZG9x7uw=
@@ -109,7 +102,6 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
109102
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
110103
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
111104
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
112-
github.com/nkovacs/streamquote v1.0.0/go.mod h1:BN+NaZ2CmdKqUuTUXUEm9j95B2TRbpOWpxbJYzzgUsc=
113105
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
114106
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
115107
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -252,7 +244,6 @@ golang.zx2c4.com/wireguard v0.0.0-20210427022245-097af6e1351b/go.mod h1:a057zjmo
252244
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20210803171230-4253848d036c h1:ADNrRDI5NR23/TUCnEmlLZLt4u9DnZ2nwRkPrAcFvto=
253245
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20210803171230-4253848d036c/go.mod h1:+1XihzyZUBJcSc5WO9SwNA7v26puQwOEDwanaxfNXPQ=
254246
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
255-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
256247
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
257248
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
258249
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=

handler/routes.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"encoding/base64"
66
"encoding/json"
77
"fmt"
8+
"io/fs"
89
"net/http"
910
"os"
1011
"sort"
1112
"strings"
1213
"time"
1314

14-
rice "github.com/GeertJohan/go.rice"
1515
"github.com/gorilla/sessions"
1616
"github.com/labstack/echo-contrib/session"
1717
"github.com/labstack/echo/v4"
@@ -948,7 +948,7 @@ func SuggestIPAllocation(db store.IStore) echo.HandlerFunc {
948948
}
949949

950950
// ApplyServerConfig handler to write config file and restart Wireguard server
951-
func ApplyServerConfig(db store.IStore, tmplBox *rice.Box) echo.HandlerFunc {
951+
func ApplyServerConfig(db store.IStore, tmplDir fs.FS) echo.HandlerFunc {
952952
return func(c echo.Context) error {
953953

954954
server, err := db.GetServer()
@@ -976,14 +976,14 @@ func ApplyServerConfig(db store.IStore, tmplBox *rice.Box) echo.HandlerFunc {
976976
}
977977

978978
// Write config file
979-
err = util.WriteWireGuardServerConfig(tmplBox, server, clients, users, settings)
979+
err = util.WriteWireGuardServerConfig(tmplDir, server, clients, users, settings)
980980
if err != nil {
981981
log.Error("Cannot apply server config: ", err)
982982
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{
983983
false, fmt.Sprintf("Cannot apply server config: %v", err),
984984
})
985985
}
986-
986+
987987
err = util.UpdateHashes(db)
988988
if err != nil {
989989
log.Error("Cannot update hashes: ", err)
@@ -1016,4 +1016,3 @@ func AboutPage() echo.HandlerFunc {
10161016
})
10171017
}
10181018
}
1019-

main.go

+25-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package main
22

33
import (
4+
"embed"
45
"flag"
56
"fmt"
67
"github.com/labstack/echo/v4"
78
"github.com/labstack/gommon/log"
89
"github.com/ngoduykhanh/wireguard-ui/store"
10+
"io/fs"
911
"net/http"
1012
"os"
1113
"time"
1214

13-
rice "github.com/GeertJohan/go.rice"
1415
"github.com/ngoduykhanh/wireguard-ui/emailer"
1516
"github.com/ngoduykhanh/wireguard-ui/handler"
1617
"github.com/ngoduykhanh/wireguard-ui/router"
@@ -51,6 +52,16 @@ const (
5152
`
5253
)
5354

55+
// embed the "templates" directory
56+
//
57+
//go:embed templates/*
58+
var embeddedTemplates embed.FS
59+
60+
// embed the "assets" directory
61+
//
62+
//go:embed assets/*
63+
var embeddedAssets embed.FS
64+
5465
func init() {
5566

5667
// command-line flags and env variables
@@ -122,17 +133,15 @@ func main() {
122133
extraData["gitCommit"] = gitCommit
123134
extraData["basePath"] = util.BasePath
124135

125-
// create rice box for embedded template
126-
tmplBox := rice.MustFindBox("templates")
127-
128-
// rice file server for assets. "assets" is the folder where the files come from.
129-
assetHandler := http.FileServer(rice.MustFindBox("assets").HTTPBox())
136+
// strip the "templates/" prefix from the embedded directory so files can be read by their direct name (e.g.
137+
// "base.html" instead of "templates/base.html")
138+
tmplDir, _ := fs.Sub(fs.FS(embeddedTemplates), "templates")
130139

131140
// create the wireguard config on start, if it doesn't exist
132-
initServerConfig(db, tmplBox)
141+
initServerConfig(db, tmplDir)
133142

134143
// register routes
135-
app := router.New(tmplBox, extraData, util.SessionSecret)
144+
app := router.New(tmplDir, extraData, util.SessionSecret)
136145

137146
app.GET(util.BasePath, handler.WireGuardClients(db), handler.ValidSession)
138147

@@ -170,26 +179,29 @@ func main() {
170179
app.POST(util.BasePath+"/wg-server/interfaces", handler.WireGuardServerInterfaces(db), handler.ValidSession, handler.ContentTypeJson, handler.NeedsAdmin)
171180
app.POST(util.BasePath+"/wg-server/keypair", handler.WireGuardServerKeyPair(db), handler.ValidSession, handler.ContentTypeJson, handler.NeedsAdmin)
172181
app.GET(util.BasePath+"/global-settings", handler.GlobalSettings(db), handler.ValidSession, handler.NeedsAdmin)
173-
174182
app.POST(util.BasePath+"/global-settings", handler.GlobalSettingSubmit(db), handler.ValidSession, handler.ContentTypeJson, handler.NeedsAdmin)
175183
app.GET(util.BasePath+"/status", handler.Status(db), handler.ValidSession)
176184
app.GET(util.BasePath+"/api/clients", handler.GetClients(db), handler.ValidSession)
177185
app.GET(util.BasePath+"/api/client/:id", handler.GetClient(db), handler.ValidSession)
178186
app.GET(util.BasePath+"/api/machine-ips", handler.MachineIPAddresses(), handler.ValidSession)
179187
app.GET(util.BasePath+"/api/suggest-client-ips", handler.SuggestIPAllocation(db), handler.ValidSession)
180-
app.POST(util.BasePath+"/api/apply-wg-config", handler.ApplyServerConfig(db, tmplBox), handler.ValidSession, handler.ContentTypeJson)
188+
app.POST(util.BasePath+"/api/apply-wg-config", handler.ApplyServerConfig(db, tmplDir), handler.ValidSession, handler.ContentTypeJson)
181189
app.GET(util.BasePath+"/wake_on_lan_hosts", handler.GetWakeOnLanHosts(db), handler.ValidSession)
182190
app.POST(util.BasePath+"/wake_on_lan_host", handler.SaveWakeOnLanHost(db), handler.ValidSession, handler.ContentTypeJson)
183191
app.DELETE(util.BasePath+"/wake_on_lan_host/:mac_address", handler.DeleteWakeOnHost(db), handler.ValidSession, handler.ContentTypeJson)
184192
app.PUT(util.BasePath+"/wake_on_lan_host/:mac_address", handler.WakeOnHost(db), handler.ValidSession, handler.ContentTypeJson)
185193

186-
// servers other static files
194+
// strip the "assets/" prefix from the embedded directory so files can be called directly without the "assets/"
195+
// prefix
196+
assetsDir, _ := fs.Sub(fs.FS(embeddedAssets), "assets")
197+
assetHandler := http.FileServer(http.FS(assetsDir))
198+
// serves other static files
187199
app.GET(util.BasePath+"/static/*", echo.WrapHandler(http.StripPrefix(util.BasePath+"/static/", assetHandler)))
188200

189201
app.Logger.Fatal(app.Start(util.BindAddress))
190202
}
191203

192-
func initServerConfig(db store.IStore, tmplBox *rice.Box) {
204+
func initServerConfig(db store.IStore, tmplDir fs.FS) {
193205
settings, err := db.GetGlobalSettings()
194206
if err != nil {
195207
log.Fatalf("Cannot get global settings: ", err)
@@ -216,7 +228,7 @@ func initServerConfig(db store.IStore, tmplBox *rice.Box) {
216228
}
217229

218230
// write config file
219-
err = util.WriteWireGuardServerConfig(tmplBox, server, clients, users, settings)
231+
err = util.WriteWireGuardServerConfig(tmplDir, server, clients, users, settings)
220232
if err != nil {
221233
log.Fatalf("Cannot create server config: ", err)
222234
}

router/router.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package router
33
import (
44
"errors"
55
"io"
6+
"io/fs"
67
"reflect"
78
"strings"
89
"text/template"
910

10-
rice "github.com/GeertJohan/go.rice"
1111
"github.com/gorilla/sessions"
1212
"github.com/labstack/echo-contrib/session"
1313
"github.com/labstack/echo/v4"
@@ -48,57 +48,57 @@ func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c
4848
}
4949

5050
// New function
51-
func New(tmplBox *rice.Box, extraData map[string]string, secret []byte) *echo.Echo {
51+
func New(tmplDir fs.FS, extraData map[string]string, secret []byte) *echo.Echo {
5252
e := echo.New()
5353
e.Use(session.Middleware(sessions.NewCookieStore(secret)))
5454

5555
// read html template file to string
56-
tmplBaseString, err := tmplBox.String("base.html")
56+
tmplBaseString, err := util.StringFromEmbedFile(tmplDir, "base.html")
5757
if err != nil {
5858
log.Fatal(err)
5959
}
6060

61-
tmplLoginString, err := tmplBox.String("login.html")
61+
tmplLoginString, err := util.StringFromEmbedFile(tmplDir, "login.html")
6262
if err != nil {
6363
log.Fatal(err)
6464
}
6565

66-
tmplProfileString, err := tmplBox.String("profile.html")
66+
tmplProfileString, err := util.StringFromEmbedFile(tmplDir, "profile.html")
6767
if err != nil {
6868
log.Fatal(err)
6969
}
7070

71-
tmplClientsString, err := tmplBox.String("clients.html")
71+
tmplClientsString, err := util.StringFromEmbedFile(tmplDir, "clients.html")
7272
if err != nil {
7373
log.Fatal(err)
7474
}
7575

76-
tmplServerString, err := tmplBox.String("server.html")
76+
tmplServerString, err := util.StringFromEmbedFile(tmplDir, "server.html")
7777
if err != nil {
7878
log.Fatal(err)
7979
}
8080

81-
tmplGlobalSettingsString, err := tmplBox.String("global_settings.html")
81+
tmplGlobalSettingsString, err := util.StringFromEmbedFile(tmplDir, "global_settings.html")
8282
if err != nil {
8383
log.Fatal(err)
8484
}
8585

86-
tmplUsersSettingsString, err := tmplBox.String("users_settings.html")
86+
tmplUsersSettingsString, err := util.StringFromEmbedFile(tmplDir, "users_settings.html")
8787
if err != nil {
8888
log.Fatal(err)
8989
}
9090

91-
tmplStatusString, err := tmplBox.String("status.html")
91+
tmplStatusString, err := util.StringFromEmbedFile(tmplDir, "status.html")
9292
if err != nil {
9393
log.Fatal(err)
9494
}
9595

96-
tmplWakeOnLanHostsString, err := tmplBox.String("wake_on_lan_hosts.html")
96+
tmplWakeOnLanHostsString, err := util.StringFromEmbedFile(tmplDir, "wake_on_lan_hosts.html")
9797
if err != nil {
9898
log.Fatal(err)
9999
}
100100

101-
aboutPageString, err := tmplBox.String("about.html")
101+
aboutPageString, err := util.StringFromEmbedFile(tmplDir, "about.html")
102102
if err != nil {
103103
log.Fatal(err)
104104
}

0 commit comments

Comments
 (0)