-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fileserver struct rather than DefaultServeMux
- Loading branch information
Showing
8 changed files
with
132 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/syntaqx/serve" | ||
) | ||
|
||
func main() { | ||
fs := serve.NewFileServer(".") | ||
log.Fatal(http.ListenAndServe(":8080", fs)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,28 @@ | ||
// Package serve provides a static http server anywhere you need one. | ||
package serve | ||
|
||
import "net/http" | ||
|
||
// FileServer implements the http.FileServer. | ||
type FileServer struct { | ||
Handler http.Handler | ||
} | ||
|
||
// NewFileServer initializes a FileServer. | ||
func NewFileServer(dir string) *FileServer { | ||
fs := &FileServer{ | ||
Handler: http.FileServer(http.Dir(dir)), | ||
} | ||
|
||
return fs | ||
} | ||
|
||
// Use wraps the handler with another, middleware style. | ||
func (fs *FileServer) Use(h func(http.Handler) http.Handler) { | ||
fs.Handler = h(fs.Handler) | ||
} | ||
|
||
// ServeHTTP implements the net/http.Handler interface. | ||
func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
fs.Handler.ServeHTTP(w, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package serve | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFileServerUse(t *testing.T) { | ||
t.Parallel() | ||
assert := assert.New(t) | ||
|
||
req, err := http.NewRequest(http.MethodGet, "/", nil) | ||
assert.NoError(err) | ||
res := httptest.NewRecorder() | ||
|
||
testMiddleware1 := func(next http.Handler) http.Handler { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte("start\n")) | ||
next.ServeHTTP(w, r) | ||
} | ||
return http.HandlerFunc(fn) | ||
} | ||
|
||
testMiddleware2 := func(next http.Handler) http.Handler { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte("end\n")) | ||
} | ||
return http.HandlerFunc(fn) | ||
} | ||
|
||
fs := NewFileServer(".") | ||
fs.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
t.Fail() | ||
}) | ||
|
||
fs.Use(testMiddleware2) | ||
fs.Use(testMiddleware1) | ||
|
||
fs.ServeHTTP(res, req) | ||
|
||
assert.Equal("start\nend\n", res.Body.String()) | ||
} | ||
|
||
func TestFileServerServeHTTP(t *testing.T) { | ||
t.Parallel() | ||
assert := assert.New(t) | ||
|
||
req, err := http.NewRequest(http.MethodGet, "/", nil) | ||
assert.NoError(err) | ||
res := httptest.NewRecorder() | ||
|
||
fs := NewFileServer(".") | ||
fs.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte("expected")) | ||
}) | ||
fs.ServeHTTP(res, req) | ||
|
||
assert.Equal("expected", res.Body.String()) | ||
} |