This repository has been archived by the owner on Oct 8, 2020. It is now read-only.
/
app.go
200 lines (192 loc) · 7.33 KB
/
app.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
197
198
199
200
package main
import (
"net/http"
"os"
"time"
fthealth "github.com/Financial-Times/go-fthealth/v1a"
oldhttphandlers "github.com/Financial-Times/http-handlers-go/httphandlers"
"github.com/Financial-Times/service-status-go/gtg"
"github.com/Financial-Times/service-status-go/httphandlers"
"github.com/Sirupsen/logrus"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/jawher/mow.cli"
)
const serviceDescription = "A RESTful API for retrieving and transforming content to preview data"
var timeout = time.Duration(10 * time.Second)
var client = &http.Client{Timeout: timeout}
func main() {
app := cli.App("content-preview", serviceDescription)
serviceName := app.String(cli.StringOpt{
Name: "app-name",
Value: "content-preview",
Desc: "The name of this service",
EnvVar: "APP_NAME",
})
appPort := app.String(cli.StringOpt{
Name: "app-port",
Value: "8084",
Desc: "Default port for Content Preview app",
EnvVar: "APP_PORT",
})
sourceAppAuth := app.String(cli.StringOpt{
Name: "source-app-auth",
Value: "default",
Desc: "Basic auth for MAPI",
EnvVar: "SOURCE_APP_AUTH",
})
sourceAppUri := app.String(cli.StringOpt{
Name: "source-app-uri",
Value: "http://methode-api-uk-p.svc.ft.com/eom-file/",
Desc: "URI of the Native Content Source Application endpoint",
EnvVar: "SOURCE_APP_URI",
})
sourceAppHealthUri := app.String(cli.StringOpt{
Name: "source-app-health-uri",
Value: "http://methode-api-uk-p.svc.ft.com/build-info",
Desc: "URI of the Native Content Source Application health endpoint",
EnvVar: "SOURCE_APP_HEALTH_URI",
})
transformAppHostHeader := app.String(cli.StringOpt{
Name: "transform-app-host-header",
Value: "methode-article-transformer",
Desc: "Transform Application Host Header",
EnvVar: "TRANSFORM_APP_HOST_HEADER",
})
transformAppUri := app.String(cli.StringOpt{
Name: "transform-app-uri",
Value: "http://methode-article-transformer-01-iw-uk-p.svc.ft.com/map/",
Desc: "URI of the Transform Application endpoint",
EnvVar: "TRANSFORM_APP_URI",
})
transformAppHealthUri := app.String(cli.StringOpt{
Name: "transform-app-health-uri",
Value: "http://methode-article-transformer-01-iw-uk-p.svc.ft.com/build-info",
Desc: "URI of the Transform Application health endpoint",
EnvVar: "TRANSFORM_APP_HEALTH_URI",
})
sourceAppName := app.String(cli.StringOpt{
Name: "source-app-name",
Value: "Native Content Service",
Desc: "Service name of the source application",
EnvVar: "SOURCE_APP_NAME",
})
transformAppName := app.String(cli.StringOpt{
Name: "transform-app-name",
Value: "Native Content Transformer Service",
Desc: "Service name of the content transformer application",
EnvVar: "TRANSFORM_APP_NAME",
})
sourceAppPanicGuide := app.String(cli.StringOpt{
Name: "source-app-panic-guide",
Value: "https://dewey.ft.com/up-mapi.html",
Desc: "Native Content Source application panic guide url for healthcheck. Default panic guide is for Methode API.",
EnvVar: "SOURCE_APP_PANIC_GUIDE",
})
transformAppPanicGuide := app.String(cli.StringOpt{
Name: "transform-app-panic-guide",
Value: "https://dewey.ft.com/up-mam.html",
Desc: "Transform application panic guide url for healthcheck. Default panic guide is for Methode Article Mapper.",
EnvVar: "TRANSFORM_APP_PANIC_GUIDE",
})
businessImpact := app.String(cli.StringOpt{
Name: "business-impact",
Value: "Editorial users won't be able to preview articles",
Desc: "Describe the business impact the dependent services would produce if one is broken.",
EnvVar: "BUSINESS_IMPACT",
})
graphiteTCPAddress := app.String(cli.StringOpt{
Name: "graphite-tcp-address",
Value: "",
Desc: "Graphite TCP address, e.g. graphite.ft.com:2003. Leave as default if you do NOT want to output to graphite (e.g. if running locally)",
EnvVar: "GRAPHITE_TCP_ADDRESS",
})
graphitePrefix := app.String(cli.StringOpt{
Name: "graphite-prefix",
Value: "coco.services.$ENV.content-preview.0",
Desc: "Prefix to use. Should start with content, include the environment, and the host name. e.g. coco.pre-prod.sections-rw-neo4j.1",
EnvVar: "GRAPHITE_PREFIX",
})
logMetrics := app.Bool(cli.BoolOpt{
Name: "log-metrics",
Value: false,
Desc: "Whether to log metrics. Set to true if running locally and you want metrics output",
EnvVar: "LOG_METRICS",
})
app.Action = func() {
sc := ServiceConfig{
*serviceName,
*appPort,
*sourceAppAuth,
*transformAppHostHeader,
*sourceAppUri,
*transformAppUri,
*sourceAppHealthUri,
*transformAppHealthUri,
*sourceAppName,
*transformAppName,
*sourceAppPanicGuide,
*transformAppPanicGuide,
*businessImpact,
*graphiteTCPAddress,
*graphitePrefix,
}
appLogger := NewAppLogger()
metricsHandler := NewMetrics()
contentHandler := ContentHandler{&sc, appLogger, &metricsHandler}
h := setupServiceHandler(sc, metricsHandler, contentHandler)
appLogger.ServiceStartedEvent(*serviceName, sc.asMap())
metricsHandler.OutputMetricsIfRequired(*graphiteTCPAddress, *graphitePrefix, *logMetrics)
err := http.ListenAndServe(":"+*appPort, h)
if err != nil {
logrus.Fatalf("Unable to start server: %v", err)
}
}
app.Run(os.Args)
}
func setupServiceHandler(sc ServiceConfig, metricsHandler Metrics, contentHandler ContentHandler) *mux.Router {
r := mux.NewRouter()
r.Path("/content-preview/{uuid}").Handler(handlers.MethodHandler{"GET": oldhttphandlers.HTTPMetricsHandler(metricsHandler.registry,
oldhttphandlers.TransactionAwareRequestLoggingHandler(logrus.StandardLogger(), contentHandler))})
r.Path(httphandlers.BuildInfoPath).HandlerFunc(httphandlers.BuildInfoHandler)
r.Path(httphandlers.PingPath).HandlerFunc(httphandlers.PingHandler)
gtgHandler := httphandlers.NewGoodToGoHandler(gtg.StatusChecker(sc.gtgCheck))
r.Path(httphandlers.GTGPath).HandlerFunc(gtgHandler)
r.Path("/__health").Handler(handlers.MethodHandler{"GET": http.HandlerFunc(fthealth.Handler(sc.serviceName, serviceDescription, sc.nativeContentSourceCheck(), sc.transformerServiceCheck()))})
r.Path("/__metrics").Handler(handlers.MethodHandler{"GET": http.HandlerFunc(metricsHttpEndpoint)})
return r
}
type ServiceConfig struct {
serviceName string
appPort string
sourceAppAuth string
transformAppHostHeader string
sourceAppUri string
transformAppUri string
sourceAppHealthUri string
transformAppHealthUri string
sourceAppName string
transformAppName string
sourceAppPanicGuide string
transformAppPanicGuide string
businessImpact string
graphiteTCPAddress string
graphitePrefix string
}
func (sc ServiceConfig) asMap() map[string]interface{} {
return map[string]interface{}{
"service-name": sc.serviceName,
"service-port": sc.appPort,
"source-app-name": sc.sourceAppName,
"source-app-uri": sc.sourceAppUri,
"transform-app-name": sc.transformAppName,
"transform-app-uri": sc.transformAppUri,
"source-app-health-uri": sc.sourceAppHealthUri,
"transform-app-health-uri": sc.transformAppHealthUri,
"source-app-panic-guide": sc.sourceAppPanicGuide,
"transform-app-panic-guide": sc.transformAppPanicGuide,
"business-impact": sc.businessImpact,
"graphite-tcp-address": sc.graphiteTCPAddress,
"graphite-prefix": sc.graphitePrefix,
}
}