Middleware cors

Enable CORS interceptor/middleware for the server.

Installation

go get github.com/rookie-ninja/rk-boot/gin

General options

These are general options to start a Gin server with rk-boot

name description type default value
gin.name The name of gin server string N/A
gin.port The port of gin server integer nil, server won’t start
gin.enabled Enable gin entry bool false
gin.description Description of gin entry. string ""

CORS options

name description type default value
gin.interceptors.cors.enabled Enable cors interceptor boolean false
gin.interceptors.cors.allowOrigins Provide allowed origins with wildcard enabled. []string *
gin.interceptors.cors.allowMethods Provide allowed methods returns as response header of OPTIONS request. []string All http methods
gin.interceptors.cors.allowHeaders Provide allowed headers returns as response header of OPTIONS request. []string Headers from request
gin.interceptors.cors.allowCredentials Returns as response header of OPTIONS request. bool false
gin.interceptors.cors.exposeHeaders Provide exposed headers returns as response header of OPTIONS request. []string ""
gin.interceptors.cors.maxAge Provide max age returns as response header of OPTIONS request. int 0

Quick start

1.Create boot.yaml

---
gin:
  - name: greeter                     # Required
    port: 8080                        # Required
    enabled: true                     # Required
    commonService:
      enabled: true                   # Optional, default: false
    interceptors:
      cors:
        enabled: true                 # Optional, default: false
        # Accept all origins from localhost
        allowOrigins:
          - "http://localhost:*"      # Optional, default: *

2.Create main.go

// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package rkdemo

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
	_ "github.com/rookie-ninja/rk-boot/gin"
)

// Application entrance.
func main() {
	// Create a new boot instance.
	boot := rkboot.NewBoot()

	// Bootstrap
	boot.Bootstrap(context.Background())

	// Wait for shutdown sig
	boot.WaitForShutdownSig(context.Background())
}

3.Create cors.html

<!DOCTYPE html>
<html>
<body>

<h1>CORS Test</h1>

<p>Call http://localhost:8080/rk/v1/healthy</p>

<script type="text/javascript">
    window.onload = function() {
        var apiUrl = 'http://localhost:8080/rk/v1/healthy';
        fetch(apiUrl).then(response => response.json()).then(data => {
            document.getElementById("res").innerHTML = data["healthy"]
        }).catch(err => {
            document.getElementById("res").innerHTML = err
        });
    };
</script>

<h4>Response: </h4>
<p id="res"></p>

</body>
</html>

4.Directory hierarchy

.
├── boot.yaml
├── cors.html
├── go.mod
├── go.sum
└── main.go

0 directories, 5 files

5.Validate

Open cors.html

6.With blocking CORS

Set gin.interceptors.cors.allowOrigins to http://localhost:8080 in order to block request.

---
gin:
  - name: greeter                     # Required
    port: 8080                        # Required
    enabled: true                     # Required
    commonService:
      enabled: true                   # Optional, default: false
    interceptors:
      cors:
        enabled: true                 # Optional, default: false
        allowOrigins:
          - "http://localhost:8080"   # Optional, default: *

Cheers

Last modified January 17, 2022 : Upgrade gin docs based on newest version (4513f9e)