Skip to content

Commit d638cd3

Browse files
committed
add logging about tls termination on root endpoint
1 parent d57229f commit d638cd3

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

src/server.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ type server struct {
1616
tmpl *template.Template
1717
}
1818

19+
type header struct {
20+
Name string
21+
Value string
22+
}
23+
1924
// TemplateData holds all the data needed for the HTML template
2025
type TemplateData struct {
2126
ApplicationName string
@@ -27,6 +32,7 @@ type TemplateData struct {
2732
RootDelaySeconds int
2833
StartUpDelaySeconds int
2934
TearDownDelaySeconds int
35+
Headers []*header
3036
LogToFileOnly bool
3137
ProcessID int
3238
UserID int
@@ -41,8 +47,8 @@ const htmlTemplate = `<!DOCTYPE html>
4147
</head>
4248
<body style='background-color:{{.Color}};'>
4349
<h1>{{.ApplicationName}}</h1>
44-
45-
<h2>Configuration</h2>
50+
51+
<h2>Configuration</h2>
4652
Application Version: {{.ApplicationVersion}}<br>
4753
Application Message: {{.ApplicationMessage}}<br>
4854
Application Liveness: {{.Alive}}<br>
@@ -53,9 +59,16 @@ const htmlTemplate = `<!DOCTYPE html>
5359
Only log to file: {{.LogToFileOnly}}<br>
5460
5561
<h2>Tech Details</h2>
62+
63+
<h3>About the Application</h3>
5664
Process Id of the application: {{.ProcessID}}<br>
5765
User Id the application is using: {{.UserID}}<br>
5866
Hostname: {{.Hostname}}<br>
67+
68+
<h3>About the Request</h3>
69+
{{range .Headers}}
70+
{{.Name}}: {{.Value}}<br>
71+
{{end}}
5972
6073
{{if .CatImageURL}}
6174
<h2>The cute cat</h2>
@@ -96,6 +109,18 @@ func (s *server) run() {
96109
}
97110
}
98111

112+
func logHeader(request *http.Request, headerName string, template *TemplateData) {
113+
headerValue := request.Header.Get(headerName)
114+
header := header{Name:headerName, Value:""}
115+
if headerValue != "" {
116+
log.Infof(" Header named '%s' has value: %s", headerName, headerValue)
117+
header.Value = headerValue
118+
} else {
119+
log.Infof(" Header named '%s' not set", headerName)
120+
}
121+
template.Headers = append(template.Headers, &header)
122+
}
123+
99124
func (s *server) handleRoot(w http.ResponseWriter, r *http.Request) {
100125
log.Info("Request to root endpoint ('/')")
101126

@@ -136,6 +161,10 @@ func (s *server) handleRoot(w http.ResponseWriter, r *http.Request) {
136161
CatImageURL: s.config.catImageUrl,
137162
}
138163

164+
logHeader(r, "X-Forwarded-Proto", &data)
165+
logHeader(r, "X-Forwarded-For", &data)
166+
logHeader(r, "X-Forwarded-Port", &data)
167+
139168
// Set content type and execute template
140169
w.Header().Set("Content-Type", "text/html")
141170
if err := s.tmpl.Execute(w, data); err != nil {

0 commit comments

Comments
 (0)