Middleware cors

Enable CORS interceptor/middleware for the server.

Installation

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

General options

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

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

CORS options

Interceptor for grpc-gateway.

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

Quick start

1.Create boot.yaml

---
grpc:
  - 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 main

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

// 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 grpc.interceptors.cors.allowOrigins to http://localhost:8080 in order to block request.

---
grpc:
  - 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 December 12, 2021 : Update based on rk-boot@v1.4.0 (233e143)