This repository has been archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
78 lines (67 loc) · 1.82 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
package main
import (
"flag"
"fmt"
"net/http"
"time"
"github.com/SAP/aker/config"
"github.com/SAP/aker/endpoint"
"github.com/SAP/aker/plugin"
"github.com/SAP/aker/uuid"
"github.com/SAP/gologger"
)
var configLocationFlag = flag.String(
"config",
"config.yml",
"Specifies the configuration file location.",
)
func main() {
flag.Parse()
cfg, err := config.LoadFromFile(*configLocationFlag)
if err != nil {
gologger.Fatalf("Failed to load configuration due to %q", err.Error())
}
mux := http.NewServeMux()
for _, endpointCfg := range cfg.Endpoints {
endpointHandler, err := endpoint.NewHandler(endpointCfg, plugin.DefaultOpener)
if err != nil {
gologger.Fatalf("Failed to build plugin chain due to %q", err.Error())
}
mux.Handle(endpointCfg.Path, endpointHandler)
}
handler := NewHeaderSticker(mux, map[string]func() string{
"X-Aker-Request-Id": func() string {
uid, _ := uuid.Random()
return uid.String()
},
})
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
srv := http.Server{
Addr: addr,
Handler: handler,
ReadTimeout: time.Duration(cfg.Server.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(cfg.Server.WriteTimeout) * time.Second,
}
gologger.Infof("Starting HTTP listener...")
if err = srv.ListenAndServe(); err != nil {
gologger.Fatalf("HTTP Listener failed with %q", err.Error())
}
}
type HeaderSticker struct {
http.Handler
headers map[string]func() string
}
func NewHeaderSticker(h http.Handler, headers map[string]func() string) *HeaderSticker {
return &HeaderSticker{
Handler: h,
headers: headers,
}
}
func (s *HeaderSticker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
for header, valueFunc := range s.headers {
value := valueFunc()
req.Header.Add(header, value)
w.Header().Add(header, value)
}
s.Handler.ServeHTTP(w, req)
}