-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds Makefile for build, using cmd directory and tests
- Loading branch information
Showing
9 changed files
with
171 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
all: clean win_64 win_32 linux_64 linux_32 darwin_64 darwin_32 freebsd_64 freebsd_32 | ||
|
||
win_64: | ||
env GOOS=windows GOARCH=amd64 go build -o "dist/pubsub-push.exe" ./cmd/pubsub-push | ||
zip -T -j -9 "dist/pubsub-push_$(shell cat VERSION)_$@.zip" dist/pubsub-push.exe | ||
rm -f dist/pubsub-push.exe | ||
|
||
win_32: | ||
env GOOS=windows GOARCH=386 go build -o "dist/pubsub-push.exe" ./cmd/pubsub-push | ||
zip -T -j -9 "dist/pubsub-push_$(shell cat VERSION)_$@.zip" dist/pubsub-push.exe | ||
rm -f dist/pubsub-push.exe | ||
|
||
linux_64: | ||
env GOOS=linux GOARCH=amd64 go build -o dist/pubsub-push ./cmd/pubsub-push | ||
gzip dist/pubsub-push -c > "dist/pubsub-push_$(shell cat VERSION)_$@.gz" | ||
rm -f dist/pubsub-push | ||
|
||
linux_32: | ||
env GOOS=linux GOARCH=386 go build -o dist/pubsub-push ./cmd/pubsub-push | ||
gzip dist/pubsub-push -c > "dist/pubsub-push_$(shell cat VERSION)_$@.gz" | ||
rm -f dist/pubsub-push | ||
|
||
darwin_64: | ||
env GOOS=darwin GOARCH=amd64 go build -o dist/pubsub-push ./cmd/pubsub-push | ||
gzip dist/pubsub-push -c > "dist/pubsub-push_$(shell cat VERSION)_$@.gz" | ||
rm -f dist/pubsub-push | ||
|
||
darwin_32: | ||
env GOOS=darwin GOARCH=386 go build -o dist/pubsub-push ./cmd/pubsub-push | ||
gzip dist/pubsub-push -c > "dist/pubsub-push_$(shell cat VERSION)_$@.gz" | ||
rm -f dist/pubsub-push | ||
|
||
clean: | ||
rm -rf dist/* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package pubsubpush | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"io" | ||
"log" | ||
"net/http" | ||
"strings" | ||
|
||
"cloud.google.com/go/pubsub" | ||
) | ||
|
||
type message struct { | ||
MessageID string `json:"messageId"` | ||
Data string `json:"data"` | ||
Attributes map[string]string `json:"attributes"` | ||
} | ||
|
||
type request struct { | ||
Message message `json:"message"` | ||
} | ||
|
||
// Headers is the list of HTTP Header to be applied on Request | ||
type Headers []string | ||
|
||
func (h *Headers) String() string { | ||
b := strings.Builder{} | ||
|
||
for _, v := range *h { | ||
b.WriteString(v) | ||
} | ||
|
||
return b.String() | ||
} | ||
|
||
// Set appends a new Header | ||
func (h *Headers) Set(value string) error { | ||
*h = append(*h, value) | ||
return nil | ||
} | ||
|
||
func (h *Headers) applyHeaders(ht *http.Request) { | ||
for _, v := range *h { | ||
parts := strings.Split(v, "=") | ||
|
||
if len(parts) == 2 { | ||
ht.Header.Set(parts[0], parts[1]) | ||
} else if len(parts) == 1 { | ||
ht.Header.Set(parts[0], "") | ||
} | ||
} | ||
} | ||
|
||
// EncodeMessage prepares the message to be like the HTTP Push from PubSub | ||
// It is an JSON with a data field containing a base64 value | ||
func EncodeMessage(m *pubsub.Message) ([]byte, int) { | ||
data := m.Data | ||
req := request{} | ||
req.Message.Data = base64.StdEncoding.EncodeToString(data) | ||
req.Message.Attributes = m.Attributes | ||
req.Message.MessageID = m.ID | ||
b, err := json.Marshal(req) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
return []byte{}, 0 | ||
} | ||
|
||
return b, len(b) | ||
} | ||
|
||
// PostMessage sends the the message to endpoint | ||
func PostMessage(url string, contentType string, body io.Reader, h *Headers) (*http.Response, error) { | ||
req, err := http.NewRequest("POST", url, body) | ||
if err != nil { | ||
log.Fatalf("I was not possible to create a new request: %v\n", err) | ||
return nil, err | ||
} | ||
req.Header.Set("Content-type", contentType) | ||
h.applyHeaders(req) | ||
return http.DefaultClient.Do(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package pubsubpush | ||
|
||
import "testing" | ||
|
||
func TestHeadersSet(t *testing.T) { | ||
h := Headers{} | ||
h.Set("Content-type=application/json") | ||
h.Set("Auth=api-key") | ||
|
||
if len(h) != 2 { | ||
t.Errorf("Len expected 2 and got %d", len(h)) | ||
} | ||
|
||
if h[0] != "Content-type=application/json" { | ||
t.Errorf("Expected value %s and got %s", "Content-type=application/json", h[0]) | ||
} | ||
|
||
if h[1] != "Auth=api-key" { | ||
t.Errorf("Expected value %s and got %s", "Auth=api-key", h[1]) | ||
} | ||
} | ||
|
||
func TestHeadersString(t *testing.T) { | ||
h := Headers{} | ||
h.Set("Content-type=application/json") | ||
|
||
if len(h) != 1 { | ||
t.Errorf("Len expected 2 and got %d", len(h)) | ||
} | ||
|
||
if h.String() != "Content-type=application/json" { | ||
t.Errorf("Expected value %s and got %s", "Content-type=application/json", h.String()) | ||
} | ||
|
||
} |