-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 2 commits
7d4ba0e
023e107
f5f3655
695ee32
34e5f8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,32 @@ import ( | |
"strings" | ||
) | ||
|
||
type StaticOptions struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need godoc documentation |
||
SkipLogging bool | ||
SkipServeIndex bool | ||
IndexFile string | ||
} | ||
|
||
func prepareStaticOptions(options []StaticOptions) StaticOptions { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
if len(options) > 0 { | ||
// Default index file to serve should be index.html | ||
if options[0].IndexFile == "" { | ||
options[0].IndexFile = "index.html" | ||
} | ||
return options[0] | ||
} | ||
return StaticOptions{ | ||
SkipLogging: false, // Logging is on by default | ||
SkipServeIndex: false, // Try to serve index.html by default | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of having this option would it be okay to have the zero value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to, but the problem is that since in prepareStaticOptions() I already check for the zero value (and set it to "index.html" as default value if it is) it can never be the zero value. It's either something or index.html. |
||
IndexFile: "index.html", // Default index file to serve | ||
} | ||
} | ||
|
||
// 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) { | ||
file := req.URL.Path | ||
f, err := dir.Open(file) | ||
|
@@ -24,29 +47,33 @@ func Static(directory string) Handler { | |
return | ||
} | ||
|
||
// Try to serve index.html | ||
if fi.IsDir() { | ||
if !opt.SkipServeIndex { | ||
// Try to serve index.html | ||
if fi.IsDir() { | ||
|
||
// redirect if missing trailing slash | ||
if !strings.HasSuffix(file, "/") { | ||
http.Redirect(res, req, file+"/", http.StatusFound) | ||
return | ||
} | ||
// redirect if missing trailing slash | ||
if !strings.HasSuffix(file, "/") { | ||
http.Redirect(res, req, file+"/", http.StatusFound) | ||
return | ||
} | ||
|
||
file = path.Join(file, "index.html") | ||
f, err = dir.Open(file) | ||
if err != nil { | ||
return | ||
} | ||
defer f.Close() | ||
file = path.Join(file, opt.IndexFile) | ||
f, err = dir.Open(file) | ||
if err != nil { | ||
return | ||
} | ||
defer f.Close() | ||
|
||
fi, err = f.Stat() | ||
if err != nil || fi.IsDir() { | ||
return | ||
fi, err = f.Stat() | ||
if err != nil || fi.IsDir() { | ||
return | ||
} | ||
} | ||
} | ||
|
||
log.Println("[Static] Serving " + file) | ||
if !opt.SkipLogging { | ||
log.Println("[Static] Serving " + file) | ||
} | ||
http.ServeContent(res, req, file, fi.ModTime(), f) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of this call here? With how Martini works, this handler shouldn't be called if the context is written right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just ran your newly added test without this check and it still passes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, yeah. I noticed that now too. Doh!
Removed it. ;)