Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/wuwenbao/gocors

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/wuwenbao/gocors

  • v0.1.0
  • Source
  • Go
  • Socket score

Version published
Created
Source

gcors

简单粗暴的解决GoWeb跨域

使用方式:只要实现了http.handler接口即可轻松对接

go默认web服务使用

package main

import (
    "log"
    "net/http"
    
    "github.com/wuwenbao/gcors"
)

func main() {
    mux := http.NewServeMux()
    
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("cors"))
    })
    
    //cors := gcors.New(mux) //默认参数为 *
    cors := gcors.New(
        mux,
        gcors.WithOrigin("*"),
        gcors.WithMethods("*"),
        gcors.WithHeaders("*"),
    )
    
    log.Fatal(http.ListenAndServe(":8080", cors))
}

第三方gorilla/mux

package main

import (
    "log"
    "net/http"
    
    "github.com/gorilla/mux"
    "github.com/wuwenbao/gcors"
)

func main() {
    router := mux.NewRouter()
    
    router.Methods("GET").Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("cors"))
    })
    
    //cors := gcors.New(mux) //默认参数为 *
    cors := gcors.NewCors(
        router,
        gcors.WithOrigin("*"),
        gcors.WithMethods("*"),
        gcors.WithHeaders("*"),
    )
    
    log.Fatal(http.ListenAndServe(":8080", cors))
}

FAQs

Package last updated on 13 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc