47 lines
1,008 B
Go
47 lines
1,008 B
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"html/template"
|
|
|
|
"context"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
const TemplateDir = "./web/templates/"
|
|
|
|
type IndexData struct {
|
|
Title string
|
|
}
|
|
|
|
func SetLogLevel(level zerolog.Level) {
|
|
zerolog.SetGlobalLevel(level)
|
|
}
|
|
|
|
func WebRouter() http.Handler {
|
|
r := chi.NewRouter()
|
|
r.Get("/", getIndex)
|
|
r.Get("/hello", getHello)
|
|
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static/"))))
|
|
return r
|
|
}
|
|
|
|
func getIndex(w http.ResponseWriter, req *http.Request) {
|
|
index, err := template.ParseFiles(TemplateDir + "index.html")
|
|
if err != nil {
|
|
log.Fatal().
|
|
Err(err).
|
|
Msg("Fatal error reading index template")
|
|
}
|
|
|
|
err = index.Execute(w, IndexData{Title: "thetitle"})
|
|
}
|
|
|
|
func getHello(w http.ResponseWriter, req *http.Request) {
|
|
log.Debug().Msg("Got index")
|
|
component := hello("Nick")
|
|
component.Render(context.Background(), w)
|
|
}
|