Refactor CORS and CSRF middleware to use options directly and remove default config functions

This commit is contained in:
2025-04-29 03:19:47 -05:00
parent 7dbde25bcf
commit 1581fbca4b
3 changed files with 19 additions and 49 deletions

View File

@ -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)
}
}
}