forked from grafana-cold-storage/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
59 lines (52 loc) · 1021 Bytes
/
Copy pathmiddleware.go
File metadata and controls
59 lines (52 loc) · 1021 Bytes
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
package middleware
import (
"errors"
"net/http"
"strconv"
"github.com/rs/cors"
"gopkg.in/macaron.v1"
)
type Context struct {
*macaron.Context
OrgId int
}
func OrgMiddleware() macaron.Handler {
return func(c *macaron.Context) {
org, err := getOrg(c.Req.Request)
if err != nil {
c.PlainText(400, []byte(err.Error()))
return
}
ctx := &Context{
Context: c,
OrgId: org,
}
c.Map(ctx)
}
}
func getOrg(req *http.Request) (int, error) {
orgStr := req.Header.Get("x-org-id")
if orgStr == "" {
return 0, nil
}
org, err := strconv.Atoi(orgStr)
if err != nil {
return 0, errors.New("bad org-id")
}
return org, nil
}
func RequireOrg() macaron.Handler {
return func(c *Context) {
if c.OrgId == 0 {
c.PlainText(401, []byte("x-org-id header missing."))
}
}
}
func CorsHandler() macaron.Handler {
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "PUT", "POST", "DELETE"},
AllowCredentials: true,
})
return c.HandlerFunc
}