https://github.com/muhfajar/go-zero-cors-middleware
Go-Zero Middleware to handle CORS request
https://github.com/muhfajar/go-zero-cors-middleware
cors go-zero middleware
Last synced: 5 months ago
JSON representation
Go-Zero Middleware to handle CORS request
- Host: GitHub
- URL: https://github.com/muhfajar/go-zero-cors-middleware
- Owner: muhfajar
- License: mit
- Created: 2021-10-05T02:46:34.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-10-11T14:22:21.000Z (over 4 years ago)
- Last Synced: 2023-07-27T22:23:41.044Z (almost 3 years ago)
- Topics: cors, go-zero, middleware
- Language: Go
- Homepage:
- Size: 35.2 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go-Zero Middleware to handle CORS request
[](https://app.travis-ci.com/muhfajar/go-zero-cors-middleware)
[](https://codecov.io/gh/muhfajar/go-zero-cors-middleware)
## Getting Started
Install `middleware`:
go get github.com/muhfajar/go-zero-cors-middleware
After setting up your [Go-Zero](https://go-zero.dev/en/) project, update your `main` package to register CORS middleware.
```
package main
import (
"flag"
"fmt"
"github.com/muhfajar/go-zero-cors-middleware/test/greet/internal/config"
"github.com/muhfajar/go-zero-cors-middleware/test/greet/internal/handler"
"github.com/muhfajar/go-zero-cors-middleware/test/greet/internal/svc"
middleware "github.com/muhfajar/go-zero-cors-middleware"
"github.com/tal-tech/go-zero/core/conf"
"github.com/tal-tech/go-zero/rest"
)
var configFile = flag.String("f", "etc/greet-api.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)
// Register go-zero-cors-middleware handler to handle preflight request
cors := middleware.NewCORSMiddleware(&middleware.Options{})
// Add run option WithNotAllowedHandler and register `.Handler()` to handle `OPTIONS` request (preflight)
server := rest.MustNewServer(c.RestConf,
rest.WithNotAllowedHandler(cors.Handler()),
)
defer server.Stop()
handler.RegisterHandlers(server, ctx)
// Register go-zero-cors-middleware
server.Use(cors.Handle)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
```
Available options
```
AllowCredentials bool Indicates whether the request can include user credentials like cookies, HTTP authentication or client side SSL certificates.
AllowHeaders []string A list of non simple headers the client is allowed to use with cross-domain requests.
AllowMethods []string A list of methods the client is allowed to use with cross-domain requests.
ExposeHeaders []string Indicates which headers are safe to expose to the API of a CORS API specification.
```
Default value
```
AllowCredentials false
AllowHeaders []string{"Content-Type", "X-CSRF-Token", "Authorization", "AccessToken", "Token"}
AllowMethods []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"}
ExposeHeaders []string{"Content-Length", "Content-Type", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers"}
```
By default, if request `Origin` header value is null or not included in request and `AllowCredentials` is not set in options, `Access-Control-Allow-Origin` will be return `*`.
But if `AllowCredentials` set by `true` value and request `Origin` header value is present, `Access-Control-Allow-Origin` will be reflected by the request `Origin` value.
## Licenses
All source code is licensed under the [MIT License](https://github.com/muhfajar/go-zero-cors-middleware/main/LICENSE).