代码拉取完成,页面将自动刷新
package cors
import (
"net/http"
"strings"
"github.com/micro/cli"
"github.com/micro/micro/plugin"
"github.com/rs/cors"
)
type allowedCors struct {
allowedHeaders []string
allowedOrigins []string
allowedMethods []string
}
func (ac *allowedCors) Flags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "cors-allowed-headers",
Usage: "Comma-seperated list of allowed headers",
EnvVar: "CORS_ALLOWED_HEADERS",
},
cli.StringFlag{
Name: "cors-allowed-origins",
Usage: "Comma-seperated list of allowed origins",
EnvVar: "CORS_ALLOWED_ORIGINS",
},
cli.StringFlag{
Name: "cors-allowed-methods",
Usage: "Comma-seperated list of allowed methods",
EnvVar: "CORS_ALLOWED_METHODS",
},
}
}
func (ac *allowedCors) Commands() []cli.Command {
return nil
}
func (ac *allowedCors) Handler() plugin.Handler {
return func(ha http.Handler) http.Handler {
hf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ha.ServeHTTP(w, r)
})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cors.New(cors.Options{
AllowedOrigins: ac.allowedOrigins,
AllowedMethods: ac.allowedMethods,
AllowedHeaders: ac.allowedHeaders,
AllowCredentials: true,
}).ServeHTTP(w, r, hf)
})
}
}
func (ac *allowedCors) Init(ctx *cli.Context) error {
ac.allowedHeaders = ac.parseAllowed(ctx, "cors-allowed-headers")
ac.allowedMethods = ac.parseAllowed(ctx, "cors-allowed-methods")
ac.allowedOrigins = ac.parseAllowed(ctx, "cors-allowed-origins")
return nil
}
func (ac *allowedCors) parseAllowed(ctx *cli.Context, flagName string) []string {
fv := ctx.String(flagName)
// no op
if len(fv) == 0 {
return nil
}
return strings.Split(fv, ",")
}
func (ac *allowedCors) String() string {
return "cors-allowed-(headers|origins|methods)"
}
// NewPlugin Creates the CORS Plugin
func NewPlugin() plugin.Plugin {
return &allowedCors{
allowedHeaders: []string{},
allowedOrigins: []string{},
allowedMethods: []string{},
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。