forked from beatlabs/patron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
196 lines (168 loc) · 6 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strconv"
"time"
"github.com/beatlabs/patron"
patronhttpclient "github.com/beatlabs/patron/client/http"
patronhttp "github.com/beatlabs/patron/component/http/v2"
patronhttpjson "github.com/beatlabs/patron/component/http/v2/encoding/json"
"github.com/beatlabs/patron/component/http/v2/router/httprouter"
"github.com/beatlabs/patron/encoding/protobuf"
"github.com/beatlabs/patron/examples"
"github.com/beatlabs/patron/log"
"github.com/beatlabs/patron/log/std"
)
const maxRequests = 1000
type httpError struct {
Error string `json:"error"`
}
func newHttpError(error string) httpError {
return httpError{Error: error}
}
var (
assetsFolder string
requestsCount int
refreshAfter int64
)
func init() {
err := os.Setenv("PATRON_JAEGER_SAMPLER_PARAM", "1.0")
if err != nil {
log.Fatalf("failed to set sampler env vars: %v", err)
}
// allows running from any folder the 'go run examples/http/main.go'
var ok bool
assetsFolder, ok = os.LookupEnv("PATRON_EXAMPLE_ASSETS_FOLDER")
if !ok {
assetsFolder = "examples/http/public"
}
// allow a thousand requests every 10 seconds
requestsCount = maxRequests
refreshAfter = time.Now().Add(10 * time.Second).Unix()
}
func main() {
name := "httpHandler"
version := "1.0.0"
logger := std.New(os.Stderr, log.DebugLevel, map[string]interface{}{"env": "staging"})
service, err := patron.New(name, version, patron.Logger(logger))
if err != nil {
log.Fatalf("failed to set up service: %v", err)
}
// Set up a simple CORS middleware
corsMiddleware := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Methods", "GET, POST")
w.Header().Add("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
w.Header().Add("Access-Control-Allow-Credentials", "Allow")
h.ServeHTTP(w, r)
})
}
var routes patronhttp.Routes
routes.Append(patronhttp.NewGetRoute("/api", getHandler, patronhttp.RateLimiting(50, 50)))
routes.Append(patronhttp.NewPostRoute("/api", httpHandler))
routes.Append(httprouter.NewFileServerRoute("/frontend/*path", assetsFolder, assetsFolder+"/index.html"))
rr, err := routes.Result()
if err != nil {
log.Fatalf("failed to create routes: %v", err)
}
router, err := httprouter.New(httprouter.Middlewares(corsMiddleware), httprouter.Routes(rr...))
if err != nil {
log.Fatalf("failed to create http router: %v", err)
}
sig := func() {
log.Info("exit gracefully...")
os.Exit(0)
}
ctx := context.Background()
err = service.WithRouter(router).WithSIGHUP(sig).Run(ctx)
if err != nil {
log.Fatalf("failed to create and run service %v", err)
}
}
func getHandler(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(http.StatusOK)
_, _ = rw.Write([]byte("Testing Middleware"))
}
func httpHandler(rw http.ResponseWriter, r *http.Request) {
now := time.Now().Unix()
if requestsCount <= 0 && refreshAfter > now {
rw.WriteHeader(http.StatusTooManyRequests)
rw.Header().Set("Retry-After", strconv.Itoa(int(refreshAfter)))
_, _ = rw.Write([]byte("no more requests and the refresh time is in the future"))
return
}
requestsCount--
if refreshAfter <= now {
requestsCount = maxRequests
}
interval, err := DoIntervalRequest()
if err != nil {
log.FromContext(r.Context()).Infof("httpHandler: failed to get interval information %v: could it be that the http-cache service is not running ?", err)
} else {
log.FromContext(r.Context()).Infof("httpHandler: pipeline initiated at: %s", interval)
}
var u examples.User
err = patronhttpjson.ReadRequest(r, &u)
if err != nil {
patronhttpjson.WriteResponse(rw, http.StatusBadRequest, newHttpError(fmt.Sprintf("failed to decode request: %v", err)))
return
}
b, err := protobuf.Encode(&u)
if err != nil {
patronhttpjson.WriteResponse(rw, http.StatusInternalServerError, newHttpError(fmt.Sprintf("failed create request: %v", err)))
return
}
httpRequest, err := http.NewRequest("GET", "http://localhost:50001", bytes.NewReader(b))
if err != nil {
patronhttpjson.WriteResponse(rw, http.StatusInternalServerError, newHttpError(fmt.Sprintf("failed create request: %v", err)))
return
}
httpRequest.Header.Add("Content-Type", protobuf.Type)
httpRequest.Header.Add("Accept", protobuf.Type)
httpRequest.Header.Add("Authorization", "Apikey 123456")
cl, err := patronhttpclient.New(patronhttpclient.Timeout(5 * time.Second))
if err != nil {
patronhttpjson.WriteResponse(rw, http.StatusInternalServerError, newHttpError(fmt.Sprintf("failed execute request: %v", err)))
return
}
rsp, err := cl.Do(httpRequest)
if err != nil {
patronhttpjson.WriteResponse(rw, http.StatusInternalServerError, newHttpError(fmt.Sprintf("failed to perform http request with protobuf payload: %v", err)))
return
}
log.FromContext(r.Context()).Infof("request processed: %s %s", u.GetFirstname(), u.GetLastname())
rw.WriteHeader(http.StatusCreated)
_, _ = rw.Write([]byte(fmt.Sprintf("got %s from HTTP route", rsp.Status)))
}
// DoIntervalRequest is a helper method to make a request to the http-cache example service from other examples
func DoIntervalRequest() (string, error) {
request, err := http.NewRequest("GET", "http://localhost:50007/", nil)
if err != nil {
return "", fmt.Errorf("failed create route request: %w", err)
}
cl, err := patronhttpclient.New(patronhttpclient.Timeout(5 * time.Second))
if err != nil {
return "", fmt.Errorf("could not create http client: %w", err)
}
response, err := cl.Do(request)
if err != nil {
return "", fmt.Errorf("failed create get to http-cache service: %w", err)
}
tb, err := io.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf("failed to decode http-cache response body: %w", err)
}
rgx := regexp.MustCompile(`\((.*?)\)`)
timeInstance := rgx.FindStringSubmatch(string(tb))
if len(timeInstance) == 1 {
return "", fmt.Errorf("could not match time instance from response %s", string(tb))
}
return timeInstance[1], nil
}