forked from beatlabs/patron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (145 loc) · 4.5 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"context"
"fmt"
"net/http"
"os"
"time"
"github.com/Shopify/sarama"
"github.com/beatlabs/patron"
clienthttp "github.com/beatlabs/patron/client/http"
patronkafka "github.com/beatlabs/patron/client/kafka/v2"
"github.com/beatlabs/patron/component/http/auth/apikey"
v2 "github.com/beatlabs/patron/component/http/v2"
"github.com/beatlabs/patron/component/http/v2/router/httprouter"
"github.com/beatlabs/patron/component/kafka"
"github.com/beatlabs/patron/encoding/json"
"github.com/beatlabs/patron/encoding/protobuf"
"github.com/beatlabs/patron/examples"
"github.com/beatlabs/patron/log"
)
const (
kafkaTopic = "patron-topic"
kafkaBroker = "localhost:9092"
)
func init() {
err := os.Setenv("PATRON_LOG_LEVEL", "debug")
if err != nil {
log.Fatalf("failed to set log level env var: %v", err)
}
err = os.Setenv("PATRON_JAEGER_SAMPLER_PARAM", "1.0")
if err != nil {
log.Fatalf("failed to set sampler env vars: %v", err)
}
err = os.Setenv("PATRON_HTTP_DEFAULT_PORT", "50001")
if err != nil {
log.Fatalf("failed to set default patron port env vars: %v", err)
}
}
func main() {
name := "http-sec"
version := "1.0.0"
service, err := patron.New(name, version, patron.LogFields(map[string]interface{}{"env": "staging"}))
if err != nil {
log.Fatalf("failed to set up service: %v", err)
}
asyncComp, err := newAsyncKafkaProducer(kafkaBroker, kafkaTopic, true)
if err != nil {
log.Fatalf("failed to create processor %v", err)
}
auth, err := apikey.New(&apiKeyValidator{validKey: "123456"})
if err != nil {
log.Fatalf("failed to create authenticator %v", err)
}
var routes v2.Routes
routes.Append(v2.NewGetRoute("/", asyncComp.forwardToKafkaHandler, v2.Auth(auth)))
rr, err := routes.Result()
if err != nil {
log.Fatalf("failed to create routes: %v", err)
}
router, err := httprouter.New(httprouter.Routes(rr...), httprouter.EnableAppNameHeaders(name, version))
if err != nil {
log.Fatalf("failed to create http router: %v", err)
}
ctx := context.Background()
err = service.WithRouter(router).Run(ctx)
if err != nil {
log.Fatalf("failed to create and run service %v", err)
}
}
type kafkaProducer struct {
prd *patronkafka.AsyncProducer
topic string
}
// newAsyncKafkaProducer creates a new asynchronous kafka producer client
func newAsyncKafkaProducer(kafkaBroker, topic string, readCommitted bool) (*kafkaProducer, error) {
saramaCfg, err := kafka.DefaultConsumerSaramaConfig("http-sec-consumer", readCommitted)
if err != nil {
return nil, err
}
prd, chErr, err := patronkafka.New([]string{kafkaBroker}, saramaCfg).CreateAsync()
if err != nil {
return nil, err
}
go func() {
for {
err := <-chErr
log.Errorf("error producing Kafka message: %v", err)
}
}()
return &kafkaProducer{prd: prd, topic: topic}, nil
}
// forwardToKafkaHandler is a http handler that decodes the input request and
// publishes the decoded content as a message into a kafka topic (also does an HTTP GET request to google.com)
func (hc *kafkaProducer) forwardToKafkaHandler(rw http.ResponseWriter, r *http.Request) {
var u examples.User
err := protobuf.Decode(r.Body, &u)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
_, _ = rw.Write([]byte(fmt.Sprintf("failed to decode request: %v", err)))
return
}
googleReq, err := http.NewRequest("GET", "https://www.google.com", nil)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
_, _ = rw.Write([]byte(fmt.Sprintf("failed to create request for www.google.com: %v", err)))
return
}
cl, err := clienthttp.New(clienthttp.Timeout(5 * time.Second))
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
_, err = cl.Do(googleReq)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
_, _ = rw.Write([]byte(fmt.Sprintf("failed to get www.google.com: %v", err)))
return
}
b, err := json.Encode(&u)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
_, _ = rw.Write([]byte(fmt.Sprintf("failed to encode message: %v", err)))
return
}
kafkaMsg := &sarama.ProducerMessage{
Topic: hc.topic,
Value: sarama.ByteEncoder(b),
}
err = hc.prd.Send(r.Context(), kafkaMsg)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
log.FromContext(r.Context()).Infof("request processed: %s %s", u.GetFirstname(), u.GetLastname())
rw.WriteHeader(http.StatusCreated)
}
type apiKeyValidator struct {
validKey string
}
func (av apiKeyValidator) Validate(key string) (bool, error) {
if key == av.validKey {
return true, nil
}
return false, nil
}