feat: SSE bs into room
This commit is contained in:
parent
648d3dff80
commit
bb89b8ccf5
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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: <div>hello with data %d! waited %d</div> \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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)))
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
<!doctype html>
|
||||
<html class="no-js" lang="">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta http-equiv="x-ua-compatible" content="ie=edge" />
|
||||
<title>Room {{ .RoomName }} : Some Automoderation</title>
|
||||
<meta name="description" content="" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
|
||||
<script
|
||||
src="https://unpkg.com/htmx.org@1.9.6"
|
||||
integrity="sha384-FhXw7b6AlE/jyjlZH5iHa/tTe9EpJ1Y55RjcgPbjeWMskSxZt1v9qkxLJWNJaGni"
|
||||
crossorigin="anonymous"
|
||||
></script>
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="/static/out.css"
|
||||
type="text/css"
|
||||
media="screen"
|
||||
/>
|
||||
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
|
||||
<!-- Place favicon.ico in the root directory -->
|
||||
</head>
|
||||
<body class="h-screen">
|
||||
<!--[if lt IE 8]>
|
||||
<p class="browserupgrade">
|
||||
You are using an <strong>outdated</strong> browser. Please
|
||||
<a href="http://browsehappy.com/">upgrade your browser</a> to improve
|
||||
your experience.
|
||||
</p>
|
||||
<![endif]-->
|
||||
<div class="h-full w-full grid">
|
||||
<script src="https://unpkg.com/htmx.org/dist/ext/sse.js"></script>
|
||||
<div id="state-receival" class="bg-blue-300"
|
||||
hx-ext="sse" sse-connect="/rooms/random">
|
||||
<div> yoyo </div>
|
||||
<div sse-swap="message"> qweopop </div>
|
||||
</div>
|
||||
<div id="controls" class="bg-green-300">
|
||||
koko
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue