Skip to content

Commit

Permalink
Super simple replacement of text/template
Browse files Browse the repository at this point in the history
  • Loading branch information
oderwat committed Jan 9, 2022
1 parent e426c37 commit 4400d29
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 60 deletions.
3 changes: 1 addition & 2 deletions pkg/app/gen/app-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ self.addEventListener("install", event => {
caches.open(cacheName).
then(cache => {
return cache.addAll([
{{range $path, $element := .ResourcesToCache}}"{{$path}}",
{{end}}
{{.ResourcesToCache}}
]);
}).
then(() => {
Expand Down
81 changes: 25 additions & 56 deletions pkg/app/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"strconv"
"strings"
"sync"
"text/template"
"time"

"github.com/maxence-charriere/go-app/v9/pkg/errors"
Expand Down Expand Up @@ -342,21 +341,11 @@ func (h *Handler) makeAppJS() []byte {
)
}

var b bytes.Buffer
if err := template.
Must(template.New("app.js").Parse(appJS)).
Execute(&b, struct {
Env string
Wasm string
WorkerJS string
}{
Env: btos(env),
Wasm: h.Resources.AppWASM(),
WorkerJS: h.resolvePackagePath("/app-worker.js"),
}); err != nil {
panic(errors.New("initializing app.js failed").Wrap(err))
}
return b.Bytes()
s := appJS
s = strings.ReplaceAll(s, "{{.Env}}", btos(env))
s = strings.ReplaceAll(s, "{{.Wasm}}", h.Resources.AppWASM())
s = strings.ReplaceAll(s, "{{.WorkerJS}}", h.resolvePackagePath("/app-worker.js"))
return []byte(s)
}

func (h *Handler) makeAppWorkerJS() []byte {
Expand All @@ -382,19 +371,15 @@ func (h *Handler) makeAppWorkerJS() []byte {
cacheResources(h.Scripts...)
cacheResources(h.CacheableResources...)

var b bytes.Buffer
if err := template.
Must(template.New("app-worker.js").Parse(appWorkerJS)).
Execute(&b, struct {
Version string
ResourcesToCache map[string]struct{}
}{
Version: h.Version,
ResourcesToCache: cacheableResources,
}); err != nil {
panic(errors.New("initializing app-worker.js failed").Wrap(err))
}
return b.Bytes()
s := appWorkerJS
s = strings.ReplaceAll(s, "{{.Version}}", h.Version)

var resources string
for entry := range cacheableResources {
resources += fmt.Sprintf("\"%s\",\n", entry)
}
s = strings.ReplaceAll(s, "{{.ResourcesToCache}}", resources)
return []byte(s)
}

func (h *Handler) makeManifestJSON() []byte {
Expand All @@ -408,33 +393,17 @@ func (h *Handler) makeManifestJSON() []byte {
return s
}

var b bytes.Buffer
if err := template.
Must(template.New("manifest.webmanifest").Parse(manifestJSON)).
Execute(&b, struct {
ShortName string
Name string
Description string
DefaultIcon string
LargeIcon string
BackgroundColor string
ThemeColor string
Scope string
StartURL string
}{
ShortName: h.ShortName,
Name: h.Name,
Description: h.Description,
DefaultIcon: h.Icon.Default,
LargeIcon: h.Icon.Large,
BackgroundColor: h.BackgroundColor,
ThemeColor: h.ThemeColor,
Scope: normalize(h.Resources.Package()),
StartURL: normalize(h.Resources.Package()),
}); err != nil {
panic(errors.New("initializing manifest.webmanifest failed").Wrap(err))
}
return b.Bytes()
s := manifestJSON
s = strings.ReplaceAll(s, "{{.ShortName}}", h.ShortName)
s = strings.ReplaceAll(s, "{{.Name}}", h.Name)
s = strings.ReplaceAll(s, "{{.Description}}", h.Description)
s = strings.ReplaceAll(s, "{{.DefaultIcon}}", h.Icon.Default)
s = strings.ReplaceAll(s, "{{.LargeIcon}}", h.Icon.Large)
s = strings.ReplaceAll(s, "{{.BackgroundColor}}", h.BackgroundColor)
s = strings.ReplaceAll(s, "{{.ThemeColor}}", h.ThemeColor)
s = strings.ReplaceAll(s, "{{.Scope}}", normalize(h.Resources.Package()))
s = strings.ReplaceAll(s, "{{.StartURL}}", normalize(h.Resources.Package()))
return []byte(s)
}

func (h *Handler) initProxyResources() {
Expand Down
Loading

0 comments on commit 4400d29

Please sign in to comment.