-
Notifications
You must be signed in to change notification settings - Fork 2
/
cors.go
69 lines (60 loc) · 1.5 KB
/
cors.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
package cors
import (
"github.com/gominima/minima"
"github.com/rs/cors"
"net/http"
)
type Options = cors.Options
/**
@info The CorsWrapper for minima's instancr
@property {*cors.Cors} [raw] The raw cors instance around which wrapper build
@property {bool} [optionPassthrough] The bool value of options
*/
type corsWrapper struct {
*cors.Cors
optionPassthrough bool
}
/**
@info Creates a corsWrapper instance
@return {corsWrapper}
*/
func New() *corsWrapper {
return &corsWrapper{}
}
/**
@info Builds the raw cors to minima handler
@return {minima.Handler}
*/
func (c *corsWrapper) Build() minima.Handler {
return func(res *minima.Response, req *minima.Request) {
c.HandlerFunc(res.Raw(), req.Raw())
if !c.optionPassthrough && req.Method() == http.MethodOptions && req.GetHeader("Access-Control-Request-Method") != "" {
res.OK()
res.CloseConn()
}
}
}
/**
@info Sets headers to allow all origins to make a request
@return {minima.Handler}
*/
func (c *corsWrapper) AllowAll() minima.Handler {
crs := &corsWrapper{Cors: cors.AllowAll()}
return crs.Build()
}
/**
@info Sets headers based on default cors setup
@return {minima.Handler}
*/
func (c *corsWrapper) Default() minima.Handler {
crs := &corsWrapper{Cors: cors.Default()}
return crs.Build()
}
/**
@info Sets headers based on the options given
@return {minima.Handler}
*/
func (c *corsWrapper) NewCors(options Options) minima.Handler {
crs := &corsWrapper{Cors: cors.New(options), optionPassthrough: options.OptionsPassthrough}
return crs.Build()
}