-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgors.go
More file actions
86 lines (72 loc) · 2.2 KB
/
Copy pathgors.go
File metadata and controls
86 lines (72 loc) · 2.2 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
package gors
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
)
// Config is the plugin configuration.
type Config struct {
AllowedOrigins []string
AllowedHeaders []string
AllowedMethods []string
PreflightMaxAge int
// Disabled is used to disable all functionalities of plugin, when it's disabled it just passes the request through
Disabled bool
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
AllowedOrigins: []string{},
AllowedHeaders: []string{},
AllowedMethods: []string{http.MethodOptions},
PreflightMaxAge: 86400, // in seconds
Disabled: false,
}
}
// Gors is the plugin struct
type Gors struct {
next http.Handler
name string
Config *Config
}
// New creates a new plugin when Traefik gets started.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
return &Gors{
next: next,
name: name,
Config: config,
}, nil
}
func (g *Gors) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if g.Config.Disabled {
g.next.ServeHTTP(rw, req)
return
}
fmt.Println("here ", req.Method)
if contains(g.Config.AllowedOrigins, "*") {
rw.Header().Add("Access-Control-Allow-Origin", "*")
} else {
rw.Header().Add("Vary", "Origin")
rw.Header().Add("Access-Control-Allow-Origin", strings.Join(g.Config.AllowedOrigins, ", "))
}
rw.Header().Add("Access-Control-Allow-Methods", strings.Join(g.Config.AllowedMethods, ", "))
rw.Header().Add("Access-Control-Allow-Headers", strings.Join(g.Config.AllowedHeaders, ", "))
rw.Header().Add("Access-Control-Max-Age", strconv.Itoa(g.Config.PreflightMaxAge))
if req.Method == http.MethodOptions {
rw.WriteHeader(http.StatusNoContent)
return
}
g.next.ServeHTTP(rw, req)
}
// Utility functions
func contains(list []string, elem string) bool {
for _, value := range list {
if value == elem {
return true
}
}
return false
}
//f = () => {console.log("sf")}; const xhr = new XMLHttpRequest(); xhr.open("POST", "http://localhost:3030"); xhr.setRequestHeader("X-PINGOTHER", "pingpong"); xhr.setRequestHeader("Content-Type", "text/xml"); xhr.onreadystatechange = f; xhr.send("<person><name>Arun</name></person>");