arcast/apps.go

57 lines
929 B
Go
Raw Permalink Normal View History

package arcast
import (
"embed"
"encoding/json"
"io/fs"
"path/filepath"
"forge.cadoles.com/arcad/arcast/pkg/server"
"github.com/pkg/errors"
)
var (
DefaultApps []server.App
2024-04-24 17:32:01 +02:00
//go:embed apps/main/build/**
appsFS embed.FS
)
func init() {
2024-04-24 17:32:01 +02:00
defaultApps, err := loadApps(
"apps/main/build",
)
if err != nil {
panic(errors.WithStack(err))
}
DefaultApps = defaultApps
}
2024-04-24 17:32:01 +02:00
func loadApps(appDirs ...string) ([]server.App, error) {
apps := make([]server.App, 0)
2024-04-24 17:32:01 +02:00
for _, dir := range appDirs {
rawManifest, err := fs.ReadFile(appsFS, filepath.Join(dir, "arcast-app.json"))
if err != nil {
return nil, errors.WithStack(err)
}
var app server.App
if err := json.Unmarshal(rawManifest, &app); err != nil {
return nil, errors.WithStack(err)
}
2024-04-24 17:32:01 +02:00
fs, err := fs.Sub(appsFS, dir)
if err != nil {
return nil, errors.WithStack(err)
}
app.FS = fs
apps = append(apps, app)
}
return apps, nil
}