refactor: common base template for pages

will allow to add common header and stuff
This commit is contained in:
efim 2023-11-13 05:25:02 +00:00
parent b1f2e896b9
commit 1297fcf35d
8 changed files with 140 additions and 55 deletions

10
routes/base_page.go Normal file
View File

@ -0,0 +1,10 @@
package routes
type baseData struct {
Title string
}
type pageData struct {
Base baseData
Content interface {}
}

View File

@ -17,23 +17,31 @@ func indexPageRoute(
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" 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
} }
data := struct { type MainData struct {
SessionStringToken string SessionStringToken string
SomeString string SomeString string
SessionToken sessions.SessionData SessionToken sessions.SessionData
}{ }
data := pageData {
Base: baseData{
Title: "hello base template title",
},
Content: MainData{
fmt.Sprintf("%+v", session), fmt.Sprintf("%+v", session),
"hello!", "hello!",
session, session,
},
} }
tmpl := template.Must(template.ParseFS(templateFs, templFile)) tmpl := template.Must(template.ParseFS(templateFs, templFile, baseFile))
err := tmpl.Execute(w, data) err := tmpl.ExecuteTemplate(w, "full-page", data)
if err != nil { if err != nil {
log.Printf("my error in executing template, huh\n %s", err) log.Printf("my error in executing template, huh\n %s", err)
} }

View File

