fix: rendering index page after login

This commit is contained in:
efim 2023-11-18 03:47:58 +00:00
parent 942091c5d8
commit cdd31e186a
3 changed files with 40 additions and 40 deletions

View File

@ -11,42 +11,47 @@ import (
"sunshine.industries/some-automoderation/sessions" "sunshine.industries/some-automoderation/sessions"
) )
func renderIndexPage(session sessions.SessionData, w http.ResponseWriter) {
var baseFile = "templates/base.gohtml"
var templFile = "templates/index.gohtml"
type MainData struct {
SessionStringToken string
SomeString string
SessionToken sessions.SessionData
}
data := pageData{
Base: baseData{
Title: "hello base template title",
},
Header: headerData{
Title: session.RoomId,
},
Content: MainData{
fmt.Sprintf("%+v", session),
"hello!",
session,
},
}
tmpl := template.Must(template.ParseFS(templateFs, templFile, baseFile))
err := tmpl.ExecuteTemplate(w, "full-page", data)
if err != nil {
log.Printf("my error in executing template, huh\n %s", err)
}
}
func indexPageRoute( func indexPageRoute(
templateFs *embed.FS, templateFs *embed.FS,
sessionSM sessions.SessionManagement, sessionSM sessions.SessionManagement,
roomsM rooms.RoomManager, roomsM rooms.RoomManager,
) http.HandlerFunc { ) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var baseFile = "templates/base.gohtml"
var templFile = "templates/index.gohtml"
session, found := getContextSession(r.Context()) session, found := getContextSession(r.Context())
if !found { if !found {
log.Printf("/ session not found, should be impossible") log.Printf("/ session not found, should be impossible")
// TODO return error i guess // TODO return error i guess
} }
type MainData struct { renderIndexPage(session, w)
SessionStringToken string
SomeString string
SessionToken sessions.SessionData
}
data := pageData{
Base: baseData{
Title: "hello base template title",
},
Header: headerData{
Title: session.RoomId,
},
Content: MainData{
fmt.Sprintf("%+v", session),
"hello!",
session,
},
}
tmpl := template.Must(template.ParseFS(templateFs, templFile, baseFile))
err := tmpl.ExecuteTemplate(w, "full-page", data)
if err != nil {
log.Printf("my error in executing template, huh\n %s", err)
}
}) })
} }

View File

@ -143,26 +143,21 @@ func createRoomHandler(templateFs *embed.FS,
log.Printf("what am i to do? error saving room %s", err) log.Printf("what am i to do? error saving room %s", err)
// todo return error notice somehow // todo return error notice somehow
} }
newSessionId, err := sessionSM.Save(r.Context(), newRoom.Name, person.Id) newSession, err := sessionSM.Save(r.Context(), newRoom.Name, person.Id)
if err != nil { if err != nil {
log.Printf("what am i to do? error saving session %s", err) log.Printf("what am i to do? error saving session %s", err)
// todo return error notice somehow // todo return error notice somehow
} }
http.SetCookie(w, &http.Cookie{ http.SetCookie(w, &http.Cookie{
Name: authCookieName, Name: authCookieName,
Value: fmt.Sprint(newSessionId), Value: fmt.Sprint(newSession.SessionId),
Secure: true, Secure: true,
HttpOnly: true, HttpOnly: true,
Path: "/", Path: "/",
}) })
var templFile = "templates/index.gohtml"
tmpl := template.Must(template.ParseFS(templateFs, templFile))
w.Header().Add("HX-Retarget", "body") w.Header().Add("HX-Retarget", "body")
w.Header().Add("HX-Push-Url", "/") w.Header().Add("HX-Push-Url", "/")
err = tmpl.Execute(w, nil) renderIndexPage(newSession, w)
if err != nil {
log.Printf("my error in executing template, huh\n %s", err)
}
} }
} }

View File

@ -20,7 +20,7 @@ type SessionData struct {
type SessionManagement interface { type SessionManagement interface {
Get(sessionId int) SessionData Get(sessionId int) SessionData
Save(ctx context.Context, roomName string, personId rooms.PersonId) (int, error) Save(ctx context.Context, roomName string, personId rooms.PersonId) (SessionData, error)
Remove(ctx context.Context, sessionId int) error Remove(ctx context.Context, sessionId int) error
} }
@ -45,7 +45,7 @@ func (redisSM RedisSM) Get(sessionId int) SessionData {
log.Printf("> successfully found %d %+v", sessionId, foundSession) log.Printf("> successfully found %d %+v", sessionId, foundSession)
return foundSession return foundSession
} }
func (redisSM RedisSM) Save(ctx context.Context, roomName string, personId rooms.PersonId) (int, error) { func (redisSM RedisSM) Save(ctx context.Context, roomName string, personId rooms.PersonId) (SessionData, error) {
randId := rand.Int() randId := rand.Int()
newSession := SessionData{ newSession := SessionData{
SessionId: randId, SessionId: randId,
@ -57,9 +57,9 @@ func (redisSM RedisSM) Save(ctx context.Context, roomName string, personId rooms
if err != nil { if err != nil {
log.Printf("> error! saving session %+v %s", newSession, err) log.Printf("> error! saving session %+v %s", newSession, err)
return 0, fmt.Errorf("error saving new session: %+v with %s", newSession, err) return SessionData{}, fmt.Errorf("error saving new session: %+v with %s", newSession, err)
} }
return randId, nil return newSession, nil
} }
func (redisSM RedisSM) Remove(ctx context.Context, sessionId int) error { func (redisSM RedisSM) Remove(ctx context.Context, sessionId int) error {
@ -73,9 +73,9 @@ func (d DummySM) Get(sessionId int) SessionData {
log.Printf("get dummy session by %d", sessionId) log.Printf("get dummy session by %d", sessionId)
return SessionData{} return SessionData{}
} }
func (d DummySM) Save(ctx context.Context, roomName string, personId rooms.PersonId) (int, error) { func (d DummySM) Save(ctx context.Context, roomName string, personId rooms.PersonId) (SessionData, error) {
log.Printf("save dummy session with %s %d", roomName, personId) log.Printf("save dummy session with %s %d", roomName, personId)
return 1, nil return SessionData{}, nil
} }
func (d DummySM) Remove(ctx context.Context, sessionId int) error { func (d DummySM) Remove(ctx context.Context, sessionId int) error {
log.Printf("deleting session %d", sessionId) log.Printf("deleting session %d", sessionId)