From bb89b8ccf5c10ec05c9d175a4ba17bf2c94a25f2 Mon Sep 17 00:00:00 2001 From: efim Date: Sun, 5 Nov 2023 15:00:36 +0000 Subject: [PATCH] feat: SSE bs into room --- routes/index_page.go | 6 ++- routes/login_page.go | 11 ++-- routes/room_page.go | 98 ++++++++++++++++++++++++++++++++++++ routes/routes.go | 3 ++ routes/static/out.css | 14 ++++++ routes/templates/room.gohtml | 45 +++++++++++++++++ 6 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 routes/room_page.go create mode 100644 routes/templates/room.gohtml diff --git a/routes/index_page.go b/routes/index_page.go index ce6eff0..a3a4a6b 100644 --- a/routes/index_page.go +++ b/routes/index_page.go @@ -18,7 +18,11 @@ func indexPageRoute( ) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var templFile = "templates/index.gohtml" - session := getContextSession(r.Context()) + session, found := getContextSession(r.Context()) + if !found { + log.Printf("/ session not found, should be impossible") + // TODO return error i guess + } data := struct { SessionStringToken string SomeString string diff --git a/routes/login_page.go b/routes/login_page.go index 3276e5a..8c9a202 100644 --- a/routes/login_page.go +++ b/routes/login_page.go @@ -33,8 +33,13 @@ const authCookieName = "auth" const loginPath = "/login" type contextKey string -func getContextSession(ctx context.Context) sessions.SessionData { - return ctx.Value(contextKey("session")).(sessions.SessionData) +func getContextSession(ctx context.Context) (sessions.SessionData, bool) { + val := ctx.Value(contextKey("session")) + if val == nil { + return sessions.SessionData{}, false + } else { + return ctx.Value(contextKey("session")).(sessions.SessionData), true + } } // checks sessionId from cookie @@ -77,7 +82,7 @@ func authedPageMiddleware( } func renderLoginPage(w http.ResponseWriter) { - var templFile = "templates/login.gohtml" + templFile := "templates/login.gohtml" tmpl := template.Must(template.ParseFS(templateFs, templFile)) err := tmpl.Execute(w, nil) if err != nil { diff --git a/routes/room_page.go b/routes/room_page.go new file mode 100644 index 0000000..513029f --- /dev/null +++ b/routes/room_page.go @@ -0,0 +1,98 @@ +package routes + +import ( + "embed" + "fmt" + "html/template" + "log" + "math/rand" + "net/http" + "time" + + "sunshine.industries/some-automoderation/rooms" + "sunshine.industries/some-automoderation/sessions" +) + +const roomPath = "/room/" + +// registering all routes for page and logic of /room/:roomName +func registerPageRoutes( + templateFs *embed.FS, + sessionSM sessions.SessionManagement, + roomsM rooms.RoomManager, +) { + http.HandleFunc("/rooms/random", streamingBsRoute()) + + http.Handle(roomPath, + authedPageMiddleware( + sessionSM, + http.StripPrefix(roomPath, roomPageRoute(templateFs, roomsM)))) + +} + +func streamingBsRoute() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "no-cache") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("Content-Type", "text/event-stream") + startTime, endTime := 0, 0 + for { + log.Printf("another step in streaming bs") + data := "data:
hello with data %d! waited %d
\n\n" + startTime = time.Now().Nanosecond() + diff := endTime - startTime + fmt.Fprintf(w, data, rand.Intn(100), diff) + w.(http.Flusher).Flush() + time.Sleep(10 * time.Second) + endTime = time.Now().Nanosecond() + } + } +} + +func roomPageRoute( + templateFs *embed.FS, + roomsM rooms.RoomManager, +) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + roomName := r.URL.Path + if roomName == "" { + log.Printf("access to empty room") + // TODO return error i suppose + w.Header().Add("HX-Redirect", "/") + return + } + + // check session, + session, found := getContextSession(r.Context()) + if !found { + log.Printf("session not found %t", found) + // TODO here will be rendering of + // 'create this room' or 'join this room' + // but yeah, let's just use auth middle that redirects to / + w.Header().Add("HX-Redirect", "/") + return + } + if session.RoomId != roomName { + log.Printf("session found %+v, but for wrong room, trying to access %s", session, roomName) + w.Header().Add("HX-Redirect", "/") + return + } + + // now we should have a session for this specific room + fmt.Printf("all checks for room %s passed with %+v", roomName, session) + + templFile := "templates/room.gohtml" + tmpl := template.Must(template.ParseFS(templateFs, templFile)) + + pageData := struct { + RoomName string + }{ + RoomName: roomName, + } + + err := tmpl.Execute(w, pageData) + if err != nil { + log.Printf("/room/%s my error in executing template, huh\n %s", roomName, err) + } + } +} diff --git a/routes/routes.go b/routes/routes.go index 49da562..c2a3da7 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -23,6 +23,9 @@ func RegisterRoutes(sessionsM sessions.SessionManagement, rooms rooms.RoomManage sessionsM, indexPageRoute(&templateFs, sessionsM, rooms))) + // main conversation room page + registerPageRoutes(&templateFs, sessionsM, rooms) + // static resources route http.Handle("/static/", http.FileServer(http.FS(staticFilesFs))) diff --git a/routes/static/out.css b/routes/static/out.css index ac08999..712bbe5 100644 --- a/routes/static/out.css +++ b/routes/static/out.css @@ -558,6 +558,10 @@ video { height: 100vh; } +.w-full { + width: 100%; +} + .grid-cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); } @@ -600,6 +604,16 @@ video { background-color: rgb(251 191 36 / var(--tw-bg-opacity)); } +.bg-blue-300 { + --tw-bg-opacity: 1; + background-color: rgb(147 197 253 / var(--tw-bg-opacity)); +} + +.bg-green-300 { + --tw-bg-opacity: 1; + background-color: rgb(134 239 172 / var(--tw-bg-opacity)); +} + .bg-main-700\/25 { background-color: rgb(194 65 12 / 0.25); } diff --git a/routes/templates/room.gohtml b/routes/templates/room.gohtml new file mode 100644 index 0000000..6d88d59 --- /dev/null +++ b/routes/templates/room.gohtml @@ -0,0 +1,45 @@ + + + + + + Room {{ .RoomName }} : Some Automoderation + + + + + + + + + + +
+ +
+
yoyo
+
qweopop
+
+
+ koko +
+ +
+ +