2019-06-03 20:44:43 +03:00
|
|
|
// Package event provides a handler which publishes an event
|
|
|
|
package event
|
|
|
|
|
|
|
|
import (
|
2019-11-18 19:37:45 +03:00
|
|
|
"encoding/json"
|
2019-06-03 20:44:43 +03:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-06-07 15:53:42 +03:00
|
|
|
"github.com/google/uuid"
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/api/handler"
|
|
|
|
proto "github.com/micro/go-micro/v3/api/proto"
|
|
|
|
"github.com/micro/go-micro/v3/util/ctx"
|
2020-03-26 14:29:28 +03:00
|
|
|
"github.com/oxtoacart/bpool"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
bufferPool = bpool.NewSizedBufferPool(1024, 8)
|
2019-06-03 20:44:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type event struct {
|
2020-03-26 14:29:28 +03:00
|
|
|
opts handler.Options
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
Handler = "event"
|
|
|
|
versionRe = regexp.MustCompilePOSIX("^v[0-9]+$")
|
|
|
|
)
|
|
|
|
|
|
|
|
func eventName(parts []string) string {
|
|
|
|
return strings.Join(parts, ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
func evRoute(ns, p string) (string, string) {
|
|
|
|
p = path.Clean(p)
|
|
|
|
p = strings.TrimPrefix(p, "/")
|
|
|
|
|
|
|
|
if len(p) == 0 {
|
|
|
|
return ns, "event"
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(p, "/")
|
|
|
|
|
|
|
|
// no path
|
|
|
|
if len(parts) == 0 {
|
|
|
|
// topic: namespace
|
|
|
|
// action: event
|
|
|
|
return strings.Trim(ns, "."), "event"
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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], ".")
|
|
|
|
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], ".")
|
|
|
|
action := eventName(parts[1:])
|
|
|
|
|
|
|
|
return topic, action
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *event) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2020-03-26 14:29:28 +03:00
|
|
|
bsize := handler.DefaultMaxRecvSize
|
|
|
|
if e.opts.MaxRecvSize > 0 {
|
|
|
|
bsize = e.opts.MaxRecvSize
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, bsize)
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
// request to topic:event
|
|
|
|
// create event
|
|
|
|
// publish to topic
|
|
|
|
|
2020-03-26 14:29:28 +03:00
|
|
|
topic, action := evRoute(e.opts.Namespace, r.URL.Path)
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
// create event
|
|
|
|
ev := &proto.Event{
|
|
|
|
Name: action,
|
|
|
|
// TODO: dedupe event
|
2019-06-07 15:53:42 +03:00
|
|
|
Id: fmt.Sprintf("%s-%s-%s", topic, action, uuid.New().String()),
|
2019-06-03 20:44:43 +03:00
|
|
|
Header: make(map[string]*proto.Pair),
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// set headers
|
|
|
|
for key, vals := range r.Header {
|
|
|
|
header, ok := ev.Header[key]
|
|
|
|
if !ok {
|
|
|
|
header = &proto.Pair{
|
|
|
|
Key: key,
|
|
|
|
}
|
|
|
|
ev.Header[key] = header
|
|
|
|
}
|
|
|
|
header.Values = vals
|
|
|
|
}
|
|
|
|
|
|
|
|
// set body
|
2019-11-18 19:37:45 +03:00
|
|
|
if r.Method == "GET" {
|
|
|
|
bytes, _ := json.Marshal(r.URL.Query())
|
|
|
|
ev.Data = string(bytes)
|
|
|
|
} else {
|
2020-03-26 14:29:28 +03:00
|
|
|
// Read body
|
|
|
|
buf := bufferPool.Get()
|
|
|
|
defer bufferPool.Put(buf)
|
|
|
|
if _, err := buf.ReadFrom(r.Body); err != nil {
|
2019-11-18 19:37:45 +03:00
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2020-03-26 14:29:28 +03:00
|
|
|
ev.Data = buf.String()
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get client
|
2020-04-12 16:29:38 +03:00
|
|
|
c := e.opts.Client
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
// create publication
|
|
|
|
p := c.NewMessage(topic, ev)
|
|
|
|
|
|
|
|
// publish event
|
|
|
|
if err := c.Publish(ctx.FromRequest(r), p); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *event) String() string {
|
|
|
|
return "event"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandler(opts ...handler.Option) handler.Handler {
|
|
|
|
return &event{
|
2020-03-26 14:29:28 +03:00
|
|
|
opts: handler.NewOptions(opts...),
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
}
|