Load templates with overlapfs to allow custom templates

This commit is contained in:
Melon 2023-10-08 16:43:27 +01:00
parent bf39f4421b
commit 3ef2923e52
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
4 changed files with 23 additions and 26 deletions

3
go.mod
View File

@ -4,9 +4,11 @@ go 1.21.1
require ( require (
github.com/1f349/cache v0.0.2 github.com/1f349/cache v0.0.2
github.com/1f349/overlapfs v0.0.1
github.com/1f349/violet v0.0.10 github.com/1f349/violet v0.0.10
github.com/MrMelon54/exit-reload v0.0.1 github.com/MrMelon54/exit-reload v0.0.1
github.com/MrMelon54/mjwt v0.1.1 github.com/MrMelon54/mjwt v0.1.1
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/subcommands v1.2.0 github.com/google/subcommands v1.2.0
github.com/google/uuid v1.3.1 github.com/google/uuid v1.3.1
github.com/julienschmidt/httprouter v1.3.0 github.com/julienschmidt/httprouter v1.3.0
@ -17,7 +19,6 @@ require (
require ( require (
github.com/MrMelon54/rescheduler v0.0.2 // indirect github.com/MrMelon54/rescheduler v0.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/protobuf v1.4.2 // indirect github.com/golang/protobuf v1.4.2 // indirect
github.com/kr/text v0.2.0 // indirect github.com/kr/text v0.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect

2
go.sum
View File

@ -1,6 +1,8 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/1f349/cache v0.0.2 h1:27QD6zPd9xYyvh9V1qqWq+EAt5+N+qvyGWKfnjMrhP8= github.com/1f349/cache v0.0.2 h1:27QD6zPd9xYyvh9V1qqWq+EAt5+N+qvyGWKfnjMrhP8=
github.com/1f349/cache v0.0.2/go.mod h1:LibAMy13dF0KO1fQA9aEjZPBCB6Y4b5kKYEQJUqc2rQ= github.com/1f349/cache v0.0.2/go.mod h1:LibAMy13dF0KO1fQA9aEjZPBCB6Y4b5kKYEQJUqc2rQ=
github.com/1f349/overlapfs v0.0.1 h1:LAxBolrXFAgU0yqZtXg/C/aaPq3eoQSPpBc49BHuTp0=
github.com/1f349/overlapfs v0.0.1/go.mod h1:I6aItQycr7nrzplmfNXp/QF9tTmKRSgY3fXmu/7Ky2o=
github.com/1f349/violet v0.0.10 h1:2HuQq7SddV60JZ4Xr7DmmhTOPbjiF+1Uqk+d6O1f18U= github.com/1f349/violet v0.0.10 h1:2HuQq7SddV60JZ4Xr7DmmhTOPbjiF+1Uqk+d6O1f18U=
github.com/1f349/violet v0.0.10/go.mod h1:Uzu6I1pLBP5UEzcUCTQBbk/NTfI5TAABSrowa8DSpR0= github.com/1f349/violet v0.0.10/go.mod h1:Uzu6I1pLBP5UEzcUCTQBbk/NTfI5TAABSrowa8DSpR0=
github.com/MrMelon54/exit-reload v0.0.1 h1:sxHa59tNEQMcikwuX2+93lw6Vi1+R7oCRF8a0C3alXc= github.com/MrMelon54/exit-reload v0.0.1 h1:sxHa59tNEQMcikwuX2+93lw6Vi1+R7oCRF8a0C3alXc=

View File

@ -5,35 +5,21 @@ import (
_ "embed" _ "embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/1f349/lavender/server/pages"
"github.com/MrMelon54/mjwt/auth" "github.com/MrMelon54/mjwt/auth"
"github.com/MrMelon54/mjwt/claims" "github.com/MrMelon54/mjwt/claims"
"github.com/golang-jwt/jwt/v4" "github.com/golang-jwt/jwt/v4"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"html/template"
"log" "log"
"net/http" "net/http"
"strings" "strings"
"time" "time"
) )
func init() {
pageParse, err := template.New("pages").Parse(flowPopupHtml)
if err != nil {
log.Fatal("flow.go: Failed to parse flow popup HTML:", err)
}
flowPopupTemplate = pageParse
pageParse, err = template.New("pages").Parse(flowCallbackHtml)
if err != nil {
log.Fatal("flow.go: Failed to parse flow callback HTML:", err)
}
flowCallbackTemplate = pageParse
}
func (h *HttpServer) flowPopup(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) { func (h *HttpServer) flowPopup(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) {
err := flowPopupTemplate.Execute(rw, map[string]any{ err := pages.FlowTemplates.Execute(rw, map[string]any{
"ServiceName": h.conf.ServiceName, "ServiceName": h.conf.ServiceName,
"Origin": req.URL.Query().Get("origin"), "Origin": req.URL.Query().Get("origin"),
}) })
@ -57,7 +43,7 @@ func (h *HttpServer) flowPopupPost(rw http.ResponseWriter, req *http.Request, _
} }
// save state for use later // save state for use later
state := login.Config.Namespace + "%" + uuid.NewString() state := login.Config.Namespace + ":" + uuid.NewString()
h.flowState.Set(state, flowStateData{ h.flowState.Set(state, flowStateData{
login, login,
targetOrigin, targetOrigin,
@ -79,7 +65,7 @@ func (h *HttpServer) flowCallback(rw http.ResponseWriter, req *http.Request, _ h
q := req.URL.Query() q := req.URL.Query()
state := q.Get("state") state := q.Get("state")
n := strings.IndexByte(state, '%') n := strings.IndexByte(state, ':')
if !h.manager.CheckNamespace(state[:n]) { if !h.manager.CheckNamespace(state[:n]) {
http.Error(rw, "Invalid state", http.StatusBadRequest) http.Error(rw, "Invalid state", http.StatusBadRequest)
return return
@ -146,7 +132,7 @@ func (h *HttpServer) flowCallback(rw http.ResponseWriter, req *http.Request, _ h
return return
} }
_ = flowCallbackTemplate.Execute(rw, map[string]any{ _ = pages.FlowTemplates.Execute(rw, map[string]any{
"ServiceName": h.conf.ServiceName, "ServiceName": h.conf.ServiceName,
"TargetOrigin": v.targetOrigin, "TargetOrigin": v.targetOrigin,
"TargetMessage": v3, "TargetMessage": v3,

View File

@ -3,18 +3,26 @@ package pages
import ( import (
"embed" "embed"
_ "embed" _ "embed"
"github.com/1f349/overlapfs"
"html/template" "html/template"
"os" "os"
"path/filepath"
) )
var ( var (
//go:embed pages/* //go:embed *.go.html
flowPages embed.FS flowPages embed.FS
flowTemplates *template.Template FlowTemplates *template.Template
) )
func LoadPages(wd string) { func LoadPages(wd string) error {
wdFs := os.DirFS(wd) wwwDir := filepath.Join(wd, "www")
err := os.Mkdir(wwwDir, os.ModePerm)
flowPages.Open() if err != nil {
return nil
}
wdFs := os.DirFS(wwwDir)
o := overlapfs.OverlapFS{A: flowPages, B: wdFs}
FlowTemplates, err = template.ParseFS(o, "*.go.html")
return err
} }