Skip to content

Commit

Permalink
templates: render static pages using templ
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <me@sumnerevans.com>
  • Loading branch information
sumnerevans committed May 25, 2024
1 parent cb00f4f commit c3612a3
Show file tree
Hide file tree
Showing 24 changed files with 1,059 additions and 822 deletions.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.22.0

require (
github.com/a-h/templ v0.2.697
github.com/beeper/libserv v0.0.0-20231231202820-c7303abfc32c
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/rs/zerolog v1.32.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7Oputl
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/a-h/templ v0.2.697 h1:OILxtWvD0NRJaoCOiZCopRDPW8paroKlGsrAiHLykNE=
github.com/a-h/templ v0.2.697/go.mod h1:5cqsugkq9IerRNucNsI4DEamdHPsoGMQy99DzydLhM8=
github.com/beeper/libserv v0.0.0-20231231202820-c7303abfc32c h1:WqjRVgUO039eiISCjsZC4F9onOEV93DJAk6v33rsZzY=
github.com/beeper/libserv v0.0.0-20231231202820-c7303abfc32c/go.mod h1:b9FFm9y4mEm36G8ytVmS1vkNzJa0KepmcdVY+qf7qRU=
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
55 changes: 41 additions & 14 deletions internal/application.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package internal

import (
"context"
"fmt"
"html/template"
"net/http"
"regexp"
"strings"

"github.com/a-h/templ"
"github.com/beeper/libserv/pkg/requestlog"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
"github.com/sendgrid/sendgrid-go"

"github.com/ColoradoSchoolOfMines/mineshspc.com/database"
"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/config"
"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/contextkeys"
"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/templates"
"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/templates/partials"
"github.com/ColoradoSchoolOfMines/mineshspc.com/website"
)

Expand Down Expand Up @@ -91,36 +97,54 @@ type renderInfo struct {
RedirectIfLoggedIn bool
}

func (a *Application) SettingsContextMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
user, err := a.GetLoggedInTeacher(r)
if err == nil {
ctx = context.WithValue(ctx, contextkeys.ContextKeyLoggedInTeacher, user)
}
ctx = context.WithValue(ctx, contextkeys.ContextKeyRegistrationEnabled, a.Config.RegistrationEnabled)
ctx = context.WithValue(ctx, contextkeys.ContextKeyHostedByHTML, a.Config.HostedByHTML)
next.ServeHTTP(w, r.WithContext(ctx))
})
}

func (a *Application) Start() {
a.Log.Info().Msg("connecting to sendgrid")
a.SendGridClient = sendgrid.NewSendClient(a.Config.SendgridAPIKey)

a.Log.Info().Msg("Starting router")

router := http.NewServeMux()
router.Handle("GET /static/", http.FileServer(http.FS(website.StaticFS))) // Serve static files

noArgs := func(r *http.Request) map[string]any { return nil }

// Static pages
staticPages := map[string]struct {
Template string
ArgGenerator func(r *http.Request) map[string]any
title string
pageName partials.PageName
content templ.Component
}{
"/{$}": {"home.html", noArgs},
"/info": {"info.html", noArgs},
"/authors": {"authors.html", noArgs},
"/rules": {"rules.html", noArgs},
"/register": {"register.html", noArgs},
"/faq": {"faq.html", noArgs},
"/archive": {"archive.html", a.GetArchiveTemplate},
"GET /{$}": {"Home", partials.PageNameHome, templates.Home()},
"GET /info/": {"Info", partials.PageNameInfo, templates.Info()},
"GET /authors/": {"Authors", "", templates.Authors()},
"GET /rules/": {"Rules", partials.PageNameRules, templates.Rules()},
"GET /register/": {"Register", partials.PageNameRegister, templates.Register()},
"GET /faq/": {"FAQ", partials.PageNameFAQ, templates.FAQ()},
"GET /archive/": {"Archive", partials.PageNameArchive, templates.Archive(archiveInfo)},
}
for path, templateInfo := range staticPages {
router.HandleFunc("GET "+path, a.ServeTemplate(a.Log, templateInfo.Template, templateInfo.ArgGenerator))
for path, pageInfo := range staticPages {
router.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if len(pageInfo.pageName) > 0 {
ctx = context.WithValue(ctx, contextkeys.ContextKeyPageName, pageInfo.pageName)
}
templates.Base(pageInfo.title, pageInfo.content).Render(ctx, w)
})
}

// Serve static files
router.Handle("GET /static/", http.FileServer(http.FS(website.StaticFS)))

// Redirect pages
redirects := map[string]string{
"/register/teacher": "/register/teacher/createaccount",
Expand Down Expand Up @@ -243,7 +267,10 @@ func (a *Application) Start() {
router.HandleFunc("GET /volunteer/scan", a.ServeTemplate(a.Log, "volunteerscan.html", a.GetVolunteerScanTemplate))
router.HandleFunc("GET /volunteer/checkin", a.HandleVolunteerCheckIn)

// Middleware goes from bottom up because it's doing function composition.
var handler http.Handler = router
handler = a.SettingsContextMiddleware(handler)
handler = requestlog.AccessLogger(false)(handler)
handler = hlog.RequestIDHandler("request_id", "RequestID")(handler)
handler = hlog.NewHandler(*a.Log)(handler)

Expand Down
Loading

0 comments on commit c3612a3

Please sign in to comment.