fix template embedding.

This commit is contained in:
fiatjaf
2023-05-27 08:15:53 -03:00
parent 768317f4c8
commit e621f0adab

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"embed"
_ "embed" _ "embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
@@ -10,18 +11,13 @@ import (
"strings" "strings"
"text/template" "text/template"
"time" "time"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19" "github.com/nbd-wtf/go-nostr/nip19"
) )
//go:embed static/raw.html //go:embed static/*
var rawHTML string var static embed.FS
//go:embed static/profile.html
var profileHTML string
//go:embed static/note.html
var noteHTML string
func render(w http.ResponseWriter, r *http.Request) { func render(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path, ":~", r.Header.Get("user-agent")) fmt.Println(r.URL.Path, ":~", r.Header.Get("user-agent"))
@@ -186,23 +182,14 @@ func render(w http.ResponseWriter, r *http.Request) {
"eventJSON": string(eventJSON), "eventJSON": string(eventJSON),
} }
templates := make(map[string]string) funcMap := template.FuncMap{
templates["profile"] = profileHTML
templates["note"] = noteHTML
templates["address"] = rawHTML
var funcMap = template.FuncMap{
"BasicFormatting": BasicFormatting, "BasicFormatting": BasicFormatting,
"SanitizeString": html.EscapeString, "SanitizeString": html.EscapeString,
} }
tmpl, err := template.Must(template.New("event"). tmpl := template.Must(template.New("event").
Funcs(funcMap). Funcs(funcMap).
Parse(templates[typ])). ParseFS(static))
ParseFiles("static/head.html", "static/top.html", "static/column_clients.html", "static/footer.html", "static/scripts.js")
if err != nil {
// Handle error
}
if err := tmpl.ExecuteTemplate(w, "event", params); err != nil { if err := tmpl.ExecuteTemplate(w, "event", params); err != nil {
http.Error(w, "error rendering: "+err.Error(), 500) http.Error(w, "error rendering: "+err.Error(), 500)