From 487f203b8071275e66fb2f000dd2c6b0c6e6dffc Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Fri, 24 May 2024 22:31:21 -0600 Subject: [PATCH] fixup! templates: render static pages using templ Signed-off-by: Sumner Evans --- go.mod | 1 + go.sum | 2 + internal/application.go | 92 ++++++++---------------- internal/contextkeys/keys.go | 10 +++ internal/templates/base.templ | 14 ++-- internal/templates/home.templ | 15 ++-- internal/templates/partials/footer.templ | 15 ++-- internal/templates/partials/navbar.templ | 56 ++++++++------- internal/templates/partials/pagenames.go | 12 ++++ 9 files changed, 108 insertions(+), 109 deletions(-) create mode 100644 internal/contextkeys/keys.go create mode 100644 internal/templates/partials/pagenames.go diff --git a/go.mod b/go.mod index 7cc5a5a..81b89b2 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 252e699..fea993e 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/application.go b/internal/application.go index 8edb93a..9d35b68 100644 --- a/internal/application.go +++ b/internal/application.go @@ -8,12 +8,15 @@ import ( "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" @@ -94,41 +97,6 @@ type renderInfo struct { RedirectIfLoggedIn bool } -type ContextKey string - -const ( - ContextKeyLoggedInTeacher ContextKey = "com.mineshspc.logged_in_user" -) - -type Info struct { - config config.Configuration - pageName string -} - -func (i *Info) Title() string { - return i.pageName -} - -func (i *Info) HostedByHTML() template.HTML { - return i.config.HostedByHTML -} - -func (i *Info) PageName() partials.PageName { - return partials.PageName(strings.ToLower(i.pageName)) -} - -func (i *Info) RegistrationEnabled() bool { - return i.config.RegistrationEnabled -} - -func (*Info) Username(ctx context.Context) string { - if loggedInUser, ok := ctx.Value(ContextKeyLoggedInTeacher).(*database.Teacher); ok { - return loggedInUser.Name - } else { - return "" - } -} - func (a *Application) Start() { a.Log.Info().Msg("connecting to sendgrid") a.SendGridClient = sendgrid.NewSendClient(a.Config.SendgridAPIKey) @@ -140,35 +108,30 @@ func (a *Application) Start() { noArgs := func(r *http.Request) map[string]any { return nil } - router.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { - info := Info{config: a.Config, pageName: "Home"} - ctx := r.Context() - user, err := a.GetLoggedInTeacher(r) - if err == nil { - ctx = context.WithValue(ctx, ContextKeyLoggedInTeacher, user) - } - templates.Base(&info, templates.Home(&info)).Render(ctx, w) - }) - - router.HandleFunc("GET /info", func(w http.ResponseWriter, r *http.Request) { - info := Info{config: a.Config, pageName: "Info"} - ctx := r.Context() - user, err := a.GetLoggedInTeacher(r) - if err == nil { - ctx = context.WithValue(ctx, ContextKeyLoggedInTeacher, user) - } - templates.Base(&info, templates.Info()).Render(ctx, w) - }) - - router.HandleFunc("GET /authors", func(w http.ResponseWriter, r *http.Request) { - info := Info{config: a.Config, pageName: "Authors"} - ctx := r.Context() - user, err := a.GetLoggedInTeacher(r) - if err == nil { - ctx = context.WithValue(ctx, ContextKeyLoggedInTeacher, user) - } - templates.Base(&info, templates.Authors()).Render(ctx, w) - }) + // Static pages + staticPages2 := map[string]struct { + title string + pageName partials.PageName + content templ.Component + }{ + "GET /{$}": {"Home", partials.PageNameHome, templates.Home()}, + "GET /info": {"Info", partials.PageNameInfo, templates.Info()}, + "GET /authors": {"Authors", "", templates.Authors()}, + } + for path, pageInfo := range staticPages2 { + router.HandleFunc(path, 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) + } + if len(pageInfo.pageName) > 0 { + ctx = context.WithValue(ctx, contextkeys.ContextKeyPageName, pageInfo.pageName) + } + ctx = context.WithValue(ctx, contextkeys.ContextKeyRegistrationEnabled, a.Config.RegistrationEnabled) + templates.Base(pageInfo.title, pageInfo.content).Render(ctx, w) + }) + } // Static pages staticPages := map[string]struct { @@ -307,6 +270,7 @@ func (a *Application) Start() { router.HandleFunc("GET /volunteer/checkin", a.HandleVolunteerCheckIn) var handler http.Handler = router + handler = requestlog.AccessLogger(false)(handler) handler = hlog.RequestIDHandler("request_id", "RequestID")(handler) handler = hlog.NewHandler(*a.Log)(handler) diff --git a/internal/contextkeys/keys.go b/internal/contextkeys/keys.go new file mode 100644 index 0000000..2d31ace --- /dev/null +++ b/internal/contextkeys/keys.go @@ -0,0 +1,10 @@ +package contextkeys + +type contextKey int + +const ( + ContextKeyLoggedInTeacher contextKey = iota + ContextKeyPageName + ContextKeyRegistrationEnabled + ContextKeyHostedByHTML +) diff --git a/internal/templates/base.templ b/internal/templates/base.templ index 11e8989..fed929c 100644 --- a/internal/templates/base.templ +++ b/internal/templates/base.templ @@ -2,13 +2,7 @@ package templates import "github.com/ColoradoSchoolOfMines/mineshspc.com/internal/templates/partials" -type BaseInfo interface { - partials.NavbarInfo - partials.FooterInfo - Title() string -} - -templ Base(info BaseInfo, content templ.Component) { +templ Base(title string, content templ.Component) { @@ -19,7 +13,7 @@ templ Base(info BaseInfo, content templ.Component) { name="description" content="The CS@Mines High School Programming Competition is a competition for high school students to write programs that solve problems." /> - { info.Title() } | CS@Mines High School Programming Competition + { title } | CS@Mines High School Programming Competition @@ -49,11 +43,11 @@ templ Base(info BaseInfo, content templ.Component) { /> - @partials.Navbar(info) + @partials.Navbar()
@content
- @partials.Footer(info) + @partials.Footer() diff --git a/internal/templates/home.templ b/internal/templates/home.templ index 6f862b3..3a972ab 100644 --- a/internal/templates/home.templ +++ b/internal/templates/home.templ @@ -1,10 +1,15 @@ package templates -type HomeInfo interface { - RegistrationEnabled() bool +import ( + "github.com/ColoradoSchoolOfMines/mineshspc.com/internal/contextkeys" +) + +func RegistrationEnabled(ctx context.Context) bool { + enabled, ok := ctx.Value(contextkeys.ContextKeyRegistrationEnabled).(bool) + return ok && enabled } -templ Home(info HomeInfo) { +templ Home() {
@@ -16,11 +21,11 @@ templ Home(info HomeInfo) {

Engage in exciting problems at the Mines HSPC! - if info.RegistrationEnabled() { + if RegistrationEnabled(ctx) { Registration is now open for the Spring 2024 competition. }

- if info.RegistrationEnabled() { + if RegistrationEnabled(ctx) {

The registration deadline is April 13th diff --git a/internal/templates/partials/footer.templ b/internal/templates/partials/footer.templ index 53f0d07..28ac469 100644 --- a/internal/templates/partials/footer.templ +++ b/internal/templates/partials/footer.templ @@ -1,13 +1,16 @@ package partials -import "html/template" +import ( + "html/template" + "github.com/ColoradoSchoolOfMines/mineshspc.com/internal/contextkeys" +) -type FooterInfo interface { - HostedByHTML() template.HTML - // TODO make more things configurable +func GetHostedByHTML(ctx context.Context) template.HTML { + hostedByHTML, _ := ctx.Value(contextkeys.ContextKeyLoggedInTeacher).(template.HTML) + return hostedByHTML } -templ Footer(info FooterInfo) { +templ Footer() {