Add option to add custom content before </head> and </body>

This commit is contained in:
nemunaire 2025-10-20 11:41:19 +07:00
commit 3f5e2c6dd4

View file

@ -23,6 +23,7 @@ package web
import (
"encoding/json"
"flag"
"io"
"io/fs"
"io/ioutil"
@ -41,9 +42,15 @@ import (
var (
indexTpl *template.Template
CustomBodyHTML = ""
CustomHeadHTML = ""
)
func init() {
flag.StringVar(&CustomHeadHTML, "custom-head-html", CustomHeadHTML, "Add custom HTML right before </head>")
flag.StringVar(&CustomBodyHTML, "custom-body-html", CustomBodyHTML, "Add custom HTML right before </body>")
}
func DeclareRoutes(cfg *config.Config, router *gin.Engine) {
appConfig := map[string]interface{}{}
@ -116,11 +123,12 @@ func serveOrReverse(forced_url string, cfg *config.Config) gin.HandlerFunc {
v, _ := ioutil.ReadAll(resp.Body)
v2 := strings.Replace(string(v), "</head>", "{{ .Head }}</head>", 1)
v2 := strings.Replace(strings.Replace(string(v), "</head>", "{{ .Head }}</head>", 1), "</body>", "{{ .Body }}</body>", 1)
indexTpl = template.Must(template.New("index.html").Parse(v2))
if err := indexTpl.ExecuteTemplate(c.Writer, "index.html", map[string]string{
"Body": CustomBodyHTML,
"Head": CustomHeadHTML,
}); err != nil {
log.Println("Unable to return index.html:", err.Error())