-
Notifications
You must be signed in to change notification settings - Fork 64
/
nats.go
95 lines (94 loc) · 2.14 KB
/
nats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package plugin
// import (
// "io/ioutil"
// "net/http"
//
// "github.com/golang/protobuf/proto"
// "github.com/labstack/echo"
// "github.com/nats-io/nats"
// )
//
// type (
// NATS struct {
// Base `json:",squash"`
// Subject string `json:"subject"`
// // Publish *natsPub `json:"publish"`
// // Request *natsReq `json:"request"`
// Async bool `json:"async"`
// conn *nats.Conn
// }
//
// // natsPub struct {
// // Subject string `json:"subject"`
// // }
// //
// // natsReq struct {
// // Timeout time.Duration `json:"timeout"`
// // }
// )
//
// func (n *NATS) Initialize() (err error) {
// // Defaults
// // if n.Request != nil {
// // // TODO: https://github.com/nats-io/nats/blob/master/TODO.md
// // if n.Request.Timeout == 0 {
// // n.Request.Timeout = 1 * time.Minute
// // }
// // }
//
// // Initialize
// if n.conn, err = nats.Connect(nats.DefaultURL); err != nil {
// return
// }
// // TODO: defer n.conn.Close()
// return
// }
//
// func (*NATS) Priority() int {
// return 1
// }
//
// func (n *NATS) Process(next echo.HandlerFunc) echo.HandlerFunc {
// return func(c echo.Context) (err error) {
// // Message
// body := c.Request().Body
// msg := new(Message)
// msg.ExternalId = "xxx"
// msg.Body, err = ioutil.ReadAll(body) // Body
// if err != nil {
// return
// }
// b, err := proto.Marshal(msg)
// if err != nil {
// return err
// }
//
// // Publish
// // TODO: in a goroutine?
// // if n.Publish != nil {
// if err = n.conn.Publish(n.Subject, b); err != nil {
// return
// }
// // }
//
// // // Request/reply
// // if n.Request != nil {
// // rep, err := n.conn.Request(n.Request.Subject, b, n.Request.Timeout)
// // if err != nil {
// // return err
// // }
// // msg := new(Message)
// // if err := proto.Unmarshal(rep.Data, msg); err != nil {
// // return err
// // }
// // for k, v := range msg.Header { // Copy headers
// // c.Response().Header().Add(k, v)
// // }
// // c.Response().WriteHeader(200)
// // _, err = c.Response().Write(msg.Body)
// // return err
// // }
//
// return c.NoContent(http.StatusOK)
// }
// }