Refactor CORS and CSRF middleware to use options directly and remove default config functions
This commit is contained in:
@ -6,38 +6,12 @@ import (
|
||||
"github.com/rs/cors"
|
||||
)
|
||||
|
||||
type CORSConfig struct {
|
||||
AllowedOrigins []string
|
||||
AllowedMethods []string
|
||||
AllowedHeaders []string
|
||||
ExposedHeaders []string
|
||||
AllowCredentials bool
|
||||
MaxAge int
|
||||
}
|
||||
|
||||
func DefaultCORSConfig() CORSConfig {
|
||||
return CORSConfig{
|
||||
AllowedOrigins: []string{},
|
||||
AllowedMethods: []string{"GET"},
|
||||
AllowedHeaders: []string{},
|
||||
ExposedHeaders: []string{},
|
||||
AllowCredentials: false,
|
||||
MaxAge: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// CORS middleware handles Cross-Origin Resource Sharing
|
||||
func CORS(config CORSConfig) Middleware {
|
||||
c := cors.New(cors.Options{
|
||||
AllowedOrigins: config.AllowedOrigins,
|
||||
AllowedMethods: config.AllowedMethods,
|
||||
AllowedHeaders: config.AllowedHeaders,
|
||||
ExposedHeaders: config.ExposedHeaders,
|
||||
AllowCredentials: config.AllowCredentials,
|
||||
MaxAge: config.MaxAge,
|
||||
})
|
||||
func CORS(options cors.Options) Middleware {
|
||||
// Create a CORS handler with the provided options
|
||||
c := cors.New(options)
|
||||
|
||||
return func(next http.Handler) http.Handler {
|
||||
return c.Handler(next)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user