forked from motiv-labs/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
55 lines (46 loc) · 1.3 KB
/
setup.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 cors
import (
"github.com/asaskevich/govalidator"
"github.com/hellofresh/janus/pkg/plugin"
"github.com/hellofresh/janus/pkg/proxy"
"github.com/rs/cors"
)
// Config represents the CORS configuration
type Config struct {
AllowedOrigins []string `json:"domains"`
AllowedMethods []string `json:"methods"`
AllowedHeaders []string `json:"request_headers"`
ExposedHeaders []string `json:"exposed_headers"`
OptionsPassthrough bool `json:"options_passthrough"`
}
func init() {
plugin.RegisterPlugin("cors", plugin.Plugin{
Action: setupCors,
Validate: validateConfig,
})
}
func setupCors(def *proxy.RouterDefinition, rawConfig plugin.Config) error {
var config Config
err := plugin.Decode(rawConfig, &config)
if err != nil {
return err
}
mw := cors.New(cors.Options{
AllowedOrigins: config.AllowedOrigins,
AllowedMethods: config.AllowedMethods,
AllowedHeaders: config.AllowedHeaders,
ExposedHeaders: config.ExposedHeaders,
OptionsPassthrough: config.OptionsPassthrough,
AllowCredentials: true,
})
def.AddMiddleware(mw.Handler)
return nil
}
func validateConfig(rawConfig plugin.Config) (bool, error) {
var config Config
err := plugin.Decode(rawConfig, &config)
if err != nil {
return false, err
}
return govalidator.ValidateStruct(config)
}