add simple / path handler

This commit is contained in:
juancwu 2026-01-05 15:23:42 -05:00
commit 7a264bf0bc
2 changed files with 18 additions and 1 deletions

View file

@ -1,6 +1,10 @@
package handler
import "net/http"
import (
"net/http"
"git.juancwu.dev/juancwu/budgit/internal/ctxkeys"
)
type homeHandler struct{}
@ -8,6 +12,16 @@ func NewHomeHandler() *homeHandler {
return &homeHandler{}
}
// HomePage will redirect to /auth if not authenticated or to /app/dashboard if authenticated.
func (h *homeHandler) HomePage(w http.ResponseWriter, r *http.Request) {
user := ctxkeys.User(r.Context())
if user == nil {
http.Redirect(w, r, "/auth", http.StatusSeeOther)
return
}
http.Redirect(w, r, "/app/dashboard", http.StatusSeeOther)
}
func (home *homeHandler) NotFoundPage(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 Page Not Found"))

View file

@ -25,6 +25,9 @@ func SetupRoutes(a *app.App) http.Handler {
sub, _ := fs.Sub(assets.AssetsFS, ".")
mux.Handle("GET /assets/", http.StripPrefix("/assets/", http.FileServer(http.FS(sub))))
// Home
mux.HandleFunc("GET /{$}", home.HomePage)
// Auth pages
authRateLimiter := middleware.RateLimitAuth()