diff --git a/.gitignore b/.gitignore index 612aa72..c5148da 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *idea .env -examples \ No newline at end of file +examples +.vscode \ No newline at end of file diff --git a/README.MD b/README.MD index d0d524d..f950615 100644 --- a/README.MD +++ b/README.MD @@ -1 +1,62 @@ -# Cors \ No newline at end of file +

+ + Minima + +

+ +### 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 +
+ +```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 + +
+ +```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) +
+ +```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()) +``` \ No newline at end of file diff --git a/_example/main.go b/_example/main.go new file mode 100644 index 0000000..89eb6a9 --- /dev/null +++ b/_example/main.go @@ -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") +} diff --git a/assets/logo.png b/assets/logo.png new file mode 100644 index 0000000..0ee88fe Binary files /dev/null and b/assets/logo.png differ diff --git a/cors.go b/cors.go index 5ad3648..31d4f5b 100644 --- a/cors.go +++ b/cors.go @@ -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 -} \ No newline at end of file +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() +} diff --git a/go.mod b/go.mod index 84a1e10..d56153e 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index 2487be6..7209748 100644 --- a/go.sum +++ b/go.sum @@ -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=