✨ feat(server): add GET /api/v1/uptime endpoint
Add new endpoint that returns server start time and uptime duration in seconds.
The endpoint follows the same pattern as handleHealth and handleInfo handlers,
using the existing startedAt field from the Server struct.
Response format:
{
"start_time": "2026-05-05T19:30:00Z",
"uptime_seconds": 1234
}
Includes unit tests with httptest.NewRecorder() to verify JSON response
structure and content type headers.
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -246,6 +246,9 @@ func (s *Server) registerApiV1Routes(r chi.Router) {
|
||||
r.Get("/{name}", s.handleGreetPath)
|
||||
})
|
||||
|
||||
// Uptime endpoint
|
||||
r.Get("/uptime", s.handleUptime)
|
||||
|
||||
// Register user authentication routes
|
||||
if s.userService != nil && s.userRepo != nil {
|
||||
// Use unified user service - much simpler!
|
||||
@@ -583,6 +586,30 @@ func (s *Server) handleInfo(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
// UptimeResponse represents the JSON response for /api/v1/uptime
|
||||
type UptimeResponse struct {
|
||||
StartTime string `json:"start_time"`
|
||||
UptimeSeconds int `json:"uptime_seconds"`
|
||||
}
|
||||
|
||||
// handleUptime godoc
|
||||
//
|
||||
// @Summary Get server uptime
|
||||
// @Description Returns server start time and uptime duration
|
||||
// @Tags System/Info
|
||||
// @Produce json
|
||||
// @Success 200 {object} UptimeResponse
|
||||
// @Router /v1/uptime [get]
|
||||
func (s *Server) handleUptime(w http.ResponseWriter, r *http.Request) {
|
||||
log.Trace().Msg("Uptime check requested")
|
||||
resp := UptimeResponse{
|
||||
StartTime: s.startedAt.Format(time.RFC3339),
|
||||
UptimeSeconds: int(time.Since(s.startedAt).Seconds()),
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
}
|
||||
|
||||
// handleGreetQuery godoc
|
||||
//
|
||||
// @Summary Get greeting with cache
|
||||
|
||||
Reference in New Issue
Block a user