Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 41 lines (34 sloc) 0.806 kb
b105d32 @paulbellamy Adding Static middleware for serving static files
authored
1 package mango
2
3 import (
4 "path"
5 "io/ioutil"
6 "os"
7 )
8
9 func fileExists(filename string) bool {
10 info, err := os.Stat(filename)
11 if err != nil {
12 return false
13 } else if !info.IsRegular() {
14 return false
15 }
16
17 return true
18 }
19
20 func readFile(filename string) (string, os.Error) {
21 body, err := ioutil.ReadFile(filename)
22 return string(body), err
23 }
24
25 func Static(directory string) Middleware {
26 return func(env Env, app App) (Status, Headers, Body) {
27 // See if we can serve a file
28 file := path.Join(directory, env.Request().URL.Path)
29 if fileExists(file) && (env.Request().Method == "GET" || env.Request().Method == "HEAD") {
30 if body, err := readFile(file); err == nil {
31 return 200, Headers{}, Body(body)
32 } else {
33 panic(err)
34 }
35 }
36
37 // No file found, pass on to app
38 return app(env)
39 }
40 }
Something went wrong with that request. Please try again.