Skip to content

Commit c8d04d0

Browse files
committed
feat: adds Makefile for build, using cmd directory and tests
1 parent b6b787e commit c8d04d0

File tree

9 files changed

+171
-75
lines changed

9 files changed

+171
-75
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
dist

Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
all: clean win_64 win_32 linux_64 linux_32 darwin_64 darwin_32 freebsd_64 freebsd_32
3+
4+
win_64:
5+
env GOOS=windows GOARCH=amd64 go build -o "dist/pubsub-push.exe" ./cmd/pubsub-push
6+
zip -T -j -9 "dist/pubsub-push_$(shell cat VERSION)_$@.zip" dist/pubsub-push.exe
7+
rm -f dist/pubsub-push.exe
8+
9+
win_32:
10+
env GOOS=windows GOARCH=386 go build -o "dist/pubsub-push.exe" ./cmd/pubsub-push
11+
zip -T -j -9 "dist/pubsub-push_$(shell cat VERSION)_$@.zip" dist/pubsub-push.exe
12+
rm -f dist/pubsub-push.exe
13+
14+
linux_64:
15+
env GOOS=linux GOARCH=amd64 go build -o dist/pubsub-push ./cmd/pubsub-push
16+
gzip dist/pubsub-push -c > "dist/pubsub-push_$(shell cat VERSION)_$@.gz"
17+
rm -f dist/pubsub-push
18+
19+
linux_32:
20+
env GOOS=linux GOARCH=386 go build -o dist/pubsub-push ./cmd/pubsub-push
21+
gzip dist/pubsub-push -c > "dist/pubsub-push_$(shell cat VERSION)_$@.gz"
22+
rm -f dist/pubsub-push
23+
24+
darwin_64:
25+
env GOOS=darwin GOARCH=amd64 go build -o dist/pubsub-push ./cmd/pubsub-push
26+
gzip dist/pubsub-push -c > "dist/pubsub-push_$(shell cat VERSION)_$@.gz"
27+
rm -f dist/pubsub-push
28+
29+
darwin_32:
30+
env GOOS=darwin GOARCH=386 go build -o dist/pubsub-push ./cmd/pubsub-push
31+
gzip dist/pubsub-push -c > "dist/pubsub-push_$(shell cat VERSION)_$@.gz"
32+
rm -f dist/pubsub-push
33+
34+
clean:
35+
rm -rf dist/*
36+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ $ pubsub-push -project proj-id -sub name -endpoint localhost -header "Content-ty
5353
-endpoint The complete URL, including schema, domain, port and the path.
5454
Eg: -endpoint http://localhost:5000/services/sync
5555
56-
-header An string like "key=value" for request headers. It can be used
56+
-header A string like "key=value" for request headers. It can be used
5757
multiplem times.
5858
Eg: -header "Content-type=application/json"
5959
```

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1

main.go renamed to cmd/pubsub-push/main.go

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,84 +3,25 @@ package main
33
import (
44
"bytes"
55
"context"
6-
"encoding/base64"
7-
"encoding/json"
86
"flag"
97
"fmt"
10-
"io"
118
"log"
12-
"net/http"
139
"os"
14-
"strings"
1510

1611
"cloud.google.com/go/pubsub"
12+
push "github.com/klassmann/pubsub-push"
1713
)
1814

1915
const (
2016
credentialsVarName string = "GOOGLE_APPLICATION_CREDENTIALS"
2117
messageMimetype string = "application/json"
2218
)
2319

24-
type headers []string
25-
26-
func (h *headers) String() string {
27-
b := strings.Builder{}
28-
29-
for _, v := range *h {
30-
b.WriteString(v)
31-
}
32-
33-
return b.String()
34-
}
35-
36-
func (h *headers) Set(value string) error {
37-
*h = append(*h, value)
38-
return nil
39-
}
40-
41-
func (h *headers) applyHeaders(ht *http.Request) {
42-
for _, v := range *h {
43-
parts := strings.Split(v, "=")
44-
45-
if len(parts) == 2 {
46-
ht.Header.Set(parts[0], parts[1])
47-
} else if len(parts) == 1 {
48-
ht.Header.Set(parts[0], "")
49-
}
50-
}
51-
}
52-
5320
type settings struct {
5421
ProjectID string
5522
Subscription string
5623
Endpoint string
57-
Headers headers
58-
}
59-
60-
type message struct {
61-
MessageID string `json:"messageId"`
62-
Data string `json:"data"`
63-
Attributes map[string]string `json:"attributes"`
64-
}
65-
66-
type request struct {
67-
Message message `json:"message"`
68-
}
69-
70-
func encodeMessage(m *pubsub.Message) ([]byte, int) {
71-
data := m.Data
72-
req := request{}
73-
req.Message.Data = base64.StdEncoding.EncodeToString(data)
74-
req.Message.Attributes = m.Attributes
75-
req.Message.MessageID = m.ID
76-
b, err := json.Marshal(req)
77-
78-
if err != nil {
79-
log.Fatal(err)
80-
return []byte{}, 0
81-
}
82-
83-
return b, len(b)
24+
Headers push.Headers
8425
}
8526

8627
func getArguments() *settings {
@@ -100,17 +41,6 @@ func getArguments() *settings {
10041
return &s
10142
}
10243

103-
func post(url string, contentType string, body io.Reader, h *headers) (*http.Response, error) {
104-
req, err := http.NewRequest("POST", url, body)
105-
if err != nil {
106-
log.Fatalf("I was not possible to create a new request: %v\n", err)
107-
return nil, err
108-
}
109-
req.Header.Set("Content-type", contentType)
110-
h.applyHeaders(req)
111-
return http.DefaultClient.Do(req)
112-
}
113-
11444
func main() {
11545
settings := getArguments()
11646

@@ -131,10 +61,10 @@ func main() {
13161
fmt.Printf("Listening subscription %s:\n", settings.Subscription)
13262
sub := client.Subscription(settings.Subscription)
13363
err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
134-
b, size := encodeMessage(m)
64+
b, size := push.EncodeMessage(m)
13565
buff := bytes.NewBuffer(b)
13666

137-
resp, err := post(settings.Endpoint, messageMimetype, buff, &settings.Headers) //http.Post(settings.Endpoint, messageMimetype, buff)
67+
resp, err := push.PostMessage(settings.Endpoint, messageMimetype, buff, &settings.Headers)
13868

13969
if err != nil {
14070
log.Fatalf("Error on send message to endpoint: %v\n", err)

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ go 1.12
55
require (
66
cloud.google.com/go v0.43.0
77
github.com/hashicorp/golang-lru v0.5.3 // indirect
8+
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 // indirect
9+
golang.org/x/arch v0.0.0-20190312162104-788fe5ffcd8c // indirect
810
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
911
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3 // indirect
1012
google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
2222
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
2323
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
2424
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
25+
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f h1:Jnx61latede7zDD3DiiP4gmNz33uK0U5HDUaF0a/HVQ=
2526
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
2627
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
2728
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
@@ -31,11 +32,16 @@ github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+
3132
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
3233
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
3334
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
35+
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 h1:UDMh68UUwekSh5iP2OMhRRZJiiBccgV7axzUG8vi56c=
36+
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
3437
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
3538
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
3639
go.opencensus.io v0.22.0 h1:C9hSCOW830chIVkdja34wa6Ky+IzWllkUinR+BtRZd4=
3740
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
41+
golang.org/x/arch v0.0.0-20190312162104-788fe5ffcd8c h1:Rx/HTKi09myZ25t1SOlDHmHOy/mKxNAcu0hP1oPX9qM=
42+
golang.org/x/arch v0.0.0-20190312162104-788fe5ffcd8c/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
3843
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
44+
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 h1:58fnuSXlxZmFdJyvtTFVmVhcMLU6v5fEb/ok4wyqtNU=
3945
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
4046
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
4147
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -96,6 +102,7 @@ golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBn
96102
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
97103
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
98104
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
105+
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0 h1:Dh6fw+p6FyRl5x/FvNswO1ji0lIGzm3KP8Y9VkS9PTE=
99106
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
100107
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
101108
google.golang.org/api v0.7.0 h1:9sdfJOzWlkqPltHAuzT2Cp+yrBeY1KRVYgms8soxMwM=
@@ -125,3 +132,4 @@ honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWh
125132
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
126133
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
127134
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
135+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

pubsubpush.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package pubsubpush
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
"io"
7+
"log"
8+
"net/http"
9+
"strings"
10+
11+
"cloud.google.com/go/pubsub"
12+
)
13+
14+
type message struct {
15+
MessageID string `json:"messageId"`
16+
Data string `json:"data"`
17+
Attributes map[string]string `json:"attributes"`
18+
}
19+
20+
type request struct {
21+
Message message `json:"message"`
22+
}
23+
24+
// Headers is the list of HTTP Header to be applied on Request
25+
type Headers []string
26+
27+
func (h *Headers) String() string {
28+
b := strings.Builder{}
29+
30+
for _, v := range *h {
31+
b.WriteString(v)
32+
}
33+
34+
return b.String()
35+
}
36+
37+
// Set appends a new Header
38+
func (h *Headers) Set(value string) error {
39+
*h = append(*h, value)
40+
return nil
41+
}
42+
43+
func (h *Headers) applyHeaders(ht *http.Request) {
44+
for _, v := range *h {
45+
parts := strings.Split(v, "=")
46+
47+
if len(parts) == 2 {
48+
ht.Header.Set(parts[0], parts[1])
49+
} else if len(parts) == 1 {
50+
ht.Header.Set(parts[0], "")
51+
}
52+
}
53+
}
54+
55+
// EncodeMessage prepares the message to be like the HTTP Push from PubSub
56+
// It is an JSON with a data field containing a base64 value
57+
func EncodeMessage(m *pubsub.Message) ([]byte, int) {
58+
data := m.Data
59+
req := request{}
60+
req.Message.Data = base64.StdEncoding.EncodeToString(data)
61+
req.Message.Attributes = m.Attributes
62+
req.Message.MessageID = m.ID
63+
b, err := json.Marshal(req)
64+
65+
if err != nil {
66+
log.Fatal(err)
67+
return []byte{}, 0
68+
}
69+
70+
return b, len(b)
71+
}
72+
73+
// PostMessage sends the the message to endpoint
74+
func PostMessage(url string, contentType string, body io.Reader, h *Headers) (*http.Response, error) {
75+
req, err := http.NewRequest("POST", url, body)
76+
if err != nil {
77+
log.Fatalf("I was not possible to create a new request: %v\n", err)
78+
return nil, err
79+
}
80+
req.Header.Set("Content-type", contentType)
81+
h.applyHeaders(req)
82+
return http.DefaultClient.Do(req)
83+
}

pubsubpush_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package pubsubpush
2+
3+
import "testing"
4+
5+
func TestHeadersSet(t *testing.T) {
6+
h := Headers{}
7+
h.Set("Content-type=application/json")
8+
h.Set("Auth=api-key")
9+
10+
if len(h) != 2 {
11+
t.Errorf("Len expected 2 and got %d", len(h))
12+
}
13+
14+
if h[0] != "Content-type=application/json" {
15+
t.Errorf("Expected value %s and got %s", "Content-type=application/json", h[0])
16+
}
17+
18+
if h[1] != "Auth=api-key" {
19+
t.Errorf("Expected value %s and got %s", "Auth=api-key", h[1])
20+
}
21+
}
22+
23+
func TestHeadersString(t *testing.T) {
24+
h := Headers{}
25+
h.Set("Content-type=application/json")
26+
27+
if len(h) != 1 {
28+
t.Errorf("Len expected 2 and got %d", len(h))
29+
}
30+
31+
if h.String() != "Content-type=application/json" {
32+
t.Errorf("Expected value %s and got %s", "Content-type=application/json", h.String())
33+
}
34+
35+
}

0 commit comments

Comments
 (0)