-
Notifications
You must be signed in to change notification settings - Fork 3
/
middeware.go
55 lines (48 loc) · 1.42 KB
/
middeware.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
package main
import (
"log"
"net/http"
"strings"
"sync"
"time"
)
type baseMiddleware struct {
handler http.Handler
}
func newBaseMiddleware(handlerToWrap http.Handler) *baseMiddleware {
return &baseMiddleware{
handler: handlerToWrap,
}
}
func (rh *baseMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
// TODO
aborted := corsMW(rw, r, "GET", "POST")
if aborted {
return
}
start := time.Now()
// log.Printf("%s receive\n", r.URL.String())
rh.handler.ServeHTTP(rw, r)
log.Printf("%s elapsed %s \n", r.URL.String(), time.Since(start))
}
var (
corsMWMethods string
onceMethodSet sync.Once
)
// CORS middleware
func corsMW(rw http.ResponseWriter, r *http.Request, methods ...string) (aborted bool) {
onceMethodSet.Do(func() {
corsMWMethods = strings.Join(append(methods, "OPTIONS"), ",")
})
rw.Header().Set("Access-Control-Allow-Credentials", "true")
rw.Header().Set("Access-Control-Max-Age", "999999") // Allow javascript requests from localhost
rw.Header().Set("Access-Control-Allow-Methods", corsMWMethods) // We will use GET and POST methods
// rw.Header().Set("Access-Control-Allow-Headers", "Content-Type") // allow JSON interchange
rw.Header().Set("Access-Control-Allow-Headers", "content-type")
rw.Header().Set("Access-Control-Allow-Origin", "*") // Allow javascript requests from localhost
if r.Method == "OPTIONS" {
rw.WriteHeader(http.StatusNoContent)
return true
}
return false
}