2023-10-14 16:06:02 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/helpers"
|
2024-08-15 16:25:15 +00:00
|
|
|
"go.uber.org/zap"
|
2023-10-14 16:06:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const StaticRoute = "/static/"
|
|
|
|
|
|
|
|
type StaticController struct {
|
2024-05-08 14:07:40 +00:00
|
|
|
baseDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStaticController(baseDir string) *StaticController {
|
|
|
|
return &StaticController{
|
|
|
|
baseDir: baseDir,
|
|
|
|
}
|
2023-10-14 16:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *StaticController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2024-05-08 14:07:40 +00:00
|
|
|
fs := http.FileServer(http.Dir(sc.baseDir + "/static"))
|
2023-10-14 16:06:02 +00:00
|
|
|
http.StripPrefix(StaticRoute, fs).ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
type IndexController struct {
|
2024-08-15 16:25:15 +00:00
|
|
|
l *zap.SugaredLogger
|
2023-10-15 15:49:33 +00:00
|
|
|
downstreamConstroller http.Handler
|
2024-05-08 14:07:40 +00:00
|
|
|
baseDir string
|
2023-10-14 16:06:02 +00:00
|
|
|
}
|
|
|
|
|
2024-08-15 16:25:15 +00:00
|
|
|
func NewIndexController(l *zap.SugaredLogger, downstream http.Handler, baseDir string) *IndexController {
|
2023-10-14 16:06:02 +00:00
|
|
|
return &IndexController{
|
2023-10-15 15:49:33 +00:00
|
|
|
l: l,
|
|
|
|
downstreamConstroller: downstream,
|
2024-05-08 14:07:40 +00:00
|
|
|
baseDir: baseDir,
|
2023-10-14 16:06:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ic IndexController) serveUI(w http.ResponseWriter, r *http.Request) (int, int, error) {
|
2023-10-15 18:11:50 +00:00
|
|
|
funcs := template.FuncMap{
|
|
|
|
"issuer": func() string { return "toto" },
|
|
|
|
}
|
|
|
|
|
2024-05-08 14:07:40 +00:00
|
|
|
lp := filepath.Join(ic.baseDir, "templates", "index.html")
|
|
|
|
hdrTpl := filepath.Join(ic.baseDir, "templates", "header.html")
|
|
|
|
footTpl := filepath.Join(ic.baseDir, "templates", "footer.html")
|
2023-10-15 18:11:50 +00:00
|
|
|
tmpl, err := template.New("index.html").Funcs(funcs).ParseFiles(hdrTpl, footTpl, lp)
|
2023-10-14 16:06:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, -1, fmt.Errorf("failed to init template: %w", err)
|
|
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
if err := tmpl.Execute(buf, nil); err != nil {
|
|
|
|
return http.StatusInternalServerError, -1, fmt.Errorf("failed to execute template: %w", err)
|
|
|
|
}
|
|
|
|
n, err := io.Copy(w, buf)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, int(n), fmt.Errorf("failed to write response; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, int(n), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ic *IndexController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.RequestURI != "/" {
|
2024-10-19 12:58:00 +00:00
|
|
|
ic.l.Debugf("Serving URI %q to oidc handler", r.RequestURI)
|
2023-10-15 15:49:33 +00:00
|
|
|
ic.downstreamConstroller.ServeHTTP(w, r)
|
2023-10-14 16:06:02 +00:00
|
|
|
return
|
|
|
|
}
|
2023-10-15 18:11:50 +00:00
|
|
|
returncode, _, err := ic.serveUI(w, r)
|
2023-10-14 16:06:02 +00:00
|
|
|
if err != nil {
|
|
|
|
ic.l.Errorf("Error serving UI: %s", err.Error())
|
|
|
|
helpers.HandleResponse(w, r, returncode, nil, ic.l)
|
|
|
|
}
|
|
|
|
}
|