Skip to content

Commit

Permalink
Remove FileServer from Mux, and offer as snippet helper code to users…
Browse files Browse the repository at this point in the history
… to build their own
  • Loading branch information
Peter Kieltyka authored and Peter Kieltyka committed Jun 21, 2017
1 parent 26633cb commit 6cbda14
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 103 deletions.
23 changes: 22 additions & 1 deletion _examples/fileserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"

"github.com/pressly/chi"
)
Expand All @@ -17,7 +18,27 @@ func main() {

workDir, _ := os.Getwd()
filesDir := filepath.Join(workDir, "files")
r.FileServer("/files", http.Dir(filesDir))
FileServer(r, "/files", http.Dir(filesDir))

http.ListenAndServe(":3333", r)
}

// FileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, ":*") {
panic("FileServer does not permit URL parameters.")
}

fs := http.StripPrefix(path, http.FileServer(root))

if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"

r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
}))
}
22 changes: 0 additions & 22 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,28 +323,6 @@ func (mx *Mux) Routes() []Route {
return mx.tree.routes()
}

// FileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func (mx *Mux) FileServer(path string, root http.FileSystem) {
if strings.ContainsAny(path, ":*") {
panic("chi: FileServer does not permit URL parameters.")
}

// TODO: test file stat, and trigger not found handler..

fs := http.StripPrefix(path, http.FileServer(root))

if path != "/" && path[len(path)-1] != '/' {
mx.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"

mx.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
}))
}

// NotFoundHandler returns the default Mux 404 responder whenever a route
// cannot be found.
func (mx *Mux) NotFoundHandler() http.HandlerFunc {
Expand Down
80 changes: 0 additions & 80 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package chi
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -1315,85 +1314,6 @@ func testMuxContextIsThreadSafe(t *testing.T) {
wg.Wait()
}

func TestMuxFileServer(t *testing.T) {
fixtures := map[string]http.File{
"index.html": &testFile{"index.html", []byte("index\n")},
"ok": &testFile{"ok", []byte("ok\n")},
}

memfs := &testFileSystem{func(name string) (http.File, error) {
name = name[1:]
// if name == "" {
// name = "index.html"
// }
if f, ok := fixtures[name]; ok {
return f, nil
}
return nil, errors.New("file not found")
}}

r := NewRouter()
r.FileServer("/mounted", memfs)
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("bye"))
})
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte("nothing here"))
})
r.FileServer("/", memfs)

ts := httptest.NewServer(r)
defer ts.Close()

if _, body := testRequest(t, ts, "GET", "/hi", nil); body != "bye" {
t.Fatalf(body)
}

// HEADS UP: net/http notfoundhandler will kick-in for static assets
if _, body := testRequest(t, ts, "GET", "/mounted/nothing-here", nil); body == "nothing here" {
t.Fatalf(body)
}

if _, body := testRequest(t, ts, "GET", "/nothing-here", nil); body == "nothing here" {
t.Fatalf(body)
}

if _, body := testRequest(t, ts, "GET", "/mounted-nothing-here", nil); body == "nothing here" {
t.Fatalf(body)
}

if _, body := testRequest(t, ts, "GET", "/hi", nil); body != "bye" {
t.Fatalf(body)
}

if _, body := testRequest(t, ts, "GET", "/ok", nil); body != "ok\n" {
t.Fatalf(body)
}

if _, body := testRequest(t, ts, "GET", "/mounted/ok", nil); body != "ok\n" {
t.Fatalf(body)
}

// TODO/FIX: testFileSystem mock struct.. it struggles to pass this since it gets
// into a redirect loop, however, it does work with http.Dir() using the disk.
// if _, body := testRequest(t, ts, "GET", "/index.html", nil); body != "index\n" {
// t.Fatalf(body)
// }

// if _, body := testRequest(t, ts, "GET", "/", nil); body != "index\n" {
// t.Fatalf(body)
// }

// if _, body := testRequest(t, ts, "GET", "/mounted", nil); body != "index\n" {
// t.Fatalf(body)
// }

// if _, body := testRequest(t, ts, "GET", "/mounted/", nil); body != "index\n" {
// t.Fatalf(body)
// }
}

func TestEscapedURLParams(t *testing.T) {
m := NewRouter()
m.Get("/api/{identifier}/{region}/{size}/{rotation}/*", func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 6cbda14

Please sign in to comment.