/
server.go
57 lines (42 loc) · 1.65 KB
/
server.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
package main
import (
"log"
"github.com/zubairhamed/canopus"
)
func main() {
server := canopus.NewServer()
server.Get("/hello", func(req canopus.Request) canopus.Response {
log.Println("Hello Called")
msg := canopus.ContentMessage(req.GetMessage().GetMessageId(), canopus.MessageAcknowledgment)
msg.SetStringPayload("Acknowledged with response : " + req.GetMessage().GetPayload().String())
res := canopus.NewResponse(msg, nil)
return res
})
server.Post("/hello", func(req canopus.Request) canopus.Response {
log.Println("Hello Called via POST")
msg := canopus.ContentMessage(req.GetMessage().GetMessageId(), canopus.MessageAcknowledgment)
msg.SetStringPayload("Acknowledged: " + req.GetMessage().GetPayload().String())
res := canopus.NewResponse(msg, nil)
return res
})
server.Get("/basic", func(req canopus.Request) canopus.Response {
msg := canopus.NewMessageOfType(canopus.MessageAcknowledgment, req.GetMessage().GetMessageId(), canopus.NewPlainTextPayload("Acknowledged"))
res := canopus.NewResponse(msg, nil)
return res
})
server.Get("/basic/json", func(req canopus.Request) canopus.Response {
msg := canopus.NewMessageOfType(canopus.MessageAcknowledgment, req.GetMessage().GetMessageId(), nil)
res := canopus.NewResponse(msg, nil)
return res
})
server.Get("/basic/xml", func(req canopus.Request) canopus.Response {
msg := canopus.NewMessageOfType(canopus.MessageAcknowledgment, req.GetMessage().GetMessageId(), nil)
res := canopus.NewResponse(msg, nil)
return res
})
server.OnMessage(func(msg canopus.Message, inbound bool) {
canopus.PrintMessage(msg)
})
server.ListenAndServe(":5683")
<-make(chan struct{})
}