This repository has been archived by the owner on May 2, 2018. It is now read-only.
/
main.go
134 lines (121 loc) · 3.96 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
package main
import (
"crypto/tls"
"fmt"
"github.com/Financial-Times/go-fthealth/v1a"
"github.com/Financial-Times/http-handlers-go/httphandlers"
status "github.com/Financial-Times/service-status-go/httphandlers"
"github.com/Financial-Times/tme-reader/tmereader"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/jawher/mow.cli"
"github.com/rcrowley/go-metrics"
"github.com/sethgrid/pester"
"net"
"net/http"
"os"
"time"
)
func init() {
log.SetFormatter(new(log.JSONFormatter))
}
func main() {
app := cli.App("subjects-transformer", "A RESTful API for transforming TME Subjects to UP json")
username := app.String(cli.StringOpt{
Name: "tme-username",
Value: "",
Desc: "TME username used for http basic authentication",
EnvVar: "TME_USERNAME",
})
password := app.String(cli.StringOpt{
Name: "tme-password",
Value: "",
Desc: "TME password used for http basic authentication",
EnvVar: "TME_PASSWORD",
})
token := app.String(cli.StringOpt{
Name: "token",
Value: "",
Desc: "Token to be used for accessing TME",
EnvVar: "TOKEN",
})
baseURL := app.String(cli.StringOpt{
Name: "base-url",
Value: "http://localhost:8080/transformers/subjects/",
Desc: "Base url",
EnvVar: "BASE_URL",
})
tmeBaseURL := app.String(cli.StringOpt{
Name: "tme-base-url",
Value: "https://tme.ft.com",
Desc: "TME base url",
EnvVar: "TME_BASE_URL",
})
port := app.Int(cli.IntOpt{
Name: "port",
Value: 8080,
Desc: "Port to listen on",
EnvVar: "PORT",
})
maxRecords := app.Int(cli.IntOpt{
Name: "maxRecords",
Value: int(10000),
Desc: "Maximum records to be queried to TME",
EnvVar: "MAX_RECORDS",
})
slices := app.Int(cli.IntOpt{
Name: "slices",
Value: int(10),
Desc: "Number of requests to be executed in parallel to TME",
EnvVar: "SLICES",
})
tmeTaxonomyName := "Subjects"
app.Action = func() {
client := getResilientClient()
mf := new(subjectTransformer)
s, err := newSubjectService(tmereader.NewTmeRepository(client, *tmeBaseURL, *username, *password, *token, *maxRecords, *slices, tmeTaxonomyName, &tmereader.KnowledgeBases{}, mf), *baseURL, tmeTaxonomyName, *maxRecords)
if err != nil {
log.Errorf("Error while creating SubjectsService: [%v]", err.Error())
}
h := newSubjectsHandler(s)
m := mux.NewRouter()
// The top one of these feels more correct, but the lower one matches what we have in Dropwizard,
// so it's what apps expect currently same as ping
m.HandleFunc(status.PingPath, status.PingHandler)
m.HandleFunc(status.PingPathDW, status.PingHandler)
m.HandleFunc(status.BuildInfoPath, status.BuildInfoHandler)
m.HandleFunc(status.BuildInfoPathDW, status.BuildInfoHandler)
m.HandleFunc("/__health", v1a.Handler("Subjects Transformer Healthchecks", "Checks for accessing TME", h.HealthCheck()))
m.HandleFunc("/__gtg", h.GoodToGo)
m.HandleFunc("/transformers/subjects", h.getSubjects).Methods("GET")
m.HandleFunc("/transformers/subjects/__ids", h.getIds).Methods("GET")
m.HandleFunc("/transformers/subjects/__count", h.getCount).Methods("GET")
m.HandleFunc("/transformers/subjects/__reload", h.reload).Methods("POST")
m.HandleFunc("/transformers/subjects/{uuid}", h.getSubjectByUUID).Methods("GET")
http.Handle("/", m)
log.Printf("listening on %d", *port)
http.ListenAndServe(fmt.Sprintf(":%d", *port),
httphandlers.HTTPMetricsHandler(metrics.DefaultRegistry,
httphandlers.TransactionAwareRequestLoggingHandler(log.StandardLogger(), m)))
}
app.Run(os.Args)
}
func getResilientClient() *pester.Client {
tr := &http.Transport{
MaxIdleConnsPerHost: 32,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
}
c := &http.Client{
Transport: tr,
Timeout: time.Duration(30 * time.Second),
}
client := pester.NewExtendedClient(c)
client.Backoff = pester.ExponentialBackoff
client.MaxRetries = 5
client.Concurrency = 1
return client
}