Skip to content

Commit

Permalink
Replace go.rice module with std embed
Browse files Browse the repository at this point in the history
  • Loading branch information
wader authored and mildred committed Apr 8, 2024
1 parent baea0cb commit 98a1cca
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 159 deletions.
2 changes: 1 addition & 1 deletion fileserver/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func localRedirect(w http.ResponseWriter, r *http.Request, newPath string) {
// To use the operating system's file system implementation,
// use http.Dir:
//
// http.Handle("/", &fileserver.FileServer{Root: http.Dir("/tmp")})
// http.Handle("/", &fileserver.FileServer{Root: http.Dir("/tmp")})
type FileServer struct {
Version string
Root http.FileSystem
Expand Down
20 changes: 10 additions & 10 deletions fileserver/fileserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
"testing"
"time"

rice "github.com/GeertJohan/go.rice"
"github.com/cortesi/devd/fstmpl"
"github.com/cortesi/devd/inject"
"github.com/cortesi/devd/ricetemp"
"github.com/cortesi/devd/routespec"
"github.com/cortesi/devd/templates"
"github.com/cortesi/termlog"
)

Expand All @@ -40,7 +40,7 @@ func ServeFile(w http.ResponseWriter, r *http.Request, name string) {
"version",
http.Dir(dir),
inject.CopyInject{},
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
fstmpl.MustMakeTemplates(templates.FS),
[]routespec.RouteSpec{},
"",
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func TestFSRedirect(t *testing.T) {
"version",
http.Dir("."),
inject.CopyInject{},
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
fstmpl.MustMakeTemplates(templates.FS),
[]routespec.RouteSpec{},
"",
},
Expand Down Expand Up @@ -208,7 +208,7 @@ func _TestFileServerCleans(t *testing.T) {
},
},
inject.CopyInject{},
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
fstmpl.MustMakeTemplates(templates.FS),
[]routespec.RouteSpec{},
"",
}
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestFileServerImplicitLeadingSlash(t *testing.T) {
"version",
http.Dir(tempDir),
inject.CopyInject{},
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
fstmpl.MustMakeTemplates(templates.FS),
[]routespec.RouteSpec{},
"",
}
Expand Down Expand Up @@ -414,7 +414,7 @@ func TestServeIndexHtml(t *testing.T) {
"version",
http.Dir("."),
inject.CopyInject{},
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
fstmpl.MustMakeTemplates(templates.FS),
[]routespec.RouteSpec{},
"",
}
Expand Down Expand Up @@ -443,7 +443,7 @@ func TestFileServerZeroByte(t *testing.T) {
"version",
http.Dir("."),
inject.CopyInject{},
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
fstmpl.MustMakeTemplates(templates.FS),
[]routespec.RouteSpec{},
"",
}
Expand Down Expand Up @@ -538,7 +538,7 @@ func TestNotFoundOverride(t *testing.T) {
"version",
fsys,
inject.CopyInject{},
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
fstmpl.MustMakeTemplates(templates.FS),
[]routespec.RouteSpec{
{Host: "", Path: "/", Value: "foo.html"},
},
Expand Down Expand Up @@ -610,7 +610,7 @@ func TestDirectoryIfNotModified(t *testing.T) {
"version",
fsys,
inject.CopyInject{},
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
fstmpl.MustMakeTemplates(templates.FS),
[]routespec.RouteSpec{},
"",
}
Expand Down
35 changes: 21 additions & 14 deletions ricetemp/ricetemp.go → fstmpl/fstmpl.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Package ricetemp makes templates from a ricebox.
package ricetemp
// Package embedtmpl makes templates from a fs.FS
package fstmpl

import (
"html/template"
"io"
"io/fs"
"os"
"strings"

"github.com/GeertJohan/go.rice"
"github.com/dustin/go-humanize"
)

Expand All @@ -25,16 +26,16 @@ func fileType(f os.FileInfo) string {
}

// MustMakeTemplates makes templates, and panic on error
func MustMakeTemplates(rb *rice.Box) *template.Template {
templates, err := MakeTemplates(rb)
func MustMakeTemplates(fs fs.ReadDirFS) *template.Template {
templates, err := MakeTemplates(fs)
if err != nil {
panic(err)
}
return templates
}

// MakeTemplates takes a rice.Box and returns a html.Template
func MakeTemplates(rb *rice.Box) (*template.Template, error) {
func MakeTemplates(fs fs.ReadDirFS) (*template.Template, error) {
tmpl := template.New("")

funcMap := template.FuncMap{
Expand All @@ -44,14 +45,20 @@ func MakeTemplates(rb *rice.Box) (*template.Template, error) {
}
tmpl.Funcs(funcMap)

err := rb.Walk("", func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
_, err := tmpl.New(path).Parse(rb.MustString(path))
if err != nil {
return err
}
ds, err := fs.ReadDir(".")
if err != nil {
return nil, err
}

for _, d := range ds {
f, _ := fs.Open(d.Name())
fbs, _ := io.ReadAll(f)

_, err := tmpl.New(d.Name()).Parse(string(fbs))
if err != nil {
return nil, err
}
return nil
})
}

return tmpl, err
}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module github.com/cortesi/devd

go 1.12
go 1.16

require (
github.com/GeertJohan/go.rice v1.0.2
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/cortesi/moddwatch v0.0.0-20210323234936-df014e95c743
github.com/cortesi/termlog v0.0.0-20210222042314-a1eec763abec
github.com/daaku/go.zipexe v1.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.13.0
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d
Expand Down
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0=
github.com/GeertJohan/go.rice v1.0.2 h1:PtRw+Tg3oa3HYwiDBZyvOJ8LdIyf6lAovJJtr7YOAYk=
github.com/GeertJohan/go.rice v1.0.2/go.mod h1:af5vUNlDNkCjOZeSGFgIJxDje9qdjsO6hshx0gTmZt4=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
Expand All @@ -13,9 +9,6 @@ github.com/cortesi/moddwatch v0.0.0-20210323234936-df014e95c743/go.mod h1:DBqag+
github.com/cortesi/termlog v0.0.0-20210222042314-a1eec763abec h1:v7D8uHsIKsyjfyhhNdY4qivqN558Ejiq+CDXiUljZ+4=
github.com/cortesi/termlog v0.0.0-20210222042314-a1eec763abec/go.mod h1:10Fm2kasJmcKf1FSMQGSWb976sfR29hejNtfS9AydB4=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E=
github.com/daaku/go.zipexe v1.0.1 h1:wV4zMsDOI2SZ2m7Tdz1Ps96Zrx+TzaK15VbUaGozw0M=
github.com/daaku/go.zipexe v1.0.1/go.mod h1:5xWogtqlYnfBXkSB1o9xysukNP9GTvaNkqzUZbt3Bw8=
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=
Expand All @@ -30,7 +23,6 @@ github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/juju/ratelimit v1.0.2 h1:sRxmtRiajbvrcLQT7S+JbqU0ntsb9W2yhSdNN8tWfaI=
github.com/juju/ratelimit v1.0.2/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -49,7 +41,6 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nkovacs/streamquote v1.0.0/go.mod h1:BN+NaZ2CmdKqUuTUXUEm9j95B2TRbpOWpxbJYzzgUsc=
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/rjeczalik/notify v0.0.0-20181126183243-629144ba06a1 h1:FLWDC+iIP9BWgYKvWKKtOUZux35LIQNAuIzp/63RQJU=
Expand All @@ -60,8 +51,6 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/toqueteos/webbrowser v1.2.0 h1:tVP/gpK69Fx+qMJKsLE7TD8LuGWPnEV71wBN9rrstGQ=
github.com/toqueteos/webbrowser v1.2.0/go.mod h1:XWoZq4cyp9WeUeak7w7LXRUQf1F1ATJMir8RTqb4ayM=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
Expand Down Expand Up @@ -107,7 +96,6 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
Expand Down
File renamed without changes.
File renamed without changes.
11 changes: 8 additions & 3 deletions livereload/livereload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
package livereload

import (
_ "embed"
"net/http"
"regexp"
"strings"
"sync"

"github.com/GeertJohan/go.rice"
"github.com/cortesi/devd/inject"
"github.com/cortesi/termlog"
"github.com/gorilla/websocket"
)

//go:embed client.js
var clientJS []byte

//go:embed LICENSE
var license []byte

// Reloader triggers a reload
type Reloader interface {
Reload(paths []string)
Expand Down Expand Up @@ -130,8 +136,7 @@ func (s *Server) Watch(ch chan []string) {
// ServeScript is a handler function that serves the livereload JavaScript file
func (s *Server) ServeScript(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "application/javascript")
clientBox := rice.MustFindBox("static")
_, err := rw.Write(clientBox.MustBytes("client.js"))
_, err := rw.Write(clientJS)
if err != nil {
s.logger.Warn("Error serving livereload script: %s", err)
}
Expand Down
Loading

0 comments on commit 98a1cca

Please sign in to comment.