feat: updated pkg implementations
This commit is contained in:
parent
86f37555b2
commit
8b161135ff
10 changed files with 579 additions and 140 deletions
|
|
@ -11,10 +11,10 @@ import (
|
|||
"slices"
|
||||
"time"
|
||||
|
||||
configurator "git.towk2.me/towk/configurator/pkg"
|
||||
"git.towk2.me/towk/configurator/pkg/util"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
makeshift "git.towk2.me/towk/makeshift/pkg"
|
||||
"git.towk2.me/towk/makeshift/pkg/util"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
|
|
@ -35,9 +35,9 @@ func New() *Service {
|
|||
Addr: ":5050",
|
||||
RootPath: "./",
|
||||
Environment: map[string]string{
|
||||
"CONFIGURATOR_HOST": "",
|
||||
"CONFIGURATOR_ROOT": "",
|
||||
"ACCESS_TOKEN": "",
|
||||
"MAKESHIFT_HOST": "",
|
||||
"MAKESHIFT_ROOT": "",
|
||||
"ACCESS_TOKEN": "",
|
||||
},
|
||||
PluginsMaxCount: DEFAULT_PLUGINS_MAX_COUNT,
|
||||
ProfilesMaxCount: DEFAULT_PROFILES_MAX_COUNT,
|
||||
|
|
@ -63,22 +63,27 @@ func (s *Service) Init() error {
|
|||
}
|
||||
err = os.MkdirAll(s.PathForData(), 0o777)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to make service profile path: %v", err)
|
||||
return fmt.Errorf("failed to make service data path: %v", err)
|
||||
}
|
||||
|
||||
// create the default files
|
||||
err = os.WriteFile(s.PathForMetadata(), []byte(DEFAULT_METADATA), 0o777)
|
||||
err = os.WriteFile(s.PathForMetadata(), []byte(FILE_METADATA), 0o777)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to make service metadata file: %v", err)
|
||||
}
|
||||
err = os.WriteFile(s.PathForHome(), []byte(DEFAULT_HOME), 0o777)
|
||||
err = os.WriteFile(s.PathForHome(), []byte(FILE_HOME_PAGE), 0o777)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to make service metadata file: %v", err)
|
||||
return fmt.Errorf("failed to make service home page file: %v", err)
|
||||
}
|
||||
err = os.WriteFile(s.PathForProfileWithID("default"), []byte(FILE_DEFAULT_PROFILE), 0o777)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to make service default profile file: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Serve() starts the configurator service and waits for requests.
|
||||
// Serve() starts the makeshift service and waits for requests.
|
||||
func (s *Service) Serve() error {
|
||||
router := chi.NewRouter()
|
||||
router.Use(middleware.RequestID)
|
||||
|
|
@ -98,7 +103,7 @@ func (s *Service) Serve() error {
|
|||
router.Get("/list/*", s.List())
|
||||
|
||||
// profiles
|
||||
router.Get("/profiles", s.GetProfiles())
|
||||
router.Get("/profiles", s.ListProfiles())
|
||||
// router.Post("/profiles", s.CreateProfiles())
|
||||
router.Get("/profile/{id}", s.GetProfile())
|
||||
router.Post("/profile/{id}", s.CreateProfile())
|
||||
|
|
@ -111,8 +116,9 @@ func (s *Service) Serve() error {
|
|||
|
||||
// plugins
|
||||
router.Get("/plugins", s.ListPlugins())
|
||||
router.Post("/plugins", s.CreatePlugin())
|
||||
router.Delete("/plugins/{id}", s.DeletePlugin())
|
||||
router.Get("/plugin/{name}", s.GetPlugin())
|
||||
router.Post("/plugin/{name}", s.CreatePlugin())
|
||||
router.Delete("/plugin/{name}", s.DeletePlugin())
|
||||
}
|
||||
|
||||
// always available public routes go here
|
||||
|
|
@ -128,22 +134,23 @@ func (s *Service) FetchJwks(uri string) {
|
|||
|
||||
}
|
||||
|
||||
func LoadProfileFromFile(path string) (*Profile, error) {
|
||||
return loadFromJSONFile[Profile](path)
|
||||
func LoadProfileFromFile(path string) (*makeshift.Profile, error) {
|
||||
return loadFromJSONFile[makeshift.Profile](path)
|
||||
}
|
||||
|
||||
// LoadPluginFromFile loads a single plugin given a single file path
|
||||
func LoadPluginFromFile(path string) (configurator.Plugin, error) {
|
||||
func LoadPluginFromFile(path string) (makeshift.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 {
|
||||
isDir, err = util.IsDirectory(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to test if plugin path is directory: %v", err)
|
||||
} else if isDir {
|
||||
return nil, fmt.Errorf("path is a directory")
|
||||
}
|
||||
|
||||
// try and open the plugin
|
||||
|
|
@ -153,15 +160,15 @@ func LoadPluginFromFile(path string) (configurator.Plugin, error) {
|
|||
}
|
||||
|
||||
// load the "Target" symbol from plugin
|
||||
symbol, err := loadedPlugin.Lookup("Target")
|
||||
symbol, err := loadedPlugin.Lookup("Makeshift")
|
||||
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)
|
||||
// assert that the plugin is a valid makeshift.Plugin
|
||||
target, ok := symbol.(makeshift.Plugin)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to load the correct symbol type at path '%s'", path)
|
||||
return nil, fmt.Errorf("failed to assert the correct symbol type at path '%s'", path)
|
||||
}
|
||||
return target, nil
|
||||
}
|
||||
|
|
@ -170,10 +177,10 @@ func LoadPluginFromFile(path string) (configurator.Plugin, error) {
|
|||
//
|
||||
// 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) {
|
||||
func LoadPluginsFromDir(dirpath string) (map[string]makeshift.Plugin, error) {
|
||||
// check if verbose option is supplied
|
||||
var (
|
||||
cps = make(map[string]configurator.Plugin)
|
||||
cps = make(map[string]makeshift.Plugin)
|
||||
err error
|
||||
)
|
||||
|
||||
|
|
@ -213,11 +220,11 @@ func LoadPluginsFromDir(dirpath string) (map[string]configurator.Plugin, error)
|
|||
return cps, nil
|
||||
}
|
||||
|
||||
func SaveProfileToFile(path string, profile *Profile) error {
|
||||
func SaveProfileToFile(path string, profile *makeshift.Profile) error {
|
||||
return saveToJSONFile(path, profile)
|
||||
}
|
||||
|
||||
func SavePluginToFile(path string, plugin *configurator.Plugin) error {
|
||||
func SavePluginToFile(path string, plugin *makeshift.Plugin) error {
|
||||
return saveToJSONFile(path, plugin)
|
||||
}
|
||||
|
||||
|
|
@ -230,12 +237,12 @@ func loadFromJSONFile[T any](path string) (*T, error) {
|
|||
|
||||
contents, err = os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read plugin file: %v", err)
|
||||
return nil, fmt.Errorf("failed to read file: %v", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(contents, &res)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal plugin: %v", err)
|
||||
return nil, fmt.Errorf("failed to unmarshal contents from JSON: %v", err)
|
||||
}
|
||||
|
||||
return res, err
|
||||
|
|
@ -259,11 +266,11 @@ func saveToJSONFile[T any](path string, data T) error {
|
|||
}
|
||||
|
||||
func (s *Service) PathForProfileWithID(id string) string {
|
||||
return s.RootPath + RELPATH_PROFILES + "/" + id
|
||||
return s.RootPath + RELPATH_PROFILES + "/" + id + ".json"
|
||||
}
|
||||
|
||||
func (s *Service) PathForPluginWithName(name string) string {
|
||||
return s.RootPath + RELPATH_PLUGINS + "/" + name
|
||||
return s.RootPath + RELPATH_PLUGINS + "/" + name + ".so"
|
||||
}
|
||||
|
||||
func (s *Service) PathForProfiles() string {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue