Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move dependency on net/http in fs to httpfs #70

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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)
~~~

Expand Down
3 changes: 2 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
11 changes: 5 additions & 6 deletions fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"sort"
Expand All @@ -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
}
Expand All @@ -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")
}
Expand All @@ -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}
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions fs/httpfs/httpfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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)
}
5 changes: 2 additions & 3 deletions fs/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package fs
import (
"bytes"
"io"
"net/http"
"path"
"path/filepath"
)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down