mirror of
https://github.com/go-micro/go-micro.git
synced 2025-11-29 21:47:44 +02:00
fix: easy lint fixes to api/ (#2567)
This commit is contained in:
@@ -26,6 +26,7 @@ type event struct {
|
||||
}
|
||||
|
||||
var (
|
||||
// Handler is the name of this handler.
|
||||
Handler = "event"
|
||||
versionRe = regexp.MustCompilePOSIX("^v[0-9]+$")
|
||||
)
|
||||
@@ -34,55 +35,56 @@ func eventName(parts []string) string {
|
||||
return strings.Join(parts, ".")
|
||||
}
|
||||
|
||||
func evRoute(ns, p string) (string, string) {
|
||||
p = path.Clean(p)
|
||||
p = strings.TrimPrefix(p, "/")
|
||||
func evRoute(namespace, myPath string) (string, string) {
|
||||
myPath = path.Clean(myPath)
|
||||
myPath = strings.TrimPrefix(myPath, "/")
|
||||
|
||||
if len(p) == 0 {
|
||||
return ns, "event"
|
||||
if len(myPath) == 0 {
|
||||
return namespace, Handler
|
||||
}
|
||||
|
||||
parts := strings.Split(p, "/")
|
||||
parts := strings.Split(myPath, "/")
|
||||
|
||||
// no path
|
||||
if len(parts) == 0 {
|
||||
// topic: namespace
|
||||
// action: event
|
||||
return strings.Trim(ns, "."), "event"
|
||||
return strings.Trim(namespace, "."), Handler
|
||||
}
|
||||
|
||||
// Treat /v[0-9]+ as versioning
|
||||
// /v1/foo/bar => topic: v1.foo action: bar
|
||||
if len(parts) >= 2 && versionRe.Match([]byte(parts[0])) {
|
||||
topic := ns + "." + strings.Join(parts[:2], ".")
|
||||
topic := namespace + "." + strings.Join(parts[:2], ".")
|
||||
action := eventName(parts[1:])
|
||||
|
||||
return topic, action
|
||||
}
|
||||
|
||||
// /foo => topic: ns.foo action: foo
|
||||
// /foo/bar => topic: ns.foo action: bar
|
||||
topic := ns + "." + strings.Join(parts[:1], ".")
|
||||
topic := namespace + "." + strings.Join(parts[:1], ".")
|
||||
action := eventName(parts[1:])
|
||||
|
||||
return topic, action
|
||||
}
|
||||
|
||||
func (e *event) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func (e *event) ServeHTTP(rsp http.ResponseWriter, req *http.Request) {
|
||||
bsize := handler.DefaultMaxRecvSize
|
||||
if e.opts.MaxRecvSize > 0 {
|
||||
bsize = e.opts.MaxRecvSize
|
||||
}
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, bsize)
|
||||
req.Body = http.MaxBytesReader(rsp, req.Body, bsize)
|
||||
|
||||
// request to topic:event
|
||||
// create event
|
||||
// publish to topic
|
||||
|
||||
topic, action := evRoute(e.opts.Namespace, r.URL.Path)
|
||||
topic, action := evRoute(e.opts.Namespace, req.URL.Path)
|
||||
|
||||
// create event
|
||||
ev := &proto.Event{
|
||||
event := &proto.Event{
|
||||
Name: action,
|
||||
// TODO: dedupe event
|
||||
Id: fmt.Sprintf("%s-%s-%s", topic, action, uuid.New().String()),
|
||||
@@ -91,49 +93,53 @@ func (e *event) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// set headers
|
||||
for key, vals := range r.Header {
|
||||
header, ok := ev.Header[key]
|
||||
for key, vals := range req.Header {
|
||||
header, ok := event.Header[key]
|
||||
if !ok {
|
||||
header = &proto.Pair{
|
||||
Key: key,
|
||||
}
|
||||
ev.Header[key] = header
|
||||
event.Header[key] = header
|
||||
}
|
||||
|
||||
header.Values = vals
|
||||
}
|
||||
|
||||
// set body
|
||||
if r.Method == "GET" {
|
||||
bytes, _ := json.Marshal(r.URL.Query())
|
||||
ev.Data = string(bytes)
|
||||
if req.Method == http.MethodGet {
|
||||
bytes, _ := json.Marshal(req.URL.Query())
|
||||
event.Data = string(bytes)
|
||||
} else {
|
||||
// Read body
|
||||
buf := bufferPool.Get()
|
||||
defer bufferPool.Put(buf)
|
||||
if _, err := buf.ReadFrom(r.Body); err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
if _, err := buf.ReadFrom(req.Body); err != nil {
|
||||
http.Error(rsp, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
ev.Data = buf.String()
|
||||
event.Data = buf.String()
|
||||
}
|
||||
|
||||
// get client
|
||||
c := e.opts.Client
|
||||
|
||||
// create publication
|
||||
p := c.NewMessage(topic, ev)
|
||||
p := c.NewMessage(topic, event)
|
||||
|
||||
// publish event
|
||||
if err := c.Publish(ctx.FromRequest(r), p); err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
if err := c.Publish(ctx.FromRequest(req), p); err != nil {
|
||||
http.Error(rsp, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e *event) String() string {
|
||||
return "event"
|
||||
return Handler
|
||||
}
|
||||
|
||||
// NewHandler returns a new event handler.
|
||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||
return &event{
|
||||
opts: handler.NewOptions(opts...),
|
||||
|
||||
Reference in New Issue
Block a user