From b7e09971d09c490a4881b99baa952a70e49b9139 Mon Sep 17 00:00:00 2001 From: mlvzk Date: Sat, 13 Jul 2019 23:19:53 +0200 Subject: [PATCH 1/4] remove dep on net/http in fs, move it to httpfs and support http.FileSystem through adapter structure --- README.md | 3 ++- example/main.go | 3 ++- fs/fs.go | 11 +++++------ fs/httpfs/fs.go | 20 ++++++++++++++++++++ 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 fs/httpfs/fs.go diff --git a/README.md b/README.md index bbaa5d66..a7f6bdd2 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ In your program, all your need to do is to import the generated package, initial ~~~ go import ( "github.com/rakyll/statik/fs" + "github.com/rakyll/statik/fs/httpfs" _ "./statik" // TODO: Replace with the absolute import path ) @@ -34,7 +35,7 @@ import ( log.Fatal(err) } - http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(statikFS))) + http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(httpfs.System(statikFS)))) http.ListenAndServe(":8080", nil) ~~~ diff --git a/example/main.go b/example/main.go index a4d4a96a..27970d17 100644 --- a/example/main.go +++ b/example/main.go @@ -8,6 +8,7 @@ import ( _ "github.com/rakyll/statik/example/statik" "github.com/rakyll/statik/fs" + "github.com/rakyll/statik/fs/httpfs" ) // Before buildling, run go generate. @@ -18,6 +19,6 @@ func main() { log.Fatal(err) } - http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(statikFS))) + http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(httpfs.System(statikFS)))) http.ListenAndServe(":8080", nil) } diff --git a/fs/fs.go b/fs/fs.go index da50fc8a..68ae2ef6 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -22,7 +22,6 @@ import ( "fmt" "io" "io/ioutil" - "net/http" "os" "path" "sort" @@ -36,10 +35,10 @@ var zipData string type file struct { os.FileInfo data []byte - fs *statikFS + fs *StatikFS } -type statikFS struct { +type StatikFS struct { files map[string]file dirs map[string][]string } @@ -52,7 +51,7 @@ func Register(data string) { // New creates a new file system with the registered zip contents data. // It unzips all files and stores them in an in-memory map. -func New() (http.FileSystem, error) { +func New() (*StatikFS, error) { if zipData == "" { return nil, errors.New("statik/fs: no zip data registered") } @@ -62,7 +61,7 @@ func New() (http.FileSystem, error) { } files := make(map[string]file, len(zipReader.File)) dirs := make(map[string][]string) - fs := &statikFS{files: files, dirs: dirs} + fs := &StatikFS{files: files, dirs: dirs} for _, zipFile := range zipReader.File { fi := zipFile.FileInfo() f := file{FileInfo: fi, fs: fs} @@ -121,7 +120,7 @@ func unzip(zf *zip.File) ([]byte, error) { // no file matching the given file name is found in the archive. // If a directory is requested, Open returns the file named "index.html" // in the requested directory, if that file exists. -func (fs *statikFS) Open(name string) (http.File, error) { +func (fs *StatikFS) Open(name string) (*httpFile, error) { name = strings.Replace(name, "//", "/", -1) if f, ok := fs.files[name]; ok { return newHTTPFile(f), nil diff --git a/fs/httpfs/fs.go b/fs/httpfs/fs.go new file mode 100644 index 00000000..64a19867 --- /dev/null +++ b/fs/httpfs/fs.go @@ -0,0 +1,20 @@ +package httpfs + +import ( + "net/http" + "github.com/rakyll/statik/fs" +) + +type system struct { + *fs.StatikFS +} + +func System(fs *fs.StatikFS) system { + return system{ + fs, + } +} + +func (fs system) Open(name string) (http.File, error) { + return fs.StatikFS.Open(name) +} From d2c5c003bcc54e3dcbf3c705a9a3c8824e43a1fa Mon Sep 17 00:00:00 2001 From: mlvzk Date: Sat, 13 Jul 2019 23:36:48 +0200 Subject: [PATCH 2/4] remove net/http dep from fs/walk.go --- fs/walk.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fs/walk.go b/fs/walk.go index f4eb37d1..49704cf3 100644 --- a/fs/walk.go +++ b/fs/walk.go @@ -17,7 +17,6 @@ package fs import ( "bytes" "io" - "net/http" "path" "path/filepath" ) @@ -27,7 +26,7 @@ import ( // All errors that arise visiting files and directories are filtered by walkFn. // // As with filepath.Walk, if the walkFn returns filepath.SkipDir, then the directory is skipped. -func Walk(hfs http.FileSystem, root string, walkFn filepath.WalkFunc) error { +func Walk(hfs *StatikFS, root string, walkFn filepath.WalkFunc) error { dh, err := hfs.Open(root) if err != nil { return err @@ -67,7 +66,7 @@ func Walk(hfs http.FileSystem, root string, walkFn filepath.WalkFunc) error { // ReadFile reads the contents of the file of hfs specified by name. // Just as ioutil.ReadFile does. -func ReadFile(hfs http.FileSystem, name string) ([]byte, error) { +func ReadFile(hfs *StatikFS, name string) ([]byte, error) { fh, err := hfs.Open(name) if err != nil { return nil, err From 01bca4f188a231aafe50832dee7a9ed941fbc6b7 Mon Sep 17 00:00:00 2001 From: mlvzk Date: Sat, 13 Jul 2019 23:50:48 +0200 Subject: [PATCH 3/4] go fmt --- fs/httpfs/fs.go | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/httpfs/fs.go b/fs/httpfs/fs.go index 64a19867..284b00c7 100644 --- a/fs/httpfs/fs.go +++ b/fs/httpfs/fs.go @@ -2,6 +2,7 @@ package httpfs import ( "net/http" + "github.com/rakyll/statik/fs" ) From 9b57ad0ef4986c413282e56182c21158964b9014 Mon Sep 17 00:00:00 2001 From: mlvzk Date: Sun, 14 Jul 2019 00:08:30 +0200 Subject: [PATCH 4/4] rename ../fs/httpfs/fs.go -> ../fs/httpfs/httpfs.go --- fs/httpfs/{fs.go => httpfs.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename fs/httpfs/{fs.go => httpfs.go} (100%) diff --git a/fs/httpfs/fs.go b/fs/httpfs/httpfs.go similarity index 100% rename from fs/httpfs/fs.go rename to fs/httpfs/httpfs.go