This repository has been archived by the owner on May 29, 2019. It is now read-only.
forked from beatlabs/patron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (120 loc) · 3.08 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"time"
"github.com/pkg/errors"
"github.com/thebeatapp/patron"
"github.com/thebeatapp/patron/examples"
"github.com/thebeatapp/patron/log"
"github.com/thebeatapp/patron/sync"
patronhttp "github.com/thebeatapp/patron/sync/http"
"github.com/thebeatapp/patron/sync/http/auth/apikey"
tracehttp "github.com/thebeatapp/patron/trace/http"
"github.com/thebeatapp/patron/trace/kafka"
)
const (
kafkaTopic = "patron-topic"
kafkaBroker = "localhost:9092"
)
func init() {
err := os.Setenv("PATRON_LOG_LEVEL", "debug")
if err != nil {
fmt.Printf("failed to set log level env var: %v", err)
os.Exit(1)
}
err = os.Setenv("PATRON_JAEGER_SAMPLER_PARAM", "1.0")
if err != nil {
fmt.Printf("failed to set sampler env vars: %v", err)
os.Exit(1)
}
err = os.Setenv("PATRON_HTTP_DEFAULT_PORT", "50001")
if err != nil {
fmt.Printf("failed to set default patron port env vars: %v", err)
os.Exit(1)
}
}
func main() {
name := "second"
version := "1.0.0"
err := patron.Setup(name, version)
if err != nil {
fmt.Printf("failed to set up logging: %v", err)
os.Exit(1)
}
httpCmp, err := newHTTPComponent(kafkaBroker, kafkaTopic, "http://localhost:50000/second")
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)
}
// Set up routes
routes := []patronhttp.Route{
patronhttp.NewAuthGetRoute("/", httpCmp.second, true, auth),
}
srv, err := patron.New(
name,
version,
patron.Routes(routes),
)
if err != nil {
log.Fatalf("failed to create service %v", err)
}
err = srv.Run()
if err != nil {
log.Fatalf("failed to run service %v", err)
}
}
type httpComponent struct {
prd kafka.Producer
topic string
}
func newHTTPComponent(kafkaBroker, topic, url string) (*httpComponent, error) {
prd, err := kafka.NewAsyncProducer([]string{kafkaBroker})
if err != nil {
return nil, err
}
return &httpComponent{prd: prd, topic: topic}, nil
}
func (hc *httpComponent) second(ctx context.Context, req *sync.Request) (*sync.Response, error) {
var u examples.User
err := req.Decode(&u)
if err != nil {
return nil, errors.Wrap(err, "failed to decode message")
}
googleReq, err := http.NewRequest("GET", "https://www.google.com", nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create request for www.google.com")
}
cl, err := tracehttp.New(tracehttp.Timeout(5 * time.Second))
if err != nil {
return nil, err
}
_, err = cl.Do(ctx, googleReq)
if err != nil {
return nil, errors.Wrap(err, "failed to get www.google.com")
}
kafkaMsg, err := kafka.NewJSONMessage(hc.topic, &u)
if err != nil {
return nil, err
}
err = hc.prd.Send(ctx, kafkaMsg)
if err != nil {
return nil, err
}
log.FromContext(ctx).Infof("request processed: %s %s", u.GetFirstname(), u.GetLastname())
return nil, nil
}
type apiKeyValidator struct {
validKey string
}
func (av apiKeyValidator) Validate(key string) (bool, error) {
if key == av.validKey {
return true, nil
}
return false, nil
}