cors

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2023 License: MIT Imports: 4 Imported by: 0

README

cors

CORS (Cross-Origin Resource Sharing) is a security feature implemented in web browsers that restricts web pages from making requests to a different domain than the one that served the web page. CORS middleware is used to enable cross-origin requests in web applications.

This package provides a middleware that can be used to enable CORS in a Lightning go web application. The middleware can be configured with various options to allow specific origins, HTTP methods, headers, and more.

Installation

go get github.com/{your_username}/{your_repository}/sdk

Usage

To use the cors middleware in your Go web application, you can import the cors package and add the middleware to your middleware chain. Here's an example:

package main

import (
	"github.com/go-labx/lightning"
	"github.com/lightning-contrib/cors"
)

func main() {
	app := lightning.DefaultApp()

    // Add the CORS middleware to the middleware chain
	app.Use(cors.Default())

    // Add your routes here
	app.Get("/ping", func(ctx *lightning.Context) {
		ctx.Success("hello world")
	})

    // Start the server
	app.Run()
}

By default, the cors middleware allows all origins (), all HTTP methods (GET, POST, PUT, DELETE), all headers (), and exposes all headers (*). You can customize these options by using the various option functions provided by the cors package.

Here's an example of how to customize the allowed origins:

app.Use(cors.New(
    cors.AllowOrigin([]string{"https://example.com", "https://www.example.com"}),
))

This will allow requests from https://example.com and https://www.example.com.

Options

The cors middleware can be customized with the following options:

  • AllowOrigin([]string): Sets the allowed origins. By default, all origins are allowed (*).
  • AllowMethods([]string): Sets the allowed HTTP methods. By default, GET, POST, PUT, and DELETE are allowed.
  • AllowHeaders([]string): Sets the allowed headers. By default, all headers are allowed (*).
  • ExposeHeaders([]string): Sets the exposed headers. By default, all headers are exposed (*).
  • SetMaxAge(int): Sets the max age in seconds for preflight requests. By default, the max age is set to 3600 seconds (1 hour).
  • AllowCredentials(bool): Sets whether credentials are allowed. By default, credentials are allowed.

API Documentation

For detailed API documentation and usage examples, please refer to the documentation.

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for more information.

License

This middleware is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowCredentials

func AllowCredentials(allowCredentials bool) option

AllowCredentials is an option function that sets whether credentials are allowed

func AllowHeaders

func AllowHeaders(headers []string) option

AllowHeaders is an option function that sets the allowed headers

func AllowMethods

func AllowMethods(methods []string) option

AllowMethods is an option function that sets the allowed HTTP methods

func AllowOrigin

func AllowOrigin(origin []string) option

AllowOrigin is an option function that sets the allowed origins

func Default

func Default() lightning.Middleware

Default returns the default CORS middleware

func ExposeHeaders

func ExposeHeaders(headers []string) option

ExposeHeaders is an option function that sets the exposed headers

func New

func New(options ...option) lightning.Middleware

New returns a new CORS middleware with the given options

func SetMaxAge

func SetMaxAge(seconds int) option

SetMaxAge is an option function that sets the max age

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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