forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpubsub.go
131 lines (108 loc) · 2.91 KB
/
pubsub.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
128
129
130
131
// Copyright 2015 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Sample pubsub demonstrates use of the google.golang.org/cloud/pubsub package from App Engine flexible environment.
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"sync"
"google.golang.org/appengine"
"google.golang.org/cloud/pubsub"
"golang.org/x/net/context"
)
var (
topic *pubsub.Topic
// Messages received by this instance.
messagesMu sync.Mutex
messages []string
)
const maxMessages = 10
func main() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, mustGetenv("GCLOUD_PROJECT"))
if err != nil {
log.Fatal(err)
}
// Create topic if it doesn't exist.
topic, _ = client.NewTopic(ctx, mustGetenv("PUBSUB_TOPIC"))
http.HandleFunc("/", listHandler)
http.HandleFunc("/pubsub/publish", publishHandler)
http.HandleFunc("/pubsub/push", pushHandler)
appengine.Main()
}
func mustGetenv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Fatalf("%s environment variable not set.", k)
}
return v
}
type pushRequest struct {
Message struct {
Attributes map[string]string
Data []byte
ID string `json:"message_id"`
}
Subscription string
}
func pushHandler(w http.ResponseWriter, r *http.Request) {
msg := &pushRequest{}
if err := json.NewDecoder(r.Body).Decode(msg); err != nil {
http.Error(w, fmt.Sprintf("Could not decode body: %v", err), http.StatusBadRequest)
return
}
messagesMu.Lock()
defer messagesMu.Unlock()
// Limit to ten.
messages = append(messages, string(msg.Message.Data))
if len(messages) > maxMessages {
messages = messages[len(messages)-maxMessages:]
}
}
func listHandler(w http.ResponseWriter, r *http.Request) {
messagesMu.Lock()
defer messagesMu.Unlock()
if err := tmpl.Execute(w, messages); err != nil {
log.Printf("Could not execute template: %v", err)
}
}
func publishHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
msg := &pubsub.Message{
Data: []byte(r.FormValue("payload")),
}
if _, err := topic.Publish(ctx, msg); err != nil {
http.Error(w, fmt.Sprintf("Could not publish message: %v", err), 500)
return
}
fmt.Fprint(w, "Message published.")
}
var tmpl = template.Must(template.New("").Parse(`<!DOCTYPE html>
<html>
<head>
<title>Pub/Sub</title>
</head>
<body>
<div>
<p>Last ten messages received by this instance:</p>
<ul>
{{ range . }}
<li>{{ . }}</li>
{{ end }}
</ul>
</div>
<!-- [START form] -->
<form method="post" action="/pubsub/publish">
<textarea name="payload" placeholder="Enter message here"></textarea>
<input type="submit">
</form>
<!-- [END form] -->
<p>Note: if the application is running across multiple instances, each
instance will have its own list of messages.</p>
</body>
</html>`))