44 lines
957 B
Go
44 lines
957 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"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,
|
|
})
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
return c.Handler(next)
|
|
}
|
|
}
|