forked from rueian/cony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.go
127 lines (114 loc) · 2.73 KB
/
producer.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
118
119
120
121
122
123
124
125
126
127
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"github.com/Dcard/cony"
amqp "github.com/rabbitmq/amqp091-go"
)
var port = flag.Int("port", 3000, "listening port")
var url = flag.String("url", "amqp://guest:guest@localhost/", "amqp url")
var form = `
{{ if eq .status "thanks"}}
<p>Thank you</p>
{{ end }}
<form method="post">
<label>
To:
<input type="text" name="to" style="width:300px" />
</label>
<br />
<label>
Message:
<textarea name="message"></textarea>
</label>
<input type="submit" value="Send" />
</form>
`
func showUsageAndStatus() {
fmt.Printf("Producer is running\n")
fmt.Printf("Listening on: %v\n\n", *port)
fmt.Println("Flags:")
flag.PrintDefaults()
fmt.Printf("\n\n")
}
func main() {
flag.Parse()
showUsageAndStatus()
// Construct new client with the flag url
// and default backoff policy
cli := cony.NewClient(
cony.URL(*url),
cony.Backoff(cony.DefaultBackoff),
)
// Declare the exchange we'll be using
exc := cony.Exchange{
Name: "email",
Kind: "fanout",
AutoDelete: true,
}
cli.Declare([]cony.Declaration{
cony.DeclareExchange(exc),
})
// Declare and register a publisher
// with the cony client.
// This needs to be "global" per client
// and we'll need to use this exact value in
// our handlers (contexts should be of help)
pbl := cony.NewPublisher(exc.Name, "")
cli.Publish(pbl)
// Start our loop in a new gorouting
// so we don't block this one
go func() {
for cli.Loop() {
select {
case err := <-cli.Errors():
fmt.Println(err)
}
}
}()
// Simple template for our web-view
tpl, err := template.New("form").Parse(form)
if err != nil {
log.Fatal(err)
return
}
// HTTP handler function
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
// "GET" shows the template along
// with the possible thanks message
hdr := w.Header()
hdr["Content-Type"] = []string{"text/html"}
tpl.Execute(w, map[string]string{
"status": r.FormValue("status"),
})
return
} else if r.Method == "POST" {
ipt := map[string]string{
"to": r.FormValue("to"),
"message": r.FormValue("message"),
}
jsn, err := json.Marshal(ipt)
if err != nil {
w.Write([]byte(err.Error()))
return
}
// "POST" publishes the value received
// from the form to AMQP
// Note: we're using the "pbl" variable
// (declared above in our code) and we
// don't declare a new Publisher value.
go pbl.Publish(amqp.Publishing{
Body: jsn,
})
http.Redirect(w, r, "/?status=thanks", http.StatusFound)
return
}
http.Error(w, "404 not found", http.StatusNotFound)
})
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", *port), nil))
}