github.com/pbberlin/go-pwa@v0.0.0-20220328105622-7c26e0ca1ab8/cmd/server/tpl.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "html/template" 6 "net/http" 7 "strings" 8 9 "github.com/pbberlin/go-pwa/pkg/cfg" 10 ) 11 12 type Scaffold struct { 13 Title, Desc string 14 15 JSTags template.HTML 16 CSSTags template.HTML 17 Content template.HTML 18 } 19 20 // SP provides the static path for a uri; 21 // prepending an app prefix and a version 22 func (sc *Scaffold) SP(uri string) string { 23 parts := strings.Split(uri, "/") 24 if len(parts) > 3 { // behold empty token from leading "/" 25 return uri 26 } 27 needle := fmt.Sprintf("/%v/", parts[1]) 28 replacement := fmt.Sprintf("/%v/%v/", parts[1], cfg.Get().TS) 29 uri = strings.Replace(uri, needle, replacement, 1) 30 return uri 31 } 32 33 func (sc *Scaffold) render(w http.ResponseWriter, cnt *strings.Builder) { 34 35 w.Header().Set("Content-Type", "text/html") 36 w.Header().Set("Content-Security-Policy", cfg.Get().CSP) 37 38 if sc.Title == "" { 39 sc.Title = cfg.Get().Title 40 } 41 if sc.Desc == "" { 42 sc.Desc = cfg.Get().Description 43 } 44 45 sc.JSTags = template.HTML(cfg.Get().JS) 46 sc.CSSTags = template.HTML(cfg.Get().CSS) 47 48 sc.Content = template.HTML(cnt.String()) 49 50 // standard output to print merged data 51 err := cfg.Get().TplMain.Execute(w, sc) 52 if err != nil { 53 fmt.Fprintf(w, "error executing tplMain: %v", err) 54 return 55 } 56 57 // fmt.Fprintf(w, cfg.Get().HTML5, sc.Title, sc.Desc, cfg.Get().JS, cfg.Get().CSS, cnt.String()) 58 }