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

Added option to disable logging when serving static assets #147

Merged
merged 5 commits into from
Jan 10, 2014
Merged
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
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need godoc documentation

// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this consistent to how we prepare options in the render middleware https://github.com/codegangsta/martini-contrib/blob/master/render/render.go#L126-L141

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")
}