diff --git a/internal/core/router/router.go b/internal/core/router/router.go index 91947af..4fb1ac6 100644 --- a/internal/core/router/router.go +++ b/internal/core/router/router.go @@ -29,7 +29,7 @@ type Router struct { type Option func(*Router) // Default CORS options -var _ = middleware.CORSOptions{ +var defCorsOpts = middleware.CORSOptions{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, AllowedHeaders: []string{"Content-Type", "Authorization"}, diff --git a/pkg/lessgo/less.go b/pkg/lessgo/less.go index 5f9317b..5ac6f4d 100644 --- a/pkg/lessgo/less.go +++ b/pkg/lessgo/less.go @@ -220,7 +220,7 @@ func App(options ...router.Option) *Router { // New Cors Options. // -// Example +// Example (default to) // // corsOptions := LessGo.NewCorsOptions( // []string{"*"}, // Allow all origins @@ -229,6 +229,20 @@ func App(options ...router.Option) *Router { // // ) func NewCorsOptions(origins []string, methods []string, headers []string) *CORSOptions { + var defCorsOpts = middleware.CORSOptions{ + AllowedOrigins: []string{"*"}, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Content-Type", "Authorization"}, + } + if len(origins) == 0 { + origins = append(origins, defCorsOpts.AllowedOrigins...) + } + if len(headers) == 0 { + headers = append(headers, defCorsOpts.AllowedHeaders...) + } + if len(methods) == 0 { + methods = append(methods, defCorsOpts.AllowedMethods...) + } return middleware.NewCorsOptions(origins, methods, headers) }