From 4d960101995266499186e03a8b5ce268f8611fec Mon Sep 17 00:00:00 2001 From: David Allen Date: Fri, 29 Aug 2025 23:52:43 -0600 Subject: [PATCH] refactor: updated routes and handlers --- pkg/models.go | 14 +++++++++++++- pkg/service/plugins.go | 12 ++++++++++-- pkg/service/service.go | 25 ++++++++++--------------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/pkg/models.go b/pkg/models.go index b17b6b2..1b45444 100644 --- a/pkg/models.go +++ b/pkg/models.go @@ -1,6 +1,8 @@ package makeshift -import "git.towk2.me/towk/makeshift/pkg/storage" +import ( + "git.towk2.me/towk/makeshift/pkg/storage" +) type ProfileMap map[string]*Profile type Profile struct { @@ -30,3 +32,13 @@ type Hook struct { func (h *Hook) Run() error { return h.Plugin.Run(h.Data, h.Args) } + +func PluginToMap(p Plugin) map[string]any { + return map[string]any{ + "name": p.Name(), + "version": p.Version(), + "description": p.Description(), + "metadata": p.Metadata(), + } + +} diff --git a/pkg/service/plugins.go b/pkg/service/plugins.go index 1575f39..0e1efc5 100644 --- a/pkg/service/plugins.go +++ b/pkg/service/plugins.go @@ -2,6 +2,7 @@ package service import ( "encoding/json" + "fmt" "io" "net/http" "os" @@ -34,12 +35,13 @@ func (s *Service) ListPlugins() http.HandlerFunc { http.Error(w, err.Error(), http.StatusInternalServerError) return } + fmt.Printf("%v", plugins) w.Write(body) } } -func (s *Service) GetPlugin() http.HandlerFunc { +func (s *Service) GetPluginInfo() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var ( pluginName = chi.URLParam(r, "name") @@ -55,7 +57,7 @@ func (s *Service) GetPlugin() http.HandlerFunc { return } - body, err = json.Marshal(plugin) + body, err = json.Marshal(makeshift.PluginToMap(plugin)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -65,6 +67,12 @@ func (s *Service) GetPlugin() http.HandlerFunc { } } +func (s *Service) GetPluginRaw() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + } +} + func (s *Service) CreatePlugin() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var ( diff --git a/pkg/service/service.go b/pkg/service/service.go index c69d68f..56e4cae 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -97,7 +97,6 @@ func (s *Service) Serve() error { } else { // general - // router.Handle("/download/*", http.StripPrefix("/download/", http.FileServer(http.Dir(s.PathForData())))) router.Get("/download/*", s.Download()) router.Post("/upload/", s.Upload()) router.Post("/upload/plugin", s.UploadPlugin()) @@ -106,18 +105,18 @@ func (s *Service) Serve() error { // profiles router.Get("/profiles", s.ListProfiles()) - // router.Post("/profiles", s.CreateProfiles()) - router.Get("/profile/{id}", s.GetProfile()) - router.Post("/profile/{id}", s.CreateProfile()) - router.Get("/profile/{id}/data", s.GetProfileData()) - router.Post("/profile/{id}/data", s.SetProfileData()) - router.Delete("/profile/{id}/data", s.DeleteProfileData()) + router.Get("/profiles/{id}", s.GetProfile()) + router.Post("/profiles/{id}", s.CreateProfile()) + router.Get("/profiles/{id}/data", s.GetProfileData()) + router.Post("/profiles/{id}/data", s.SetProfileData()) + router.Delete("/profiles/{id}/data", s.DeleteProfileData()) // plugins router.Get("/plugins", s.ListPlugins()) - router.Get("/plugin/{name}", s.GetPlugin()) - router.Post("/plugin/{name}", s.CreatePlugin()) - router.Delete("/plugin/{name}", s.DeletePlugin()) + router.Get("/plugins/{name}/info", s.GetPluginInfo()) + router.Get("/plugins/{name}/raw", s.GetPluginRaw()) + router.Post("/plugins/{name}", s.CreatePlugin()) + router.Delete("/plugins/{name}", s.DeletePlugin()) } // always available public routes go here @@ -192,12 +191,8 @@ func LoadPluginsFromDir(dirpath string) (map[string]makeshift.Plugin, error) { // 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) { + if info.IsDir() || err != nil || hasValidExt(path) { return nil }