/
context.go
46 lines (37 loc) · 1.1 KB
/
context.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
package httpdispatch
import (
"net/http"
)
var (
ctxParamKey = ctxParam{} // for http.Request.Context() introduced from go 1.7
)
type ctxParam struct{}
// ContextHandle defines container of registered http.Handler with useful context,
// such as package name, controller name and action name of handle.
type ContextHandle struct {
handler http.Handler
useCtx bool
}
// NewContextHandle returns *ContextHandle with handler info
func NewContextHandle(handler http.Handler, useContext bool) *ContextHandle {
return &ContextHandle{
handler: handler,
useCtx: useContext,
}
}
// FileHandle defines static files server context
type FileHandle struct {
*ContextHandle
}
// NewFileHandle returns *FileHandle with passed http.HandlerFunc
func NewFileHandle(fs http.FileSystem) *FileHandle {
return &FileHandle{
ContextHandle: NewContextHandle(http.FileServer(fs), false),
}
}
// Handle hijacks request path with filepath by overwrite
func (fh *FileHandle) Handle(w http.ResponseWriter, r *http.Request, ps Params) {
r.URL.Path = ps.ByName("filepath")
r.RequestURI = r.URL.String()
fh.handler.ServeHTTP(w, r)
}