-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.go
54 lines (47 loc) · 1.12 KB
/
handler.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
package swagserver
import (
"html/template"
"net/http"
"path"
"strings"
"github.com/syllabix/swagserver/config"
"github.com/syllabix/swagserver/theme"
)
type tmpldata struct {
SpecURL string
Theme theme.Name
BasePath string
}
type handler struct {
fileserver http.Handler
tmpl *template.Template
specURL string
basePath string
theme theme.Name
}
func isStaticFile(path string) bool {
return strings.HasSuffix(path, ".js") ||
strings.HasSuffix(path, ".css") ||
strings.HasSuffix(path, ".png")
}
func newhandler(settings config.Settings, fileserver http.Handler, tmpl *template.Template) *handler {
return &handler{
fileserver: fileserver,
tmpl: tmpl,
specURL: settings.SwaggerSpecURL,
basePath: strings.TrimRight(settings.URLPath, "/"),
theme: settings.Theme,
}
}
func (s *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if isStaticFile(r.URL.Path) {
dir, _ := path.Split(r.URL.Path)
http.StripPrefix(dir, s.fileserver).ServeHTTP(w, r)
} else {
s.tmpl.Execute(w, tmpldata{
SpecURL: s.specURL,
Theme: s.theme,
BasePath: s.basePath,
})
}
}