add home handler + add templates folder

This commit is contained in:
rmanach
2025-01-03 13:54:35 +01:00
parent f712242e39
commit 874378cf2d
14 changed files with 267 additions and 216 deletions
+37
View File
@@ -0,0 +1,37 @@
package home
import (
"bytes"
"fmt"
"librapi/templates"
"net/http"
"github.com/rs/zerolog/log"
)
func Handler() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
getHome(w, r)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
}
func getHome(w http.ResponseWriter, _ *http.Request) {
home := templates.GetHome()
buf := bytes.NewBufferString("")
if err := home.Execute(buf, nil); err != nil {
log.Err(err).Msg("unable to generate template")
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
return
}
if _, err := fmt.Fprint(w, buf); err != nil {
log.Err(err).Msg("unable to write to response")
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
}
}