-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqp_rest.go
77 lines (57 loc) · 1.33 KB
/
amqp_rest.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
package main
import (
"github.com/streadway/amqp"
"io"
"io/ioutil"
"net/http"
"strings"
)
func queueRequest(request *http.Request, channel *amqp.Channel) {
}
func sendErrorResponse(err error, w *http.ResponseWriter) {
}
func main() {
connection, err := amqp.Dial("amqp://localhost:5672")
if err != nil {
panic("Could not connect to amqp server")
}
channel, err := connection.Channel()
http.HandleFunc("/", func(w http.ResponseWriter, request *http.Request) {
uri_split := strings.Split("/", request.RequestURI)
if len(uri_split) < 1 {
//TODO error on this condition
}
exchange := uri_split[0]
routing_key := ""
content_type := request.Header.Get("ContentType")
if len(uri_split) > 1 {
routing_key = uri_split[1]
}
if content_type == "" {
content_type = "text/json"
}
body, err := ioutil.ReadAll(request.Body)
if err != nil {
sendErrorResponse(err, &w)
}
err = channel.Publish(
exchange,
routing_key,
false,
false,
amqp.Publishing{
Headers: amqp.Table{},
ContentType: content_type,
ContentEncoding: "",
Body: body,
DeliveryMode: amqp.Persistent,
Priority: 0,
},
)
if err != nil {
sendErrorResponse(err, &w)
}
io.WriteString(w, "Wrote hello world to queue\n")
})
http.ListenAndServe(":12345", nil)
}