Skip to content

Commit

Permalink
Merge pull request #147 from JamesClonk/master
Browse files Browse the repository at this point in the history
Added option to disable logging when serving static assets
  • Loading branch information
codegangsta committed Jan 10, 2014
2 parents 3185ecd + 34e5f8e commit 38efc80
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
34 changes: 30 additions & 4 deletions static.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,33 @@ import (
"strings"
)

// StaticOptions is a struct for specifying configuration options for the martini.Static middleware.
type StaticOptions struct {
// SkipLogging can be used to switch log messages to *log.logger off.
SkipLogging bool
// IndexFile defines which file to serve as index if it exists.
IndexFile string
}

func prepareStaticOptions(options []StaticOptions) StaticOptions {
var opt StaticOptions
if len(options) > 0 {
opt = options[0]
}

// Defaults
if len(opt.IndexFile) == 0 {
opt.IndexFile = "index.html"
}

return opt
}

// Static returns a middleware handler that serves static files in the given directory.
func Static(directory string) Handler {
func Static(directory string, staticOpt ...StaticOptions) Handler {
dir := http.Dir(directory)
opt := prepareStaticOptions(staticOpt)

return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
if req.Method != "GET" && req.Method != "HEAD" {
return
Expand All @@ -27,7 +51,7 @@ func Static(directory string) Handler {
return
}

// Try to serve index.html
// try to serve index file
if fi.IsDir() {

// redirect if missing trailing slash
Expand All @@ -36,7 +60,7 @@ func Static(directory string) Handler {
return
}

file = path.Join(file, "index.html")
file = path.Join(file, opt.IndexFile)
f, err = dir.Open(file)
if err != nil {
return
Expand All @@ -49,7 +73,9 @@ func Static(directory string) Handler {
}
}

log.Println("[Static] Serving " + file)
if !opt.SkipLogging {
log.Println("[Static] Serving " + file)
}
http.ServeContent(res, req, file, fi.ModTime(), f)
}
}
56 changes: 56 additions & 0 deletions static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package martini

import (
"bytes"
"github.com/codegangsta/inject"
"log"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -81,3 +83,57 @@ func Test_Static_BadDir(t *testing.T) {
m.ServeHTTP(response, req)
refute(t, response.Code, http.StatusOK)
}

func Test_Static_Options_Logging(t *testing.T) {
response := httptest.NewRecorder()

var buffer bytes.Buffer
m := &Martini{inject.New(), []Handler{}, func() {}, log.New(&buffer, "[martini] ", 0)}
m.Map(m.logger)
m.Map(defaultReturnHandler())

opt := StaticOptions{}
m.Use(Static(".", opt))

req, err := http.NewRequest("GET", "http://localhost:3000/martini.go", nil)
if err != nil {
t.Error(err)
}

m.ServeHTTP(response, req)
expect(t, response.Code, http.StatusOK)
expect(t, buffer.String(), "[martini] [Static] Serving /martini.go\n")

// Now without logging
m.Handlers()
buffer.Reset()

// This should disable logging
opt.SkipLogging = true
m.Use(Static(".", opt))

m.ServeHTTP(response, req)
expect(t, response.Code, http.StatusOK)
expect(t, buffer.String(), "")
}

func Test_Static_Options_ServeIndex(t *testing.T) {
response := httptest.NewRecorder()

var buffer bytes.Buffer
m := &Martini{inject.New(), []Handler{}, func() {}, log.New(&buffer, "[martini] ", 0)}
m.Map(m.logger)
m.Map(defaultReturnHandler())

opt := StaticOptions{IndexFile: "martini.go"} // Define martini.go as index file
m.Use(Static(".", opt))

req, err := http.NewRequest("GET", "http://localhost:3000/", nil)
if err != nil {
t.Error(err)
}

m.ServeHTTP(response, req)
expect(t, response.Code, http.StatusOK)
expect(t, buffer.String(), "[martini] [Static] Serving /martini.go\n")
}

0 comments on commit 38efc80

Please sign in to comment.