Skip to content

Commit

Permalink
use go:embed
Browse files Browse the repository at this point in the history
  • Loading branch information
blotus committed Aug 17, 2022
1 parent 04def84 commit 9ca1d0a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 46 deletions.
48 changes: 18 additions & 30 deletions cli/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package serve

import (
"crypto/tls"
"embed"
"errors"
"fmt"
"io/fs"
"net"
"net/http"
"net/url"
Expand All @@ -13,7 +15,6 @@ import (
"strconv"
"strings"

rice "github.com/GeertJohan/go.rice"
"github.com/cloudflare/cfssl/api"
"github.com/cloudflare/cfssl/api/bundle"
"github.com/cloudflare/cfssl/api/certadd"
Expand Down Expand Up @@ -81,38 +82,28 @@ func v1APIPath(path string) string {
return (&url.URL{Path: path}).String()
}

// httpBox implements http.FileSystem which allows the use of Box with a http.FileServer.
// Attempting to Open an API endpoint will result in an error.
type httpBox struct {
*rice.Box
redirects map[string]string
//go:embed static
var staticContent embed.FS

var staticRedirections = map[string]string{
"bundle": "index.html",
"scan": "index.html",
"packages": "index.html",
}

func (hb *httpBox) findStaticBox() (err error) {
hb.Box, err = rice.FindBox("static")
return
type staticFS struct {
fs fs.FS
redirections map[string]string
}

// Open returns a File for non-API enpoints using the http.File interface.
func (hb *httpBox) Open(name string) (http.File, error) {
func (s *staticFS) Open(name string) (fs.File, error) {
if strings.HasPrefix(name, V1APIPrefix) {
return nil, os.ErrNotExist
}

if location, ok := hb.redirects[name]; ok {
return hb.Box.Open(location)
if location, ok := s.redirections[name]; ok {
return s.fs.Open(location)
}

return hb.Box.Open(name)
}

// staticBox is the box containing all static assets.
var staticBox = &httpBox{
redirects: map[string]string{
"/scan": "/index.html",
"/bundle": "/index.html",
"/packages": "/index.html",
},
return s.fs.Open(name)
}

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

"/": func() (http.Handler, error) {
if err := staticBox.findStaticBox(); err != nil {
return nil, err
}

return http.FileServer(staticBox), nil
subFS, _ := fs.Sub(staticContent, "static")
return http.FileServer(http.FS(&staticFS{fs: subFS, redirections: staticRedirections})), nil
},

"health": func() (http.Handler, error) {
Expand Down
15 changes: 0 additions & 15 deletions cli/serve/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package serve
import (
"net/http"
"net/http/httptest"
"os"
"testing"

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

err := staticBox.Walk("", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if !info.IsDir() {
expected["/"+path] = http.StatusOK
}
return nil
})
if err != nil {
t.Error(err)
}

// Disabled endpoints should return '404 Not Found'
expected[v1APIPath("sign")] = http.StatusNotFound
expected[v1APIPath("authsign")] = http.StatusNotFound
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/cloudflare/cfssl

go 1.14
go 1.16

require (
bitbucket.org/liamstask/goose v0.0.0-20150115234039-8488cc47d90c
Expand Down

0 comments on commit 9ca1d0a

Please sign in to comment.