The current repo belongs to Paused status, and some functions are restricted. For details, please refer to the description of repo status
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
Paused

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
metadata.go 1.28 KB
Copy Edit Raw Blame History
Asim Aslam authored 2017-10-11 17:26 . Support many equals
package metadata
import (
"net/http"
"strings"
"github.com/micro/cli"
"github.com/micro/go-micro/client"
"github.com/micro/micro/plugin"
)
type metadata struct {
md map[string]string
}
func (m *metadata) Flags() []cli.Flag {
return []cli.Flag{
cli.StringSliceFlag{
Name: "metadata",
Usage: "A list of key-value pairs to be forwarded as metadata",
EnvVar: "METADATA",
},
}
}
func (m *metadata) Commands() []cli.Command {
return nil
}
func (m *metadata) Handler() plugin.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for k, v := range m.md {
// set metadata
r.Header.Set(k, v)
}
// exec handler
h.ServeHTTP(w, r)
})
}
}
func (m *metadata) Init(ctx *cli.Context) error {
md := ctx.StringSlice("metadata")
// no op
if len(md) == 0 {
return nil
}
// iterate the string slice
for _, pair := range md {
parts := strings.Split(pair, "=")
if len(parts) < 2 {
continue
}
// set key-vals
m.md[parts[0]] = strings.Join(parts[1:], "=")
}
// wrap the client
client.DefaultClient = newClient(m.md)
return nil
}
func (m *metadata) String() string {
return "metadata"
}
func NewPlugin() plugin.Plugin {
return &metadata{
md: make(map[string]string),
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.9.3

Search

0d507c66 1850385 C8b1a773 1850385