-
Notifications
You must be signed in to change notification settings - Fork 2
/
static.go
96 lines (83 loc) · 2.35 KB
/
static.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
package static
import (
"mime"
"net/http"
"os"
"strings"
"go.uber.org/zap"
log "go.uber.org/zap"
)
// Config holds all config vars
type Config struct {
Prefix string `long:"prefix" default:"html" description:"EmbeddedFS prefix"`
}
// Service holds SOAP service
type Service struct {
Config *Config
Log *log.Logger
ListHandler func() []string
DataHandler func(name string) ([]byte, error)
InfoHandler func(name string) (os.FileInfo, error)
}
// New creates an Service object
func New(cfg Config, logger *log.Logger,
lh func() []string,
dh func(name string) ([]byte, error),
ih func(name string) (os.FileInfo, error),
) *Service {
return &Service{
Config: &cfg,
Log: logger,
ListHandler: lh,
DataHandler: dh,
InfoHandler: ih,
}
}
// SetupRouter add routes to mux
func (srv Service) SetupRouter(mux *http.ServeMux) {
for _, v := range srv.ListHandler() {
if !strings.HasPrefix(v, srv.Config.Prefix+"/") {
continue
}
uri := strings.TrimPrefix(strings.TrimSuffix(v, "index.html"), srv.Config.Prefix)
srv.Log.Info("Serve static", zap.String("uri", uri))
mux.HandleFunc(uri, srv.serveStatic(v))
}
}
func (srv Service) serveStatic(name string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
srv.Log.Info("Handle static", zap.String("file", name))
data, err := srv.DataHandler(name)
if err != nil {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusNotFound)
} else {
// TODO: add file info
w.Header().Set("Content-Type", mime.TypeByExtension(name))
w.Write(data)
}
}
}
// Proxy holds proxied handler
type Proxy struct {
h http.Handler
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
setupResponse(&w, r)
if (*r).Method == "OPTIONS" {
return
}
p.h.ServeHTTP(w, r)
}
// OptionsProxy returns proxy for Options request handler
func OptionsProxy(h http.Handler) http.Handler { // , opts ...Option
p := &Proxy{
h: h,
}
return p
}
func setupResponse(w *http.ResponseWriter, req *http.Request) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
(*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
(*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, x-grpc-web")
}