some-automoderation/routes/index_page.go

74 lines
1.7 KiB
Go

package routes
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"sunshine.industries/some-automoderation/rooms"
"sunshine.industries/some-automoderation/sessions"
)
type indexContent struct {
AuthedData authedData
LoginSectionData LoginSectionData
}
type authedData struct {
SessionStringToken string
SomeString string
Session sessions.SessionData
}
func (ad authedData) IsZero() bool {
return ad == authedData{}
}
// index page shows info on logged in room OR login room form on the left
// and general automoderation rules on the right
func indexPageRoute(
templateFs *embed.FS,
sessionSM sessions.SessionManagement,
roomsM rooms.RoomManager,
) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, err := getRequestSession(r, sessionSM)
log.Printf("/ the session i got is %+v", session)
if err != nil {
log.Printf("/ session not found, means should render the login section %s", err)
// TODO return error i guess
}
baseFile := "templates/base.gohtml"
pageTemplFile := "templates/index.gohtml"
loginSectionFile := "templates/login-section.gohtml"
authData := authedData{}
if err == nil {
authData = authedData{
fmt.Sprintf("%+v", session),
"hello!",
session,
}
}
data := pageData{
Base: baseData{
Title: "Some Automoderation: simple automation",
},
Header: headerData{
Title: session.RoomId,
},
Content: indexContent{
AuthedData: authData,
},
}
tmpl := template.Must(template.ParseFS(templateFs, pageTemplFile, baseFile, loginSectionFile))
err = tmpl.ExecuteTemplate(w, "full-page", data)
if err != nil {
log.Printf("my error in executing template, huh\n %s", err)
}
})
}