Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*idea
.env
examples
examples
.vscode
63 changes: 62 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
@@ -1 +1,62 @@
# Cors
<p align="center">
<a href="https://gominima.studio">
<img alt="Minima" src="./assets/logo.png" />
</a>
</p>

### This is package is wrapper based on [rs/cors](https://github.com/rs/cors) package made for minima.

___

# Geting Started
### Install the package using `go get github.com/gominima/cors` and call it in your main function
<br>

```go
package main

import (
"github.com/gominima/cors"
"github.com/gominima/minima"
)

func main() {
app := minima.New()
crs := cors.New()
app.Get("/", func(res *minima.Response, req *minima.Request) {
res.OK().Send("Hello World")
res.CloseConn()
})
app.Use(crs.AllowAll())
app.Listen(":3000")
}
```
### Try curling the the localhost the result would be something like this

<br>

```sh
$ curl -D - -H 'Origin: http://abc.com' http://localhost:3000/
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Vary: Origin
Date: Wed, 09 Feb 2022 03:42:42 GMT
Content-Length: 11
Content-Type: text/plain; charset=utf-8
```
## Custom Cors

### Using your own custom cors config is as simple as it gets thanks to [rs/cors](https://github.com/rs/cors)
<br>

```go
app := minima.New()
crs := cors.New()
c := crs.NewCors(cors.Options{
AllowedOrigins: []string{"http://ur_url.com"},
AllowCredentials: true,
// Enable Debugging for testing, consider disabling in production
Debug: true,
})
app.Use(c())
```
17 changes: 17 additions & 0 deletions _example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"github.com/gominima/cors"
"github.com/gominima/minima"
)

func main() {
app := minima.New()
crs := cors.New()
app.Get("/", func(res *minima.Response, req *minima.Request) {
res.OK().Send("Hello World")
res.CloseConn()
})
app.Use(crs.AllowAll())
app.Listen(":3000")
}
Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 66 additions & 5 deletions cors.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,69 @@
package cors

import "github.com/gominima/minima"
import (
"github.com/gominima/minima"
"github.com/rs/cors"
"net/http"
)

type cors struct {
res *minima.Response
req *minima.Request
}
type Options = cors.Options

/**
@info The CorsWrapper for minima's instancr
@property {*cors.Cors} [raw] The raw cors instance around which wrapper build
@property {bool} [optionPassthrough] The bool value of options
*/
type corsWrapper struct {
*cors.Cors
optionPassthrough bool
}

/**
@info Creates a corsWrapper instance
@return {corsWrapper}
*/
func New() *corsWrapper {
return &corsWrapper{}
}

/**
@info Builds the raw cors to minima handler
@return {minima.Handler}
*/
func (c *corsWrapper) Build() minima.Handler {
return func(res *minima.Response, req *minima.Request) {
c.HandlerFunc(res.Raw(), req.Raw())
if !c.optionPassthrough && req.Method() == http.MethodOptions && req.Header().Get("Access-Control-Request-Method") != "" {
res.OK()
res.CloseConn()
}

}
}

/**
@info Sets headers to allow all origins to make a request
@return {minima.Handler}
*/
func (c *corsWrapper) AllowAll() minima.Handler {
crs := &corsWrapper{Cors: cors.AllowAll()}
return crs.Build()
}

/**
@info Sets headers based on default cors setup
@return {minima.Handler}
*/
func (c *corsWrapper) Default() minima.Handler {
crs := &corsWrapper{Cors: cors.Default()}
return crs.Build()
}

/**
@info Sets headers based on the options given
@return {minima.Handler}
*/
func (c *corsWrapper) NewCors(options Options) minima.Handler {
crs := &corsWrapper{Cors: cors.New(options), optionPassthrough: options.OptionsPassthrough}
return crs.Build()
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/gominima/cors

go 1.17

require github.com/gominima/minima v0.1.2-alpha // indirect
require (
github.com/gominima/minima v0.1.2-alpha.0.20220207142511-7e3d93afcb00 // indirect
github.com/rs/cors v1.8.2 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/gominima/minima v0.1.2-alpha h1:bkF+6ov/RrSebS6UQlO2FZqwzUVLcqFbxoC2Rcii/K8=
github.com/gominima/minima v0.1.2-alpha/go.mod h1:cK4qIs4pa3puqyRlWrHS5ThQpPUDFz4p+u5WuEEC7HQ=
github.com/gominima/minima v0.1.2-alpha.0.20220207142511-7e3d93afcb00 h1:3w3bNtI97sLyT5kx856NHVE2EYsRCL2eNTAbrnQ7Yd4=
github.com/gominima/minima v0.1.2-alpha.0.20220207142511-7e3d93afcb00/go.mod h1:P5UfLGNw2HbXoGrP0MOavT5mxfpMfZZHbhwCsTy257c=
github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U=
github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=