Skip to content

Commit 9ca1d0a

Browse files
committed
use go:embed
1 parent 04def84 commit 9ca1d0a

File tree

3 files changed

+19
-46
lines changed

3 files changed

+19
-46
lines changed

cli/serve/serve.go

+18-30
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package serve
33

44
import (
55
"crypto/tls"
6+
"embed"
67
"errors"
78
"fmt"
9+
"io/fs"
810
"net"
911
"net/http"
1012
"net/url"
@@ -13,7 +15,6 @@ import (
1315
"strconv"
1416
"strings"
1517

16-
rice "github.com/GeertJohan/go.rice"
1718
"github.com/cloudflare/cfssl/api"
1819
"github.com/cloudflare/cfssl/api/bundle"
1920
"github.com/cloudflare/cfssl/api/certadd"
@@ -81,38 +82,28 @@ func v1APIPath(path string) string {
8182
return (&url.URL{Path: path}).String()
8283
}
8384

84-
// httpBox implements http.FileSystem which allows the use of Box with a http.FileServer.
85-
// Attempting to Open an API endpoint will result in an error.
86-
type httpBox struct {
87-
*rice.Box
88-
redirects map[string]string
85+
//go:embed static
86+
var staticContent embed.FS
87+
88+
var staticRedirections = map[string]string{
89+
"bundle": "index.html",
90+
"scan": "index.html",
91+
"packages": "index.html",
8992
}
9093

91-
func (hb *httpBox) findStaticBox() (err error) {
92-
hb.Box, err = rice.FindBox("static")
93-
return
94+
type staticFS struct {
95+
fs fs.FS
96+
redirections map[string]string
9497
}
9598

96-
// Open returns a File for non-API enpoints using the http.File interface.
97-
func (hb *httpBox) Open(name string) (http.File, error) {
99+
func (s *staticFS) Open(name string) (fs.File, error) {
98100
if strings.HasPrefix(name, V1APIPrefix) {
99101
return nil, os.ErrNotExist
100102
}
101-
102-
if location, ok := hb.redirects[name]; ok {
103-
return hb.Box.Open(location)
103+
if location, ok := s.redirections[name]; ok {
104+
return s.fs.Open(location)
104105
}
105-
106-
return hb.Box.Open(name)
107-
}
108-
109-
// staticBox is the box containing all static assets.
110-
var staticBox = &httpBox{
111-
redirects: map[string]string{
112-
"/scan": "/index.html",
113-
"/bundle": "/index.html",
114-
"/packages": "/index.html",
115-
},
106+
return s.fs.Open(name)
116107
}
117108

118109
var errBadSigner = errors.New("signer not initialized")
@@ -242,11 +233,8 @@ var endpoints = map[string]func() (http.Handler, error){
242233
},
243234

244235
"/": func() (http.Handler, error) {
245-
if err := staticBox.findStaticBox(); err != nil {
246-
return nil, err
247-
}
248-
249-
return http.FileServer(staticBox), nil
236+
subFS, _ := fs.Sub(staticContent, "static")
237+
return http.FileServer(http.FS(&staticFS{fs: subFS, redirections: staticRedirections})), nil
250238
},
251239

252240
"health": func() (http.Handler, error) {

cli/serve/serve_test.go

-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package serve
33
import (
44
"net/http"
55
"net/http/httptest"
6-
"os"
76
"testing"
87

98
"github.com/cloudflare/cfssl/cli"
@@ -18,20 +17,6 @@ func TestServe(t *testing.T) {
1817
expected[v1APIPath(endpoint)] = http.StatusOK
1918
}
2019

21-
err := staticBox.Walk("", func(path string, info os.FileInfo, err error) error {
22-
if err != nil {
23-
return err
24-
}
25-
26-
if !info.IsDir() {
27-
expected["/"+path] = http.StatusOK
28-
}
29-
return nil
30-
})
31-
if err != nil {
32-
t.Error(err)
33-
}
34-
3520
// Disabled endpoints should return '404 Not Found'
3621
expected[v1APIPath("sign")] = http.StatusNotFound
3722
expected[v1APIPath("authsign")] = http.StatusNotFound

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/cloudflare/cfssl
22

3-
go 1.14
3+
go 1.16
44

55
require (
66
bitbucket.org/liamstask/goose v0.0.0-20150115234039-8488cc47d90c

0 commit comments

Comments
 (0)