@ -13,6 +13,10 @@ import (
"sunshine.industries/some-automoderation/sessions" "sunshine.industries/some-automoderation/sessions"
) )
type MainData struct {
IsRoomExisting bool
}
// function to register all http routes for servicing auth pages and logic // function to register all http routes for servicing auth pages and logic
func registerLoginRoutes( func registerLoginRoutes(
templateFs *embed.FS, templateFs *embed.FS,
@ -82,9 +86,19 @@ func authedPageMiddleware(
} }
func renderLoginPage(w http.ResponseWriter) { func renderLoginPage(w http.ResponseWriter) {
baseFile := "templates/base.gohtml"
templFile := "templates/login.gohtml" templFile := "templates/login.gohtml"
tmpl := template.Must(template.ParseFS(templateFs, templFile)) tmpl := template.Must(template.ParseFS(templateFs, templFile, baseFile))
err := tmpl.Execute(w, nil) data := pageData {
Base: baseData{
Title: "login",
},
Content: MainData{
IsRoomExisting: false,
},
}
err := tmpl.ExecuteTemplate(w, "full-page", data)
if err != nil { if err != nil {
log.Printf("my error in executing template, huh\n %s", err) log.Printf("my error in executing template, huh\n %s", err)
} }
@ -226,7 +240,7 @@ func joinRoomHandler(templateFs *embed.FS,
PasswordHash: personPass, PasswordHash: personPass,
Id: rooms.RandomPersonId(), Id: rooms.RandomPersonId(),
} }
err := roomsM.Update(context.TODO(), room.Name, func(fromRoom rooms.Room) (toRoom rooms.Room) { err := roomsM.Update(r.Context(), room.Name, func(fromRoom rooms.Room) (toRoom rooms.Room) {
log.Printf("/login/join about to modify room %+v", fromRoom) log.Printf("/login/join about to modify room %+v", fromRoom)
toRoom = fromRoom toRoom = fromRoom
toRoom.AllKnownPeople = append(toRoom.AllKnownPeople, person) toRoom.AllKnownPeople = append(toRoom.AllKnownPeople, person)
@ -245,7 +259,7 @@ func joinRoomHandler(templateFs *embed.FS,
// now we have room and person, can create a session // now we have room and person, can create a session
// and we've checked password // and we've checked password
err = roomsM.Update(context.TODO(), room.Name, func(fromRoom rooms.Room) (toRoom rooms.Room) { err = roomsM.Update(r.Context(), room.Name, func(fromRoom rooms.Room) (toRoom rooms.Room) {
toRoom = fromRoom toRoom = fromRoom
toRoom.Paricipants = append(toRoom.Paricipants, person.Id) toRoom.Paricipants = append(toRoom.Paricipants, person.Id)
return toRoom return toRoom

View File

@ -196,7 +196,8 @@ func roomPageRoute(
fmt.Printf("all checks for room %s passed with %+v", roomName, session) fmt.Printf("all checks for room %s passed with %+v", roomName, session)
templFile := "templates/room.gohtml" templFile := "templates/room.gohtml"
tmpl := template.Must(template.ParseFS(templateFs, templFile)) baseFile := "templates/base.gohtml"
tmpl := template.Must(template.ParseFS(templateFs, templFile, baseFile))
type GestureData struct { type GestureData struct {
Name string Name string
@ -210,15 +211,21 @@ func roomPageRoute(
}) })
} }
pageData := struct { contentData := struct {
Room rooms.Room Room rooms.Room
Gestures []GestureData Gestures []GestureData
}{ }{
Room: room, Room: room,
Gestures: gesturesData, Gestures: gesturesData,
} }
data := pageData{
Base: baseData{
Title: "room-lala-from-base",
},
Content: contentData,
}
err = tmpl.Execute(w, pageData) err = tmpl.ExecuteTemplate(w, "full-page", data)
if err != nil { if err != nil {
log.Printf("/room/%s my error in executing template, huh\n %s", roomName, err) log.Printf("/room/%s my error in executing template, huh\n %s", roomName, err)
} }

View File

@ -0,0 +1,36 @@
{{ define "full-page" }}
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>{{ .Base.Title }}</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>
<!--[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]-->
{{ template "main-content" .Content }}
</body>
</html>
{{ end }}

View File

@ -28,7 +28,8 @@
your experience. your experience.
</p> </p>
<![endif]--> <![endif]-->
{{ define "main-content" }}
<main>
<h1>Hello</h1> <h1>Hello</h1>
<p>This is index</p> <p>This is index</p>
<p>Your session is {{ .SessionStringToken }}</p> <p>Your session is {{ .SessionStringToken }}</p>
@ -36,5 +37,7 @@
<a href="/room/{{ .SessionToken.RoomId }}" <a href="/room/{{ .SessionToken.RoomId }}"
class="text-blue-700 underline" class="text-blue-700 underline"
>You've logged into a room {{ .SessionToken.RoomId }}</a> >You've logged into a room {{ .SessionToken.RoomId }}</a>
</main>
{{ end }}
</body> </body>
</html> </html>

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" /> <meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>Untitled!!!</title> <title>Should be set in base template</title>
<meta name="description" content="" /> <meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<script <script
@ -28,6 +28,7 @@
your experience. your experience.
</p> </p>
<![endif]--> <![endif]-->
{{ define "main-content" }}
<main class="grid grid-rows-[auto,1fr] h-screen"> <main class="grid grid-rows-[auto,1fr] h-screen">
<header> <header>
<h1>Some Automoderation: login page</h1> <h1>Some Automoderation: login page</h1>
@ -142,5 +143,6 @@
</script> </script>
</section> </section>
</main> </main>
{{ end }}
</body> </body>
</html> </html>

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" /> <meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>Room {{ .Room.Name }} : Some Automoderation</title> <title>Should be set in base template</title>
<meta name="description" content="" /> <meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
@ -21,7 +21,7 @@
<link rel="apple-touch-icon" href="/apple-touch-icon.png" /> <link rel="apple-touch-icon" href="/apple-touch-icon.png" />
<!-- Place favicon.ico in the root directory --> <!-- Place favicon.ico in the root directory -->
</head> </head>
<body class="h-screen"> <body>
<!--[if lt IE 8]> <!--[if lt IE 8]>
<p class="browserupgrade"> <p class="browserupgrade">
You are using an <strong>outdated</strong> browser. Please You are using an <strong>outdated</strong> browser. Please
@ -29,6 +29,8 @@
your experience. your experience.
</p> </p>
<![endif]--> <![endif]-->
{{ define "main-content" }}
<main class="h-screen">
<div class="h-full w-full grid"> <div class="h-full w-full grid">
<script src="https://unpkg.com/htmx.org/dist/ext/sse.js"></script> <script src="https://unpkg.com/htmx.org/dist/ext/sse.js"></script>
<div <div
@ -53,12 +55,15 @@
{{ .Name }} {{ .Name }}
</button> </button>
{{ end }} {{ end }}
<button hx-get="/rooms/releaseHand" <button
hx-get="/rooms/releaseHand"
class="bg-white rounded border-yellow-700 border-4" class="bg-white rounded border-yellow-700 border-4"
> >
Release Hand Release Hand
</button> </button>
</div> </div>
</div> </div>
</main>
{{ end }}
</body> </body>
</html> </html>