- Add streamd serve start|stop|restart|status|update with auto-written streamd.yaml - Panel modules: STREAMD_MODULES_DIR for describe, YAML channel fallback, textarea value fix - Per-stream WireGuard/SOCKS egress profiles + Settings UI - Drop drm-* from release builds; track streamd-bg.sh / streamd-update.sh wrappers - Docs: streamd-server-setup lifecycle and update flow
165 lines
4.5 KiB
Go
165 lines
4.5 KiB
Go
// Package modcfg loads an app module's values from modules/<name>/,
|
|
// with environment-variable overrides.
|
|
//
|
|
// Load order:
|
|
// 1. module-publish.yaml (tracked universal template) if present
|
|
// 2. module.yaml (local / release copy; may add secrets) overlays on top
|
|
//
|
|
// Local module.yaml is gitignored and never published. Release zips ship
|
|
// module-publish.yaml renamed to module.yaml.
|
|
package modcfg
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"streamd/modhost"
|
|
"streamd/repo"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Dir returns the directory holding a module's values.
|
|
// Prefers the active modules dir (see modhost / --modules-dir); then the
|
|
// directory beside a panel/release binary (<mod>/.bin/<name>); then
|
|
// apps/modules/<name>/ for older checkouts.
|
|
func Dir(name string) string {
|
|
preferred := filepath.Join(modhost.ModulesDir(""), name)
|
|
if st, err := os.Stat(preferred); err == nil && st.IsDir() {
|
|
return preferred
|
|
}
|
|
if modhost.ModulesDirOverride() != "" {
|
|
return preferred
|
|
}
|
|
if beside := dirBesideExecutable(name); beside != "" {
|
|
return beside
|
|
}
|
|
root := repo.Root()
|
|
if _, err := os.Stat(filepath.Join(root, "modules")); err == nil {
|
|
return filepath.Join(root, "modules", name)
|
|
}
|
|
return filepath.Join(root, "apps", "modules", name)
|
|
}
|
|
|
|
// dirBesideExecutable finds module.yaml next to a panel/release binary layout:
|
|
// <modDir>/.bin/<name>[.exe]
|
|
// <modDir>/.bin/<goos>-<goarch>/<name>[.exe]
|
|
func dirBesideExecutable(name string) string {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
if resolved, err := filepath.EvalSymlinks(exe); err == nil {
|
|
exe = resolved
|
|
}
|
|
base := strings.TrimSuffix(filepath.Base(exe), ".exe")
|
|
if !strings.EqualFold(base, name) {
|
|
return ""
|
|
}
|
|
binDir := filepath.Dir(exe)
|
|
candidates := []string{}
|
|
if strings.EqualFold(filepath.Base(binDir), ".bin") {
|
|
candidates = append(candidates, filepath.Dir(binDir))
|
|
}
|
|
parent := filepath.Dir(binDir)
|
|
if strings.EqualFold(filepath.Base(parent), ".bin") {
|
|
candidates = append(candidates, filepath.Dir(parent))
|
|
}
|
|
for _, dir := range candidates {
|
|
if fileExists(filepath.Join(dir, "module.yaml")) || fileExists(filepath.Join(dir, "module-publish.yaml")) {
|
|
return dir
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func fileExists(p string) bool {
|
|
st, err := os.Stat(p)
|
|
return err == nil && !st.IsDir()
|
|
}
|
|
|
|
// Path returns the preferred values file path (module.yaml).
|
|
func Path(name string) string {
|
|
return filepath.Join(Dir(name), "module.yaml")
|
|
}
|
|
|
|
// PublishPath is the tracked universal template.
|
|
func PublishPath(name string) string {
|
|
return filepath.Join(Dir(name), "module-publish.yaml")
|
|
}
|
|
|
|
// Result reports where a module's values came from, for `drm modules`.
|
|
type Result struct {
|
|
Path string
|
|
Loaded bool
|
|
}
|
|
|
|
// Load reads module-publish.yaml then overlays module.yaml into dst.
|
|
// Missing files are not an error — the module runs on Go defaults plus flags/env.
|
|
func Load(name string, dst any) (Result, error) {
|
|
dir := Dir(name)
|
|
publish := filepath.Join(dir, "module-publish.yaml")
|
|
local := filepath.Join(dir, "module.yaml")
|
|
res := Result{Path: local}
|
|
|
|
loadedAny := false
|
|
if b, err := os.ReadFile(publish); err == nil {
|
|
if err := yaml.Unmarshal(b, dst); err != nil {
|
|
return res, fmt.Errorf("%s: %w", publish, err)
|
|
}
|
|
loadedAny = true
|
|
res.Path = publish
|
|
} else if !os.IsNotExist(err) {
|
|
return res, err
|
|
}
|
|
|
|
if b, err := os.ReadFile(local); err == nil {
|
|
if err := yaml.Unmarshal(b, dst); err != nil {
|
|
return res, fmt.Errorf("%s: %w", local, err)
|
|
}
|
|
loadedAny = true
|
|
res.Path = local
|
|
} else if !os.IsNotExist(err) {
|
|
return res, err
|
|
}
|
|
|
|
res.Loaded = loadedAny
|
|
return res, nil
|
|
}
|
|
|
|
// EnvKey is the environment variable a field override is read from:
|
|
// TG4_POLICY_KEY for module "tg4", field "policy_key".
|
|
func EnvKey(module, field string) string {
|
|
clean := func(s string) string {
|
|
s = strings.ToUpper(s)
|
|
s = strings.ReplaceAll(s, "-", "_")
|
|
s = strings.ReplaceAll(s, ".", "_")
|
|
return s
|
|
}
|
|
return clean(module) + "_" + clean(field)
|
|
}
|
|
|
|
// Env returns the environment override for a field, or "".
|
|
func Env(module, field string) string {
|
|
return strings.TrimSpace(os.Getenv(EnvKey(module, field)))
|
|
}
|
|
|
|
// Override picks the first non-empty of: flag value, environment, current value.
|
|
func Override(module, field, flagVal, current string) string {
|
|
if strings.TrimSpace(flagVal) != "" {
|
|
return flagVal
|
|
}
|
|
if v := Env(module, field); v != "" {
|
|
return v
|
|
}
|
|
return current
|
|
}
|
|
|
|
// Resolve makes a module-relative path absolute against the repo root.
|
|
func Resolve(p string) string {
|
|
if p == "" || filepath.IsAbs(p) {
|
|
return p
|
|
}
|
|
return filepath.Join(repo.Root(), p)
|
|
}
|