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 ( import (
"encoding/json" "encoding/json"
"flag"
"io" "io"
"io/fs" "io/fs"
"io/ioutil" "io/ioutil"
@ -41,9 +42,15 @@ import (
var ( var (
indexTpl *template.Template indexTpl *template.Template
CustomBodyHTML = ""
CustomHeadHTML = "" 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) { func DeclareRoutes(cfg *config.Config, router *gin.Engine) {
appConfig := map[string]interface{}{} appConfig := map[string]interface{}{}
@ -116,11 +123,12 @@ func serveOrReverse(forced_url string, cfg *config.Config) gin.HandlerFunc {
v, _ := ioutil.ReadAll(resp.Body) 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)) indexTpl = template.Must(template.New("index.html").Parse(v2))
if err := indexTpl.ExecuteTemplate(c.Writer, "index.html", map[string]string{ if err := indexTpl.ExecuteTemplate(c.Writer, "index.html", map[string]string{
"Body": CustomBodyHTML,
"Head": CustomHeadHTML, "Head": CustomHeadHTML,
}); err != nil { }); err != nil {
log.Println("Unable to return index.html:", err.Error()) log.Println("Unable to return index.html:", err.Error())