initial
This commit is contained in:
54
internal/handlers/index.go
Normal file
54
internal/handlers/index.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.eeqj.de/sneak/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, []string{"templates/base.html", "templates/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)
|
||||
}
|
||||
Reference in New Issue
Block a user