Changed replace text in HTML to use gonja template

This commit is contained in:
David Allen 2024-03-05 20:47:02 -07:00
parent 64f75345cd
commit dc195afc61
No known key found for this signature in database
GPG key ID: 1D2A29322FBB6FCB

View file

@ -8,6 +8,8 @@ import (
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/nikolalohinski/gonja/v2"
"github.com/nikolalohinski/gonja/v2/exec"
) )
type Server struct { type Server struct {
@ -52,12 +54,18 @@ func (s *Server) WaitForAuthorizationCode(loginUrl string, callback string) (str
}) })
r.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { r.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
// show login page with notice to redirect // show login page with notice to redirect
loginPage, err := os.ReadFile("pages/index.html") template, err := gonja.FromFile("pages/index.html")
if err != nil { if err != nil {
fmt.Printf("failed to load login page: %v\n", err) panic(err)
}
data := exec.NewContext(map[string]interface{}{
"loginUrl": loginUrl,
})
if err = template.Execute(w, data); err != nil { // Prints: Hello Bob!
panic(err)
} }
loginPage = []byte(strings.ReplaceAll(string(loginPage), "{{loginUrl}}", loginUrl))
w.Write(loginPage)
}) })
r.HandleFunc(callback, func(w http.ResponseWriter, r *http.Request) { r.HandleFunc(callback, func(w http.ResponseWriter, r *http.Request) {
// get the code from the OIDC provider // get the code from the OIDC provider