Skip to content

Commit

Permalink
feat: def cors options
Browse files Browse the repository at this point in the history
  • Loading branch information
hokamsingh committed Aug 31, 2024
1 parent fb45b70 commit e37fb3a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/core/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
16 changes: 15 additions & 1 deletion pkg/lessgo/less.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func App(options ...router.Option) *Router {

// New Cors Options.
//
// Example
// Example (default to)
//
// corsOptions := LessGo.NewCorsOptions(
// []string{"*"}, // Allow all origins
Expand All @@ -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)
}

Expand Down

0 comments on commit e37fb3a

Please sign in to comment.