Skip to content

Commit faaf577

Browse files
authored
Merge pull request #1 from moabdelazem/feature/add-request-count
feat: add request counter to dashboard
2 parents 090e16f + 13cfe75 commit faaf577

3 files changed

Lines changed: 183 additions & 161 deletions

File tree

internal/handler/handler.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net"
99
"net/http"
1010
"runtime"
11+
"sync/atomic"
1112
"time"
1213

1314
"github.com/moabdelazem/gitops-sample-app/internal/config"
@@ -16,9 +17,10 @@ import (
1617
)
1718

1819
type Handler struct {
19-
cfg *config.Config
20-
tmpl *template.Template
21-
startTime time.Time
20+
cfg *config.Config
21+
tmpl *template.Template
22+
startTime time.Time
23+
requestCount atomic.Int64
2224
}
2325

2426
func New(cfg *config.Config, tmpl *template.Template) *Handler {
@@ -36,6 +38,7 @@ func (h *Handler) Register(mux *http.ServeMux) {
3638
}
3739

3840
func (h *Handler) Index(w http.ResponseWriter, r *http.Request) {
41+
h.requestCount.Add(1)
3942
info := h.buildInfo()
4043
w.Header().Set("Content-Type", "text/html; charset=utf-8")
4144
if err := h.tmpl.Execute(w, info); err != nil {
@@ -58,15 +61,16 @@ func (h *Handler) APIInfo(w http.ResponseWriter, r *http.Request) {
5861

5962
func (h *Handler) buildInfo() model.AppInfo {
6063
return model.AppInfo{
61-
Version: version.Version,
62-
GitCommit: version.GitCommit,
63-
BuildTime: version.BuildTime,
64-
Environment: h.cfg.Environment,
65-
PodName: h.cfg.PodName,
66-
NodeName: h.cfg.NodeName,
67-
HostIP: getHostIP(),
68-
GoVersion: runtime.Version(),
69-
Uptime: formatUptime(time.Since(h.startTime)),
64+
Version: version.Version,
65+
GitCommit: version.GitCommit,
66+
BuildTime: version.BuildTime,
67+
Environment: h.cfg.Environment,
68+
PodName: h.cfg.PodName,
69+
NodeName: h.cfg.NodeName,
70+
HostIP: getHostIP(),
71+
GoVersion: runtime.Version(),
72+
Uptime: formatUptime(time.Since(h.startTime)),
73+
RequestCount: h.requestCount.Load(),
7074
}
7175
}
7276

internal/model/appinfo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ type AppInfo struct {
1010
HostIP string `json:"host_ip"`
1111
GoVersion string `json:"go_version"`
1212
Uptime string `json:"uptime"`
13+
RequestCount int64 `json:"request_count"`
1314
}

web/templates/index.html

Lines changed: 166 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,177 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<meta name="description" content="GitOps Sample App - deployment metadata dashboard">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta
7+
name="description"
8+
content="GitOps Sample App - deployment metadata dashboard"
9+
/>
710
<title>GitOps Sample App</title>
8-
<meta http-equiv="refresh" content="30">
9-
<link rel="preconnect" href="https://fonts.googleapis.com">
10-
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
11-
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
11+
<meta http-equiv="refresh" content="30" />
12+
<link rel="preconnect" href="https://fonts.googleapis.com" />
13+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
14+
<link
15+
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500&display=swap"
16+
rel="stylesheet"
17+
/>
1218
<style>
13-
* {
14-
margin: 0;
15-
padding: 0;
16-
box-sizing: border-box;
17-
}
18-
19+
* {
20+
margin: 0;
21+
padding: 0;
22+
box-sizing: border-box;
23+
}
24+
25+
body {
26+
font-family: "Inter", sans-serif;
27+
background: #111;
28+
color: #e5e5e5;
29+
min-height: 100vh;
30+
display: flex;
31+
justify-content: center;
32+
padding: 4rem 1.5rem;
33+
}
34+
35+
.container {
36+
max-width: 640px;
37+
width: 100%;
38+
}
39+
40+
h1 {
41+
font-size: 1.25rem;
42+
font-weight: 600;
43+
color: #fff;
44+
margin-bottom: 0.25rem;
45+
}
46+
47+
.subtitle {
48+
font-size: 0.8rem;
49+
color: #666;
50+
margin-bottom: 2rem;
51+
}
52+
53+
.env-badge {
54+
display: inline-block;
55+
padding: 0.2rem 0.6rem;
56+
border-radius: 4px;
57+
font-size: 0.65rem;
58+
font-weight: 600;
59+
text-transform: uppercase;
60+
letter-spacing: 0.05em;
61+
margin-bottom: 2rem;
62+
}
63+
64+
.env-badge--development {
65+
background: #1e3a5f;
66+
color: #7db8f0;
67+
}
68+
69+
.env-badge--staging {
70+
background: #3d3214;
71+
color: #d4a841;
72+
}
73+
74+
.env-badge--production,
75+
.env-badge--prod {
76+
background: #1a3a2a;
77+
color: #4ade80;
78+
}
79+
80+
table {
81+
width: 100%;
82+
border-collapse: collapse;
83+
}
84+
85+
tr {
86+
border-bottom: 1px solid #222;
87+
}
88+
89+
td {
90+
padding: 0.75rem 0;
91+
vertical-align: top;
92+
}
93+
94+
td:first-child {
95+
font-size: 0.75rem;
96+
font-weight: 500;
97+
color: #666;
98+
text-transform: uppercase;
99+
letter-spacing: 0.05em;
100+
width: 140px;
101+
white-space: nowrap;
102+
}
103+
104+
td:last-child {
105+
font-family: "JetBrains Mono", monospace;
106+
font-size: 0.85rem;
107+
color: #e5e5e5;
108+
word-break: break-all;
109+
}
110+
111+
.footer {
112+
margin-top: 2rem;
113+
padding-top: 1rem;
114+
border-top: 1px solid #222;
115+
font-size: 0.7rem;
116+
color: #444;
117+
}
118+
119+
@media (max-width: 480px) {
19120
body {
20-
font-family: 'Inter', sans-serif;
21-
background: #111;
22-
color: #e5e5e5;
23-
min-height: 100vh;
24-
display: flex;
25-
justify-content: center;
26-
padding: 4rem 1.5rem;
27-
}
28-
29-
.container {
30-
max-width: 640px;
31-
width: 100%;
32-
}
33-
34-
h1 {
35-
font-size: 1.25rem;
36-
font-weight: 600;
37-
color: #fff;
38-
margin-bottom: 0.25rem;
39-
}
40-
41-
.subtitle {
42-
font-size: 0.8rem;
43-
color: #666;
44-
margin-bottom: 2rem;
121+
padding: 2rem 1rem;
45122
}
46-
47-
.env-badge {
48-
display: inline-block;
49-
padding: 0.2rem 0.6rem;
50-
border-radius: 4px;
51-
font-size: 0.65rem;
52-
font-weight: 600;
53-
text-transform: uppercase;
54-
letter-spacing: 0.05em;
55-
margin-bottom: 2rem;
56-
}
57-
58-
.env-badge--development {
59-
background: #1e3a5f;
60-
color: #7db8f0;
61-
}
62-
63-
.env-badge--staging {
64-
background: #3d3214;
65-
color: #d4a841;
66-
}
67-
68-
.env-badge--production, .env-badge--prod {
69-
background: #1a3a2a;
70-
color: #4ade80;
71-
}
72-
73-
table {
74-
width: 100%;
75-
border-collapse: collapse;
76-
}
77-
78-
tr {
79-
border-bottom: 1px solid #222;
80-
}
81-
82-
td {
83-
padding: 0.75rem 0;
84-
vertical-align: top;
85-
}
86-
87123
td:first-child {
88-
font-size: 0.75rem;
89-
font-weight: 500;
90-
color: #666;
91-
text-transform: uppercase;
92-
letter-spacing: 0.05em;
93-
width: 140px;
94-
white-space: nowrap;
95-
}
96-
97-
td:last-child {
98-
font-family: 'JetBrains Mono', monospace;
99-
font-size: 0.85rem;
100-
color: #e5e5e5;
101-
word-break: break-all;
102-
}
103-
104-
.footer {
105-
margin-top: 2rem;
106-
padding-top: 1rem;
107-
border-top: 1px solid #222;
108-
font-size: 0.7rem;
109-
color: #444;
110-
}
111-
112-
@media (max-width: 480px) {
113-
body { padding: 2rem 1rem; }
114-
td:first-child { width: 100px; }
124+
width: 100px;
115125
}
126+
}
116127
</style>
117-
</head>
118-
<body>
128+
</head>
129+
<body>
119130
<div class="container">
120-
<h1>GitOps Sample App</h1>
121-
<p class="subtitle">Deployment Metadata</p>
122-
<span class="env-badge env-badge--{{.Environment}}">{{.Environment}}</span>
123-
124-
<table>
125-
<tr>
126-
<td>Version</td>
127-
<td>{{.Version}}</td>
128-
</tr>
129-
<tr>
130-
<td>Commit</td>
131-
<td>{{.GitCommit}}</td>
132-
</tr>
133-
<tr>
134-
<td>Built</td>
135-
<td>{{.BuildTime}}</td>
136-
</tr>
137-
<tr>
138-
<td>Environment</td>
139-
<td>{{.Environment}}</td>
140-
</tr>
141-
<tr>
142-
<td>Pod</td>
143-
<td>{{.PodName}}</td>
144-
</tr>
145-
<tr>
146-
<td>Node</td>
147-
<td>{{.NodeName}}</td>
148-
</tr>
149-
<tr>
150-
<td>Host IP</td>
151-
<td>{{.HostIP}}</td>
152-
</tr>
153-
</table>
154-
155-
<div class="footer">
156-
Uptime: {{.Uptime}} / Go {{.GoVersion}} / Auto-refreshes every 30s
157-
</div>
131+
<h1>GitOps Sample App</h1>
132+
<p class="subtitle">Deployment Metadata</p>
133+
<span class="env-badge env-badge--{{.Environment}}"
134+
>{{.Environment}}</span
135+
>
136+
137+
<table>
138+
<tr>
139+
<td>Version</td>
140+
<td>{{.Version}}</td>
141+
</tr>
142+
<tr>
143+
<td>Commit</td>
144+
<td>{{.GitCommit}}</td>
145+
</tr>
146+
<tr>
147+
<td>Built</td>
148+
<td>{{.BuildTime}}</td>
149+
</tr>
150+
<tr>
151+
<td>Environment</td>
152+
<td>{{.Environment}}</td>
153+
</tr>
154+
<tr>
155+
<td>Pod</td>
156+
<td>{{.PodName}}</td>
157+
</tr>
158+
<tr>
159+
<td>Node</td>
160+
<td>{{.NodeName}}</td>
161+
</tr>
162+
<tr>
163+
<td>Host IP</td>
164+
<td>{{.HostIP}}</td>
165+
</tr>
166+
<tr>
167+
<td>Requests</td>
168+
<td>{{.RequestCount}}</td>
169+
</tr>
170+
</table>
171+
172+
<div class="footer">
173+
Uptime: {{.Uptime}} / Go {{.GoVersion}} / Auto-refreshes every 30s
174+
</div>
158175
</div>
159-
</body>
176+
</body>
160177
</html>

0 commit comments

Comments
 (0)