Merge branch 'major-rewrite' of https://git.towk2.me/towk/configurator-ng into major-rewrite

This commit is contained in:
David Allen 2025-08-14 16:53:48 -06:00
commit 05a4913e70
Signed by: towk
GPG key ID: 793B2924A49B3A3F
2 changed files with 229 additions and 31 deletions

View file

@ -3,11 +3,16 @@ package service
import (
"encoding/json"
"fmt"
"io/fs"
"net/http"
"os"
"path/filepath"
"plugin"
"slices"
"time"
configurator "git.towk2.me/towk/configurator/pkg"
"git.towk2.me/towk/configurator/pkg/util"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/chi/v5"
)
@ -66,9 +71,9 @@ func (s *Service) Serve() error {
router.Get("/profile/{id}/data", s.GetProfileData())
router.Post("/profile/{id}/data", s.SetProfileData())
router.Delete("/profile/{id}/data", s.DeleteProfileData())
router.Post("/profile/{id}/paths/{path}", s.CreateProfilePath())
router.Delete("/profile/{id}/paths/{path}", s.DeleteProfilePath())
router.Get("/profile/{id}/paths/{path}", s.GetProfilePath())
// router.Post("/profile/{id}/paths/{path}", s.CreateProfilePath())
// router.Delete("/profile/{id}/paths/{path}", s.DeleteProfilePath())
// router.Get("/profile/{id}/paths/{path}", s.GetProfilePath())
// plugins
router.Get("/plugins", s.GetPlugins())
@ -90,14 +95,99 @@ func (s *Service) FetchJwks(uri string) {
}
func LoadProfileFromFile(path string) (*Profile, error) {
return LoadFromJSONFile[Profile](path)
return loadFromJSONFile[Profile](path)
}
func LoadPluginFromFile(path string) (*configurator.Plugin, error) {
return LoadFromJSONFile[configurator.Plugin](path)
// LoadPluginFromFile loads a single plugin given a single file path
func LoadPluginFromFile(path string) (configurator.Plugin, error) {
var (
isDir bool
err error
loadedPlugin *plugin.Plugin
)
// skip loading plugin if path is a directory with no error
if isDir, err = util.IsDirectory(path); err == nil && isDir {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("failed to test if plugin path is directory: %v", err)
}
// try and open the plugin
loadedPlugin, err = plugin.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open plugin at path '%s': %v", path, err)
}
// load the "Target" symbol from plugin
symbol, err := loadedPlugin.Lookup("Target")
if err != nil {
return nil, fmt.Errorf("failed to look up symbol at path '%s': %v", path, err)
}
// assert that the plugin is a valid configurator.Plugin
target, ok := symbol.(configurator.Plugin)
if !ok {
return nil, fmt.Errorf("failed to load the correct symbol type at path '%s'", path)
}
return target, nil
}
func LoadFromJSONFile[T any](path string) (*T, error) {
// LoadPluginsFromDir loads all plugins in a given directory.
//
// Returns a map of plugins. Each plugin can be accessed by the name
// returned by the plugin.GetName() implemented.
func LoadPluginsFromDir(dirpath string) (map[string]configurator.Plugin, error) {
// check if verbose option is supplied
var (
cps = make(map[string]configurator.Plugin)
err error
)
// helper to check for valid extensions
var hasValidExt = func(path string) bool {
var validExts = []string{".so", ".dylib", ".dll"}
return slices.Contains(validExts, filepath.Ext(path))
}
// walk all files in directory only loading *valid* plugins
err = filepath.Walk(dirpath, func(path string, info fs.FileInfo, err error) error {
// skip trying to load generator plugin if directory or error
if info.IsDir() || err != nil {
return nil
}
// only try loading if file has .so extension
if hasValidExt(path) {
return nil
}
// load the plugin from current path
p, err := LoadPluginFromFile(path)
if err != nil {
return fmt.Errorf("failed to load plugin in directory '%s': %v", path, err)
}
// map each plugin by name for lookup
cps[p.Name()] = p
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to walk directory '%s': %v", dirpath, err)
}
return cps, nil
}
func SaveProfileToFile(path string, profile *Profile) error {
return saveToJSONFile(path, profile)
}
func SavePluginToFile(path string, plugin *configurator.Plugin) error {
return saveToJSONFile(path, plugin)
}
func loadFromJSONFile[T any](path string) (*T, error) {
var (
res *T
contents []byte
@ -117,6 +207,27 @@ func LoadFromJSONFile[T any](path string) (*T, error) {
return res, err
}
func (s *Service) BuildProfilePath(id string) string {
return s.RootPath + PLUGINS_RELPATH + "/" + id
func saveToJSONFile[T any](path string, data T) error {
var (
contents []byte
err error
)
contents, err = json.Marshal(data)
if err != nil {
return fmt.Errorf("failed to marshal data to JSON: %v", err)
}
err = os.WriteFile(path, contents, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to write JSON to file: %v", err)
}
return nil
}
func (s *Service) PathForProfileWithID(id string) string {
return s.RootPath + PROFILES_RELPATH + "/" + id
}
func (s *Service) PathForPlugins() string {
return s.RootPath + PLUGINS_RELPATH
}