Templates are now embedded using //go:embed and parsed once at startup with template.Must(template.ParseFS(...)). This avoids re-parsing template files from disk on every request and removes the dependency on template files being present at runtime. closes #7
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"sneak.berlin/go/webhooker/internal/database"
|
|
)
|
|
|
|
type IndexResponse struct {
|
|
Message string `json:"message"`
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
func (s *Handlers) HandleIndex() http.HandlerFunc {
|
|
// Calculate server start time
|
|
startTime := time.Now()
|
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
|
// Calculate uptime
|
|
uptime := time.Since(startTime)
|
|
uptimeStr := formatUptime(uptime)
|
|
|
|
// Get user count from database
|
|
var userCount int64
|
|
s.db.DB().Model(&database.User{}).Count(&userCount)
|
|
|
|
// Prepare template data
|
|
data := map[string]interface{}{
|
|
"Version": s.params.Globals.Version,
|
|
"Uptime": uptimeStr,
|
|
"UserCount": userCount,
|
|
}
|
|
|
|
// Render the template
|
|
s.renderTemplate(w, req, "index.html", data)
|
|
}
|
|
}
|
|
|
|
// formatUptime formats a duration into a human-readable string
|
|
func formatUptime(d time.Duration) string {
|
|
days := int(d.Hours()) / 24
|
|
hours := int(d.Hours()) % 24
|
|
minutes := int(d.Minutes()) % 60
|
|
|
|
if days > 0 {
|
|
return fmt.Sprintf("%dd %dh %dm", days, hours, minutes)
|
|
}
|
|
if hours > 0 {
|
|
return fmt.Sprintf("%dh %dm", hours, minutes)
|
|
}
|
|
return fmt.Sprintf("%dm", minutes)
|
|
}
|