BaseURL configuration variable generalization

This commit is contained in:
2022-03-25 15:11:29 +01:00
parent 41748363d1
commit 33dbb6ea47
9 changed files with 51 additions and 40 deletions

View File

@ -2,9 +2,12 @@ package route
import (
"net/http"
"strings"
"forge.cadoles.com/wpetit/hydra-passwordless/internal/config"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service"
"gitlab.com/wpetit/goweb/service/template"
"gitlab.com/wpetit/goweb/template/html"
)
@ -14,8 +17,8 @@ func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Da
data, err := template.Extend(data,
html.WithFlashes(w, r, ctn),
template.WithBuildInfo(w, r, ctn),
withBaseURL(w, r, ctn),
)
if err != nil {
panic(errors.Wrap(err, "could not extend template data"))
}
@ -23,6 +26,19 @@ func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Da
return data
}
func withBaseURL(w http.ResponseWriter, r *http.Request, ctn *service.Container) template.DataExtFunc {
return func(data template.Data) (template.Data, error) {
conf, err := config.From(ctn)
if err != nil {
return nil, errors.WithStack(err)
}
data["BaseURL"] = strings.TrimSuffix(conf.HTTP.BaseURL, "/")
return data, nil
}
}
func renderErrorPage(w http.ResponseWriter, r *http.Request, statusCode int, title, description string) error {
ctn := container.Must(r.Context())
tmpl := template.Must(ctn)

View File

@ -1,10 +1,13 @@
package route
import (
"fmt"
"net/http"
netMail "net/mail"
"strings"
"forge.cadoles.com/wpetit/hydra-passwordless/internal/command"
"forge.cadoles.com/wpetit/hydra-passwordless/internal/config"
"forge.cadoles.com/wpetit/hydra-passwordless/internal/hydra"
"github.com/gorilla/csrf"
"github.com/pkg/errors"
@ -81,6 +84,7 @@ func handleLoginForm(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(ctn)
hydr := hydra.Must(ctn)
bus := cqrs.Must(ctn)
conf := config.Must(ctn)
if err := r.ParseForm(); err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
@ -143,14 +147,21 @@ func handleLoginForm(w http.ResponseWriter, r *http.Request) {
return
}
var baseURL string
if conf.HTTP.BaseURL != "" {
baseURL = strings.TrimSuffix(conf.HTTP.BaseURL, "/")
} else {
baseURL = fmt.Sprintf("%s//%s", r.Host, r.URL.Scheme)
}
cmd := &command.SendConfirmationEmailRequest{
Email: email,
Challenge: challenge,
DefaultScheme: r.URL.Scheme,
DefaultAddress: r.Host,
RememberMe: rememberMe == "on",
ClientName: res.Client.ClientName,
ClientURI: res.Client.ClientURI,
Email: email,
Challenge: challenge,
BaseURL: baseURL,
RememberMe: rememberMe == "on",
ClientName: res.Client.ClientName,
ClientURI: res.Client.ClientURI,
}
if _, err := bus.Exec(ctx, cmd); err != nil {
panic(errors.Wrap(err, "could not execute command"))