feat: redirect root path based on auth state
All checks were successful
check / check (push) Successful in 1m53s

/ now checks for an authenticated session:
- Authenticated users → 303 redirect to /sources
- Unauthenticated users → 303 redirect to /pages/login

Removes the old index page template rendering and formatUptime
helper (now dead code). Adds tests for both redirect cases.
This commit is contained in:
clawbot
2026-03-17 04:58:20 -07:00
parent f003ec7141
commit 9a0a6f88e3
4 changed files with 48 additions and 81 deletions

View File

@@ -1,49 +1,20 @@
package handlers
import (
"fmt"
"net/http"
"time"
"sneak.berlin/go/webhooker/internal/database"
)
// HandleIndex returns a handler for the root path that redirects based
// on authentication state: authenticated users go to /sources (the
// dashboard), unauthenticated users go to the login page.
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,
return func(w http.ResponseWriter, r *http.Request) {
sess, err := s.session.Get(r)
if err == nil && s.session.IsAuthenticated(sess) {
http.Redirect(w, r, "/sources", http.StatusSeeOther)
return
}
// Render the template
s.renderTemplate(w, req, "index.html", data)
http.Redirect(w, r, "/pages/login", http.StatusSeeOther)
}
}
// 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)
}