This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
forked from seven5/seven5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_files.go
97 lines (89 loc) · 3.17 KB
/
static_files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package seven5
import (
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
const (
GOPATH_PREFIX = "/gopath"
CONTINUE = 50
)
//StaticFilesServer is a wrapper around http.Handler that understands about
//the Gopath for debugging. Note that most applications with complex routing
//requirements would probably be better off using SimpleComponentMatcher because
//StaticFilesServer only understands static files, not URL->file mappings.
type StaticFilesServer interface {
http.Handler
}
//SimpleStaticFileServer is a simple implementation of a file server
//for static files that is sufficient for most applications. It
//defaults to serving "/" from "static" (child of the current directory)
//but this can be changed with the environment variable STATIC_DIR.
type SimpleStaticFilesServer struct {
testMode bool
mountedAt string
staticDir string
fs http.Handler
}
//NewStaticFilesServer returns a new file server and if isTestMode
//is true and the environment variable GOPATH is set, it will
//also serve up go source files from the GOPATH. It expects that the
//prefix "/gopath" will be used for gopath requests. You should supply
//the place this has been "mounted" in the URL space (usually "/"") in
//the first parameter.
func NewStaticFilesServer(mountedAt string, isTestMode bool) *SimpleStaticFilesServer {
staticDir := "static"
env := os.Getenv("STATIC_DIR")
if env != "" {
log.Printf("STATIC_DIR is set, using %s for static files", env)
staticDir = env
}
return &SimpleStaticFilesServer{
testMode: isTestMode,
mountedAt: mountedAt,
staticDir: staticDir,
fs: http.StripPrefix(mountedAt, http.FileServer(http.Dir(staticDir))),
}
}
//Utility function for taking a requested URL and finding it in the Gopath
//and returning its contents. It will return 404 if the file cannot be found
//and expects that the desired parameter already has had the gopath prefix
//stripped of it, e.g. /foo.go not /gopath/foo.go.
func GopathLookup(w http.ResponseWriter, r *http.Request, desired string) {
gopathSegments := strings.Split(os.Getenv("GOPATH"), ":")
for _, gopath := range gopathSegments {
_, err := os.Stat(filepath.Join(gopath, desired))
if err != nil {
continue
}
log.Printf("[GOPATH CONTENT] %s", filepath.Join(gopath, desired))
http.ServeFile(w, r, filepath.Join(gopath, desired))
return
}
http.NotFound(w, r)
}
//Return the path to the file that has the desired content, or return "" if
//the file cannot be found.
func GopathSearch(desired string) string {
gopathSegments := strings.Split(os.Getenv("GOPATH"), ":")
for _, gopath := range gopathSegments {
_, err := os.Stat(filepath.Join(gopath, "src", desired))
if err != nil {
continue
}
return filepath.Join(gopath, "src", desired)
}
return ""
}
//ServeHTTP retuns a static file or a not found error. This function meets
//the requirement of net/http#Handler.
func (s *SimpleStaticFilesServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if s.testMode && strings.HasPrefix(r.URL.String(), GOPATH_PREFIX) {
GopathLookup(w, r, strings.TrimPrefix(r.URL.String(), GOPATH_PREFIX))
return
}
log.Printf("[STATIC CONTENT (%s)]: %v", s.staticDir, r.URL.String())
s.fs.ServeHTTP(w, r)
}