Skip to content

Commit

Permalink
fixup! 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 7ef603e commit ce0374a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 14 additions & 5 deletions internal/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ 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)
Expand Down Expand Up @@ -125,14 +138,9 @@ func (a *Application) Start() {
for path, pageInfo := range staticPages {
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)
})
}
Expand Down Expand Up @@ -261,6 +269,7 @@ func (a *Application) Start() {

// 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
6 changes: 3 additions & 3 deletions internal/templates/partials/footer.templ
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package partials

import (
"html/template"

"github.com/ColoradoSchoolOfMines/mineshspc.com/internal/contextkeys"
)

func GetHostedByHTML(ctx context.Context) template.HTML {
hostedByHTML, _ := ctx.Value(contextkeys.ContextKeyLoggedInTeacher).(template.HTML)
hostedByHTML, _ := ctx.Value(contextkeys.ContextKeyHostedByHTML).(template.HTML)
return hostedByHTML
}

Expand All @@ -32,8 +33,7 @@ templ Footer() {
&copy; 2023 Mines ACM.
View the
<a href="https://github.com/ColoradoSchoolOfMines/mineshspc.com" target="_blank">
source
code
source code
</a>.
@templ.Raw(GetHostedByHTML(ctx))
</p>
Expand Down

0 comments on commit ce0374a

Please sign in to comment.