2024-02-13 11:00:48 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-02-13 21:39:34 +00:00
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2024-02-19 09:09:30 +00:00
|
|
|
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/jwt"
|
2024-02-13 11:00:48 +00:00
|
|
|
)
|
|
|
|
|
2024-02-19 09:09:30 +00:00
|
|
|
func (h *BaseHandler) Temporal(w http.ResponseWriter, r *http.Request, principal *AuthenticatedPrincipal) {
|
2024-02-13 21:39:34 +00:00
|
|
|
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
|
2024-02-15 17:43:35 +00:00
|
|
|
Host: h.config.Temporal.UIHost,
|
2024-02-13 21:39:34 +00:00
|
|
|
Scheme: "http",
|
|
|
|
})
|
|
|
|
|
2024-02-19 09:09:30 +00:00
|
|
|
originalDirector := proxy.Director
|
2024-02-13 21:39:34 +00:00
|
|
|
|
2024-02-19 09:09:30 +00:00
|
|
|
proxy.Director = func(r *http.Request) {
|
|
|
|
originalDirector(r)
|
|
|
|
// Add authentication token to be able to access temporal.
|
|
|
|
// FIXME: Maybe cache it somehow so we don't generate it on every request?
|
|
|
|
token, _ := jwt.NewTokenForUser(
|
|
|
|
h.config.Jwt.PrivateKey,
|
|
|
|
h.config.Jwt.PublicKey,
|
|
|
|
principal.User.Email,
|
|
|
|
)
|
|
|
|
r.Header.Add("Authorization", "Bearer "+token)
|
2024-02-13 11:00:48 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 21:39:34 +00:00
|
|
|
proxy.ServeHTTP(w, r)
|
2024-02-13 11:00:48 +00:00
|
|
|
}
|