refactor: index page route separated

This commit is contained in:
efim 2023-11-05 05:00:01 +00:00
parent 83c634c2b9
commit 648d3dff80
4 changed files with 52 additions and 13 deletions

37
routes/index_page.go Normal file
View File

@ -0,0 +1,37 @@
package routes
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"sunshine.industries/some-automoderation/rooms"
"sunshine.industries/some-automoderation/sessions"
)
func indexPageRoute(
templateFs *embed.FS,
sessionSM sessions.SessionManagement,
roomsM rooms.RoomManager,
) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var templFile = "templates/index.gohtml"
session := getContextSession(r.Context())
data := struct {
SessionStringToken string
SomeString string
SessionToken sessions.SessionData
}{
fmt.Sprintf("%+v", session),
"hello!",
session,
}
tmpl := template.Must(template.ParseFS(templateFs, templFile))
err := tmpl.Execute(w, data)
if err != nil {
log.Printf("my error in executing template, huh\n %s", err)
}
})
}

View File

@ -2,9 +2,6 @@ package routes
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"sunshine.industries/some-automoderation/rooms"
@ -24,15 +21,7 @@ func RegisterRoutes(sessionsM sessions.SessionManagement, rooms rooms.RoomManage
// main page template
http.Handle("/", authedPageMiddleware(
sessionsM,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var templFile = "templates/index.gohtml"
session := getContextSession(r.Context())
tmpl := template.Must(template.ParseFS(templateFs, templFile))
err := tmpl.Execute(w, fmt.Sprintf("%+v", session))
if err != nil {
log.Printf("my error in executing template, huh\n %s", err)
}
})))
indexPageRoute(&templateFs, sessionsM, rooms)))
// static resources route
http.Handle("/static/",

View File

@ -613,6 +613,15 @@ video {
line-height: 1.75rem;
}
.text-blue-700 {
--tw-text-opacity: 1;
color: rgb(29 78 216 / var(--tw-text-opacity));
}
.underline {
text-decoration-line: underline;
}
.invalid\:bg-red-700:invalid {
--tw-bg-opacity: 1;
background-color: rgb(185 28 28 / var(--tw-bg-opacity));

View File

@ -31,6 +31,10 @@
<h1>Hello</h1>
<p>This is index</p>
<p>Your session is {{ . }}</p>
<p>Your session is {{ .SessionStringToken }}</p>
<p>Some string is {{ .SomeString }}</p>
<a href="/room/{{ .SessionToken.RoomId }}"
class="text-blue-700 underline"
>You've logged into a room {{ .SessionToken.RoomId }}</a>
</body>
</html>