added lang support to all html templates and cleaned up template rendering

This commit is contained in:
partisan 2024-10-08 22:11:06 +02:00
parent 5dd3114e2d
commit d9d0301548
18 changed files with 645 additions and 410 deletions

View file

@ -3,7 +3,9 @@ package main
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"html/template"
"net/http"
"strings"
)
@ -15,9 +17,35 @@ var (
"add": func(a, b int) int {
return a + b
},
"translate": Translate,
"toJSON": func(v interface{}) (string, error) {
jsonBytes, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(jsonBytes), nil
},
}
)
// Helper function to render templates without elapsed time measurement
func renderTemplate(w http.ResponseWriter, tmplName string, data map[string]interface{}) {
// Parse the template with common functions (including translate)
tmpl, err := template.New(tmplName).Funcs(funcs).ParseFiles("templates/" + tmplName)
if err != nil {
printErr("Error parsing template: %v", err)
http.Error(w, Translate("internal_server_error"), http.StatusInternalServerError)
return
}
// Execute the template
err = tmpl.Execute(w, data)
if err != nil {
printErr("Error executing template: %v", err)
http.Error(w, Translate("internal_server_error"), http.StatusInternalServerError)
}
}
func generateStrongRandomString(length int) string {
bytes := make([]byte, length)
_, err := rand.Read(bytes)