-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
78 lines (64 loc) · 1.75 KB
/
main.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
package main
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"github.com/nats-io/nats.go"
)
func main() {
h1 := func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, fmt.Sprintf("Hello from a H1 for %q!\n", req.URL.Path))
}
// Handle via NATS.
natsHandleFunc("foo", h1)
// Handle via HTTP
http.HandleFunc("/foo", h1)
log.Printf("Listening on HTTP localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func natsConnect() *nats.Conn {
if nc, err := nats.Connect("localhost"); err == nil {
log.Printf("NATS connected to localhost")
return nc
} else if nc, err := nats.Connect("demo.nats.io"); err == nil {
log.Printf("NATS connected to demo.nats.io")
return nc
}
log.Fatalf("Could not connect to NATS System")
return nil
}
var nc *nats.Conn
func natsHandleFunc(subject string, handler func(http.ResponseWriter, *http.Request)) {
// NATS Setup
if nc == nil {
nc = natsConnect()
}
var _rb [512]byte
_, err := nc.Subscribe(subject, func(m *nats.Msg) {
// Determine if HTTP request format. For now assume its not and construct one.
buf := bytes.NewBuffer(m.Data)
req, err := http.NewRequest("GET", subject, buf)
if err != nil {
log.Printf("Error creating http request: %v", err)
}
rr := httptest.NewRecorder()
// Call into our handler.
handler(rr, req)
// Generate HTTP response.
r := rr.Result()
r.ContentLength = int64(rr.Body.Len())
respBuf := bytes.NewBuffer(_rb[:0])
r.Write(respBuf)
rb := respBuf.Bytes()
// Hack for now since Write() ignores r.Proto and forces HTTP/1.1
rb = bytes.Replace(rb, []byte("HTTP/1.1"), []byte("NATS/0.1"), 1)
// Send response.
m.Respond(rb)
})
if err != nil {
log.Fatalf("NATS Error subscribing to %q, %v", subject, err)
}
}