determine tailscale remote user and greet them
1 file changed, 28 insertions(+), 6 deletions(-)
changed files
M domain/content/publisher/mux.go → domain/content/publisher/mux.go
@@ -1,16 +1,38 @@ package publisher import ( + "context" "net/http" - pubtpl "alin.ovh/homestead/domain/content/publisher/templates" - "alin.ovh/homestead/domain/web/templates" + "alin.ovh/homestead/domain/content/publisher/templates" + basetpl "alin.ovh/homestead/domain/web/templates" ihttp "alin.ovh/homestead/shared/http" + "tailscale.com/tailcfg" ) -func (app *App) Index(w http.ResponseWriter, _ *http.Request) ihttp.Error { - err := pubtpl.IndexPage(app.siteSettings, templates.PageSettings{ - Title: "Home", +type user struct{} + +func (app *App) WithUserContext(fn ihttp.HandleFunc) ihttp.HandleFunc { + return func(w http.ResponseWriter, r *http.Request) ihttp.Error { + ctx := r.Context() + who, err := app.localClient.WhoIs(ctx, r.RemoteAddr) + if err != nil { + return ihttp.InternalServerError("cannot determine user", err) + } + + return fn(w, r.WithContext( + context.WithValue(ctx, user{}, who.UserProfile.Clone()), + )) + } +} + +func (app *App) Index(w http.ResponseWriter, r *http.Request) ihttp.Error { + user := r.Context().Value(user{}).(*tailcfg.UserProfile) + err := templates.IndexPage(app.siteSettings, templates.PageSettings{ + PageSettings: basetpl.PageSettings{ + Title: "Home", + }, + User: user.LoginName, }).Render(w) if err != nil { return ihttp.InternalServerError("Failed to render index page", err)@@ -21,7 +43,7 @@ } func (app *App) Style(w http.ResponseWriter, r *http.Request) ihttp.Error { w.Header().Set("Content-Type", "text/css") - http.ServeFileFS(w, r, templates.Files, "style.css") + http.ServeFileFS(w, r, basetpl.Files, "style.css") return nil }