Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize code and bug fix #13

Merged
merged 14 commits into from
Aug 28, 2023
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ Fiber http driver for Goravel.
go get -u github.com/goravel/fiber
```

2. Register service provider, make sure it is registered first.
2. Register service provider

```
// config/app.go
import "github.com/goravel/fiber"

"providers": []foundation.ServiceProvider{
&fiber.ServiceProvider{},
...
&fiber.ServiceProvider{},
}
```

Expand All @@ -47,15 +47,14 @@ import (
"default": "fiber",

"drivers": map[string]any{
...
"fiber": map[string]any{
// prefork mode, see https://docs.gofiber.io/api/fiber/#config
"prefork": false,
"route": func() (route.Engine, error) {
return fiberfacades.Route(), nil
},
},
}
},
```

## Testing
Expand Down
25 changes: 25 additions & 0 deletions config/cors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"github.com/goravel/framework/facades"
)

func init() {
config := facades.Config()
config.Add("cors", map[string]any{
// Cross-Origin Resource Sharing (CORS) Configuration
//
// Here you may configure your settings for cross-origin resource sharing
// or "CORS". This determines what cross-origin operations may execute
// in web browsers. You are free to adjust these settings as needed.
//
// To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
"paths": []string{"*"},
"allowed_methods": []string{"*"},
"allowed_origins": []string{"*"},
"allowed_headers": []string{"*"},
"exposed_headers": []string{""},
"max_age": 0,
"supports_credentials": false,
})
}
3 changes: 1 addition & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"time"

"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"

"github.com/goravel/framework/contracts/http"
"github.com/valyala/fasthttp"
)

func Background() http.Context {
Expand Down
130 changes: 130 additions & 0 deletions cors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package fiber

import (
"fmt"
"strings"

"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/goravel/framework/contracts/http"
)

func Cors() http.Middleware {
return func(ctx http.Context) {
path := ctx.Request().Path()
corsPaths, ok := ConfigFacade.Get("cors.paths").([]string)
if !ok {
ctx.Request().Next()
return
}

needCors := false
for _, corsPath := range corsPaths {
corsPath = pathToFiberPath(corsPath)
if strings.HasSuffix(corsPath, "*") {
corsPath = strings.ReplaceAll(corsPath, "*", "")
if corsPath == "" || strings.HasPrefix(strings.TrimPrefix(path, "/"), strings.TrimPrefix(corsPath, "/")) {
needCors = true
break
}
} else {
if strings.TrimPrefix(path, "/") == strings.TrimPrefix(corsPath, "/") {
needCors = true
break
}
}
}

if !needCors {
ctx.Request().Next()
return
}

fiberCtx := ctx.(*Context)
if err := cors.New(cors.Config{
AllowMethods: allowedMethods(),
AllowOrigins: allowedOrigins(),
AllowHeaders: allowedHeaders(),
ExposeHeaders: exposedHeaders(),
MaxAge: ConfigFacade.GetInt("cors.max_age"),
AllowCredentials: ConfigFacade.GetBool("cors.supports_credentials"),
})(fiberCtx.Instance()); err != nil {
panic(err)
}
}
}

func allowedMethods() string {
var allowedMethods string
allowedMethodConfigs := ConfigFacade.Get("cors.allowed_methods").([]string)
for i, method := range allowedMethodConfigs {
if method == "*" {
allowedMethods = fmt.Sprintf("%s,%s,%s,%s,%s,%s", http.MethodGet, http.MethodPost, http.MethodHead, http.MethodPut, http.MethodDelete, http.MethodPatch)
break
}
if i == len(allowedMethodConfigs)-1 {
allowedMethods += method
break
}

allowedMethods += method + ","
}

return allowedMethods
}

func allowedOrigins() string {
var allowedOrigins string
allowedOriginConfigs := ConfigFacade.Get("cors.allowed_origins").([]string)
for i, origin := range allowedOriginConfigs {
if origin == "*" {
allowedOrigins = "*"
break
}
if i == len(allowedOriginConfigs)-1 {
allowedOrigins += origin
break
}

allowedOrigins += origin + ","
}

return allowedOrigins
}

func allowedHeaders() string {
var allowedHeaders string
allowedHeaderConfigs := ConfigFacade.Get("cors.allowed_headers").([]string)
for i, header := range allowedHeaderConfigs {
if header == "*" {
allowedHeaders = ""
break
}
if i == len(allowedHeaderConfigs)-1 {
allowedHeaders += header
break
}

allowedHeaders += header + ","
}

return allowedHeaders
}

func exposedHeaders() string {
var exposedHeaders string
exposedHeaderConfigs := ConfigFacade.Get("cors.exposed_headers").([]string)
for i, header := range exposedHeaderConfigs {
if header == "*" {
exposedHeaders = ""
break
}
if i == len(exposedHeaderConfigs)-1 {
exposedHeaders += header
break
}

exposedHeaders += header + ","
}

return exposedHeaders
}
Loading