-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhandler_config.go
More file actions
133 lines (118 loc) · 3.13 KB
/
handler_config.go
File metadata and controls
133 lines (118 loc) · 3.13 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package api
import (
"fmt"
"mime"
"mokapi/config/dynamic"
"net/http"
"path/filepath"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
type config struct {
Id string `json:"id"`
Url string `json:"url"`
Provider string `json:"provider"`
Time time.Time `json:"time"`
Refs []configRef `json:"refs,omitempty"`
Tags []string `json:"tags,omitempty"`
}
type configRef struct {
Id string `json:"id"`
Url string `json:"url"`
Provider string `json:"provider"`
Time time.Time `json:"time"`
}
func (h *handler) handleConfig(w http.ResponseWriter, r *http.Request) {
segments := strings.Split(r.URL.Path, "/")
if len(segments) == 3 {
h.getConfigs(w)
} else if len(segments) == 4 {
h.getConfigMetaData(w, segments[3])
} else if len(segments) == 5 {
h.getConfigData(w, r, segments[3])
} else {
w.WriteHeader(http.StatusBadRequest)
}
}
func (h *handler) getConfigs(w http.ResponseWriter) {
var configs []config
for _, c := range h.app.Configs {
configs = append(configs, toConfig(c))
}
w.Header().Set("Content-Type", "application/json")
writeJsonBody(w, configs)
}
func (h *handler) getConfigMetaData(w http.ResponseWriter, key string) {
c := h.app.FindConfig(key)
if c == nil {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
writeJsonBody(w, toConfig(c))
}
func (h *handler) getConfigData(w http.ResponseWriter, r *http.Request, key string) {
c := h.app.FindConfig(key)
if c == nil {
w.WriteHeader(http.StatusNotFound)
return
}
token := r.Header.Get("If-None-Match")
checksum := fmt.Sprintf(`"%x"`, c.Info.Checksum)
if token != "" && token == checksum {
w.WriteHeader(http.StatusNotModified)
return
}
path := c.Info.Kernel().Path()
ext := filepath.Ext(path)
mt := mime.TypeByExtension(filepath.Ext(ext))
if mt == "" {
if ext == "" {
values, err := mime.ExtensionsByType(c.Info.Kernel().ContentType)
if err == nil && len(values) > 0 {
ext = values[0]
path += ext
mt = mime.TypeByExtension(filepath.Ext(ext))
}
}
if mt == "" {
mt = "text/plain"
}
}
w.Header().Set("Last-Modified", c.Info.Time.UTC().Format(http.TimeFormat))
w.Header().Set("Content-Type", mt)
w.Header().Set("Content-Disposition", "inline; filename=\""+filepath.Base(path)+"\"")
w.Header().Set("ETag", checksum)
w.Header().Set("Cache-Control", "no-cache")
w.WriteHeader(http.StatusOK)
_, err := w.Write(c.Raw)
if err != nil {
log.Errorf("http write file %v failed: %v", c.Info.Url.String(), err)
}
}
func getConfigs(src []*dynamic.Config) (dst []config) {
for _, cfg := range src {
dst = append(dst, toConfig(cfg))
}
return
}
func toConfig(cfg *dynamic.Config) config {
var refs []configRef
for _, ref := range cfg.Refs.List(false) {
refs = append(refs, configRef{
Id: ref.Info.Key(),
Url: filepath.ToSlash(ref.Info.Path()),
Provider: ref.Info.Provider,
Time: ref.Info.Time,
})
}
return config{
Id: cfg.Info.Key(),
Url: filepath.ToSlash(cfg.Info.Path()),
Time: cfg.Info.Time,
Provider: cfg.Info.Provider,
Refs: refs,
Tags: cfg.Info.Tags,
}
}