-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhttp.go
More file actions
72 lines (62 loc) · 1.76 KB
/
http.go
File metadata and controls
72 lines (62 loc) · 1.76 KB
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
package gohaml
import (
"net/http"
"strings"
)
// create an http.Handler that loads haml files from locations relative
// to base dir, taking into account that they won't end in *haml in the
// http request. Translates URLs such that:
//
// /bla.html -> ${base}/bla.haml
// /bla/bla/dingdong.html -> ${base}/bla/bla/dingdong.haml
// /bla/bla/ -> ${base}/bla/bla/index.haml
func NewHamlHandler(base string) (hndl http.Handler, err error) {
var l Loader
if l, err = NewFileSystemLoader(base); err != nil {
return
}
return &httpHamlHandler{l}, nil
}
type httpHamlHandler struct {
loader Loader
}
var defaultScope map[string]interface{}
func adjustSuffix(path string) string {
const htmlExt = ".html"
const htmExt = ".htm"
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
// swap .html extension ...
if strings.HasSuffix(path, htmlExt) || strings.HasSuffix(path, htmExt) {
path = path[:strings.LastIndex(path, ".")]
return path + ".haml"
}
if strings.HasSuffix(path, "/") {
return path + "index.haml"
}
return path + "/index.haml"
}
func (h *httpHamlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
const indexPage = "/index.html"
path := r.URL.Path
// borrowed from net/http/fs.go
// redirect .../index.html to .../
// can't use Redirect() because that would make the path absolute,
// which would be a problem running under StripPrefix
if strings.HasSuffix(path, indexPage) {
newPath := "./"
if q := r.URL.RawQuery; q != "" {
newPath += "?" + q
}
w.Header().Set("Location", newPath)
w.WriteHeader(http.StatusMovedPermanently)
return
}
path = adjustSuffix(path)
if engine, err := h.loader.Load(path); err != nil {
http.NotFound(w, r)
} else {
w.Write(([]byte)(engine.Render(defaultScope)))
}
}