This repository has been archived on 2025-08-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
web-app-template/service/index_and_404.go
Tim Wundenberg a70138f2f7
All checks were successful
Build Docker Image / Explore-Gitea-Actions (push) Successful in 47s
Build and Push Docker Image / Explore-Gitea-Actions (push) Successful in 55s
fix: restructure env handling for better testing capabillities #181
2024-09-29 23:55:47 +02:00

34 lines
824 B
Go

package service
import (
"database/sql"
"me-fit/template"
"me-fit/types"
"me-fit/utils"
"net/http"
"github.com/a-h/templ"
)
func HandleIndexAnd404(db *sql.DB, serverSettings *types.ServerSettings) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user := utils.GetUserFromSession(db, r)
var comp templ.Component = nil
userComp := UserInfoComp(user)
if r.URL.Path != "/" {
comp = template.Layout(template.NotFound(), userComp, serverSettings.Environment)
w.WriteHeader(http.StatusNotFound)
} else {
comp = template.Layout(template.Index(), userComp, serverSettings.Environment)
}
err := comp.Render(r.Context(), w)
if err != nil {
utils.LogError("Failed to render index", err)
http.Error(w, "Failed to render index", http.StatusInternalServerError)
}
}
}