38 lines
853 B
Go
38 lines
853 B
Go
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)
|
|
}
|
|
}
|