-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
117 lines (98 loc) · 2.64 KB
/
api.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package api
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"github.com/gin-gonic/gin"
"github.com/maxence-charriere/go-app/v9/pkg/app"
natsws "github.com/mlctrez/goapp-natsws"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/o1egl/govatar"
"image"
"image/jpeg"
)
type Api struct {
ns *server.Server
serverConn *nats.Conn
}
func New(ns *server.Server) *Api {
return &Api{ns: ns}
}
func (a *Api) Register(engine *gin.Engine) {
proxy := &natsws.Proxy{
Context: context.TODO(),
Manager: natsws.StaticManager(true, "ws://127.0.0.1:8242"),
}
engine.GET("/natsws/:clientId", gin.WrapH(proxy))
engine.GET("/echo/:who", a.echo)
a.setupNatsApi()
}
func (a *Api) setupNatsApi() {
var err error
a.serverConn, err = nats.Connect("", nats.InProcessServer(a.ns))
if err != nil {
app.Logf("error creating serverConn %w", err)
return
}
nc := a.serverConn
_, err = nc.Subscribe("chat.say", func(msg *nats.Msg) {
//fmt.Printf("Got: %q\n", msg.Data)
err = nc.Publish("chat.room", msg.Data)
if err != nil {
app.Logf("error publishing chat.room message %w", err)
}
})
if err != nil {
app.Logf("error subscribing to chat.say %w", err)
}
_, err = nc.Subscribe("govatar.female", func(msg *nats.Msg) {
var img image.Image
// always female and random
img, err = govatar.Generate(govatar.FEMALE)
if err != nil {
app.Logf("error generating govatar %w", err)
return
}
var buf bytes.Buffer
err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: 80})
if err != nil {
app.Logf("error generating jpeg %w", err)
return
}
err = msg.Respond([]byte("data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(buf.Bytes())))
if err != nil {
app.Logf("error responding to message %w", err)
return
}
})
if err != nil {
app.Logf("error subscribing to govatar.female %w", err)
return
}
}
func (a *Api) echo(c *gin.Context) {
if a.serverConn != nil {
who := c.Param("who")
err := a.serverConn.Publish(fmt.Sprintf("echo.%s", who), []byte("{{ Random 10 100}}"))
if err != nil {
app.Logf("error publishing message %w", err)
}
}
}
type NatsInfoMsg struct {
ServerId string `json:"server_id"`
ServerName string `json:"server_name"`
Version string `json:"version"`
Proto int `json:"proto"`
Go string `json:"go"`
Host string `json:"host"`
Port int `json:"port"`
Headers bool `json:"headers"`
MaxPayload int `json:"max_payload"`
ClientId int `json:"client_id"`
ClientIp string `json:"client_ip"`
Cluster string `json:"cluster"`
ConnectUrls []string `json:"connect_urls"`
}