-
Notifications
You must be signed in to change notification settings - Fork 0
/
magellan-log-collector.go
157 lines (139 loc) · 3 KB
/
magellan-log-collector.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"golang.org/x/oauth2/google"
"google.golang.org/api/pubsub/v1"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
var (
apiTokens []string
)
func mustGetenv(ctx context.Context, k string) string {
v := os.Getenv(k)
if v == "" {
log.Printf("%s environment variable not set.", k)
}
return v
}
func verifyApiToken(token string) error {
for _, x := range apiTokens {
if x == token {
return nil
}
}
return errors.New("invalid api token.")
}
type Input struct {
ApiToken string `json:"api_token"`
Logs []struct {
Type string `json:"type"`
Attributes interface{} `json:"attributes"`
} `json:"logs"`
}
type Output struct {
Success bool `json:"success"`
Message string `json:"message"`
}
func postHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
output := Output{false, "something wrong."}
code := 500
defer func() {
outjson, e := json.Marshal(output)
if e != nil {
log.Printf(e.Error())
}
w.Header().Set("Content-Type", "application/json")
if code == 200 {
fmt.Fprint(w, string(outjson))
} else {
http.Error(w, string(outjson), code)
}
}()
if r.Method != "POST" {
output.Message = "only POST method method was accepted"
code = 404
return
}
body, e := ioutil.ReadAll(r.Body)
if e != nil {
output.Message = e.Error()
return
}
if len(body) > 64*1024 {
output.Message = "Request payload size exceeded 64KB."
code = 400
return
}
input := Input{}
e = json.Unmarshal(body, &input)
if e != nil {
output.Message = e.Error()
log.Printf(e.Error())
code = 400
return
}
if apiTokens == nil {
apiTokens = strings.Split(mustGetenv(ctx, "API_TOKEN"), ",")
}
e = verifyApiToken(input.ApiToken)
if e != nil {
output.Message = e.Error()
code = 401
return
}
hc, err := google.DefaultClient(ctx, pubsub.PubsubScope)
if err != nil {
log.Printf(err.Error())
}
pubsubService, err := pubsub.New(hc)
if err != nil {
log.Printf(err.Error())
}
topicId := "projects/" + mustGetenv(ctx, "GCLOUD_PROJECT") + "/topics/" + mustGetenv(ctx, "PUBSUB_TOPIC")
for _, entry := range input.Logs {
json, e := json.Marshal(entry)
if e != nil {
output.Message = e.Error()
log.Printf(e.Error())
code = 400
return
}
msg := &pubsub.PublishRequest{
Messages: []*pubsub.PubsubMessage{
{
Data: base64.StdEncoding.EncodeToString(json),
},
},
}
if _, err := pubsubService.Projects.Topics.Publish(topicId, msg).Do(); err != nil {
output.Message = fmt.Sprintf("Could not publish message: %v", err)
log.Printf("Could not publish message: %v", err)
code = 500
return
}
}
output.Success = true
output.Message = "ok"
code = 200
return
}
func main() {
apiTokens = nil
http.HandleFunc("/", postHandler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}