add home handler + add templates folder
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user