fasthttpcors

package module
v0.0.0-...-d4c0719 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2017 License: MIT Imports: 6 Imported by: 2

README

fasthttpcors

Go Report Card

Cors handler for fasthttp server

Read First | Installation | Usage | License

Handling CORS in fasthttp

Read First

Installation

  • go get github.com/adhityaramadhanus/fasthttpcors

Usage

package main

import (
	"log"

	cors "github.com/AdhityaRamadhanus/fasthttpcors"
	"github.com/valyala/fasthttp"
)

func main() {
	withCors := cors.NewCorsHandler(cors.Options{
		// if you leave allowedOrigins empty then fasthttpcors will treat it as "*"
		AllowedOrigins: []string{"http://example.com"}, // Only allow example.com to access the resource
		// if you leave allowedHeaders empty then fasthttpcors will accept any non-simple headers
		AllowedHeaders: []string{"x-something-client", "Content-Type"}, // only allow x-something-client and Content-Type in actual request
		// if you leave this empty, only simple method will be accepted
		AllowedMethods:   []string{"GET", "POST"}, // only allow get or post to resource
		AllowCredentials: false,                   // resource doesn't support credentials
		AllowMaxAge:      5600,                    // cache the preflight result
		Debug:            true,
	})
	if err := fasthttp.ListenAndServe(":8080", withCors.CorsMiddleware(RequestHandler)); err != nil {
		log.Fatalf("Error in ListenAndServe: %s", err)
	}
}

func RequestHandler(ctx *fasthttp.RequestCtx) {
	ctx.SetContentType("plain/text")
	ctx.SetStatusCode(200)
	ctx.SetBodyString("OK")
}

TODO

  • add test

License

MIT © [Adhitya Ramadhanus]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CorsHandler

type CorsHandler struct {
	// contains filtered or unexported fields
}

func DefaultHandler

func DefaultHandler() *CorsHandler

func NewCorsHandler

func NewCorsHandler(options Options) *CorsHandler

func (*CorsHandler) CorsMiddleware

func (c *CorsHandler) CorsMiddleware(innerHandler fasthttp.RequestHandler) fasthttp.RequestHandler

type Logger

type Logger interface {
	Log(...interface{})
}

func NewLogger

func NewLogger(w io.Writer) Logger

NewLogger return normal tracer

func OffLogger

func OffLogger() Logger

OffLogger return tracer that do nothing

type Options

type Options struct {
	AllowedOrigins   []string
	AllowedHeaders   []string
	AllowMaxAge      int
	AllowedMethods   []string
	ExposedHeaders   []string
	AllowCredentials bool
	Debug            bool
}

Options is struct that defined cors properties

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL