Skip to content

Commit ee95ef4

Browse files
authored
Feat/healthcheck (#18)
* feat: Adds a healthcheck endpoint * feat: Implements healthcheck into Dockerfile
1 parent 1214dcf commit ee95ef4

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

Dockerfile

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ COPY . .
88
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/bin/app main.go
99

1010
# Stage 2: Run the application
11-
# Create non-root user, copy bin from build stage, set perms, expose port, run app
12-
FROM alpine:latest
11+
# Create non-root user, copy bin from build stage, set perms,
12+
# expose port, start health check and then run the app
13+
FROM alpine:3.20
1314
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
1415
COPY --from=builder /app/bin/app /usr/local/bin/app
1516
RUN chmod +x /usr/local/bin/app
1617
USER appuser
1718
EXPOSE 8080
19+
HEALTHCHECK \
20+
--interval=30s \
21+
--timeout=10s \
22+
--start-period=5s \
23+
--retries=3 \
24+
CMD curl -f http://localhost:8080/health || exit 1
1825
CMD ["app"]
19-

handlers/health-check.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
// HandleHealthCheck returns the status of the application
8+
func HandleHealthCheck() http.Handler {
9+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
10+
w.Header().Set("Content-Type", "application/json")
11+
w.WriteHeader(http.StatusOK)
12+
w.Write([]byte(`{"status":"ok", "message":"We're alive!"}`))
13+
})
14+
}

server/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func (s *Server) routes() {
4444
s.mux.Handle("GET /api/social-tags", handlers.HandleGetSocialTags())
4545
s.mux.Handle("GET /api/tls", handlers.HandleTLS())
4646
s.mux.Handle("GET /api/trace-route", handlers.HandleTraceRoute())
47+
s.mux.Handle("GET /health", handlers.HandleHealthCheck())
4748
}
4849

4950
func (s *Server) Run() error {

0 commit comments

Comments
 (0)