Browse Source

static file server supports

tags/v0.9.0
jimzhan 9 years ago
parent
commit
0db2906b72
3 changed files with 50 additions and 1 deletions
  1. +6
    -0
      rex.go
  2. +15
    -1
      server.go
  3. +29
    -0
      server_test.go

+ 6
- 0
rex.go View File

return Default.Group(path) return Default.Group(path)
} }


// FileServer registers a handler to serve HTTP (GET|HEAD) requests
// with the contents of file system under the given directory.
func FileServer(prefix, dir string) {
Default.FileServer(prefix, dir)
}

// Use appends middleware module into the serving list, modules will be served in FIFO order. // Use appends middleware module into the serving list, modules will be served in FIFO order.
func Use(module func(http.Handler) http.Handler) { func Use(module func(http.Handler) http.Handler) {
Default.Use(module) Default.Use(module)

+ 15
- 1
server.go View File



import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"path/filepath"
"reflect" "reflect"
"runtime" "runtime"
"time" "time"
} }


func New() *Server { func New() *Server {
return &Server{
self := &Server{
middleware: new(middleware), middleware: new(middleware),
mux: mux.NewRouter().StrictSlash(true), mux: mux.NewRouter().StrictSlash(true),
} }
return self
} }


// build constructs all server/subservers along with their middleware modules chain. // build constructs all server/subservers along with their middleware modules chain.
return name return name
} }


// FileServer registers a handler to serve HTTP (GET|HEAD) requests
// with the contents of file system under the given directory.
func (self *Server) FileServer(prefix, dir string) {
if abs, err := filepath.Abs(dir); err == nil {
fs := http.StripPrefix(prefix, http.FileServer(http.Dir(abs)))
self.mux.PathPrefix(prefix).Handler(fs)
} else {
log.Fatalf("Failed to setup file server: %v", err)
}
}

// Use add the middleware module into the stack chain. // Use add the middleware module into the stack chain.
func (self *Server) Use(module func(http.Handler) http.Handler) { func (self *Server) Use(module func(http.Handler) http.Handler) {
self.middleware.stack = append(self.middleware.stack, module) self.middleware.stack = append(self.middleware.stack, module)

+ 29
- 0
server_test.go View File

"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"path"
"testing" "testing"


. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
}) })
} }


func TestFileServer(t *testing.T) {
Convey("rex.FileServer", t, func() {
var (
prefix = "/assets/"
filename = "logo.png"
)
tempdir := os.TempDir()
filepath := path.Join(tempdir, filename)
os.Create(filepath)
defer os.Remove(filepath)

app := New()
app.FileServer(prefix, tempdir)

request, _ := http.NewRequest("GET", path.Join(prefix, filename), nil)
response := httptest.NewRecorder()
app.ServeHTTP(response, request)
So(response.Code, ShouldEqual, http.StatusOK)

filename = "index.html"
request, _ = http.NewRequest("HEAD", prefix, nil)
response = httptest.NewRecorder()
app.ServeHTTP(response, request)
So(response.Code, ShouldEqual, http.StatusOK)
})
}

func TestUse(t *testing.T) { func TestUse(t *testing.T) {
app := New() app := New()
app.Get("/", func(w http.ResponseWriter, r *http.Request) { app.Get("/", func(w http.ResponseWriter, r *http.Request) {

Loading…
Cancel
Save