Files
sonr/pkg/dwn/embed.go
T

72 lines
1.5 KiB
Go
Raw Normal View History

package dwn
2024-09-07 18:12:58 -04:00
import (
2024-09-21 21:42:51 -04:00
"bytes"
"context"
2024-09-07 18:12:58 -04:00
_ "embed"
2024-09-18 02:22:17 -04:00
"encoding/json"
2024-09-07 18:12:58 -04:00
2024-09-21 21:42:51 -04:00
"github.com/a-h/templ"
2024-09-07 18:12:58 -04:00
"github.com/ipfs/boxo/files"
)
2024-09-18 02:22:17 -04:00
//go:embed app.wasm
2024-09-07 18:12:58 -04:00
var dwnWasmData []byte
2024-09-11 15:10:54 -04:00
//go:embed sw.js
var swJSData []byte
2024-09-22 01:26:15 -04:00
var (
dwnWasmFile = files.NewBytesFile(dwnWasmData)
swJSFile = files.NewBytesFile(swJSData)
)
// NewVaultDirectory creates a new directory with the default files
func NewVaultDirectory(cnfg *Config) (files.Node, error) {
2024-09-22 01:26:15 -04:00
dwnJSON, err := json.Marshal(cnfg)
if err != nil {
return nil, err
}
dwnStr, err := templ.JSONString(cnfg)
if err != nil {
return nil, err
}
w := bytes.NewBuffer(nil)
err = indexFile(dwnStr).Render(context.Background(), w)
if err != nil {
return nil, err
}
fileMap := map[string]files.Node{
"config.json": files.NewBytesFile(dwnJSON),
"sw.js": swJSFile,
"app.wasm": dwnWasmFile,
"index.html": files.NewBytesFile(w.Bytes()),
}
return files.NewMapDirectory(fileMap), nil
}
2024-09-21 21:42:51 -04:00
// Use IndexHTML template to generate the index file
func IndexHTMLFile(c *Config) (files.Node, error) {
2024-09-21 21:42:51 -04:00
str, err := templ.JSONString(c)
if err != nil {
return nil, err
}
w := bytes.NewBuffer(nil)
err = indexFile(str).Render(context.Background(), w)
if err != nil {
return nil, err
}
indexData := w.Bytes()
return files.NewBytesFile(indexData), nil
}
// MarshalConfigFile uses the config template to generate the dwn config file
func MarshalConfigFile(c *Config) (files.Node, error) {
2024-09-21 21:42:51 -04:00
dwnConfigData, err := json.Marshal(c)
2024-09-18 02:22:17 -04:00
if err != nil {
return nil, err
}
return files.NewBytesFile(dwnConfigData), nil
}