forked from razonyang/clevergo-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cors.go
33 lines (28 loc) · 904 Bytes
/
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
package core
import (
"github.com/clevergo/clevergo"
"github.com/rs/cors"
)
type CORSConfig struct {
AllowedOrigins []string `koanf:"allowed_origins"`
AllowedMethods []string `koanf:"allowed_methods"`
AllowedHeaders []string `koanf:"allowed_headers"`
MaxAge int `koanf:"max_age"`
AllowedCredentials bool `koanf:"allow_credentials"`
Debug bool `koanf:"debug"`
}
func NewCORS(cfg CORSConfig) *cors.Cors {
return cors.New(cors.Options{
AllowedOrigins: cfg.AllowedOrigins,
AllowedHeaders: cfg.AllowedHeaders,
MaxAge: cfg.MaxAge,
AllowCredentials: cfg.AllowedCredentials,
AllowedMethods: cfg.AllowedMethods,
Debug: cfg.Debug,
})
}
type CORSMiddleware clevergo.MiddlewareFunc
func NewCORSMiddleware(cfg CORSConfig) CORSMiddleware {
fn := NewCORS(cfg).Handler
return CORSMiddleware(clevergo.WrapHH(fn))
}