refactor: updated cmd and pkg implementations

This commit is contained in:
David Allen 2025-08-30 23:30:46 -06:00
parent d88ab2c01f
commit fbed466c3d
Signed by: towk
GPG key ID: 0430CDBE22619155
10 changed files with 287 additions and 196 deletions

View file

@ -8,6 +8,7 @@ import (
makeshift "git.towk2.me/towk/makeshift/pkg"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
)
func (s *Service) ListPlugins() http.HandlerFunc {
@ -124,24 +125,17 @@ func (s *Service) CreatePlugin() http.HandlerFunc {
func (s *Service) DeletePlugin() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
path string
plugin makeshift.Plugin
err error
pluginName = chi.URLParam(r, "name")
path = s.PathForPluginWithName(pluginName)
err error
)
plugin, err = getPluginFromRequestBody(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
path = s.PathForPluginWithName(plugin.Name())
log.Debug().Str("path", path).Send()
err = os.Remove(path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
}

View file

@ -88,13 +88,10 @@ func (s *Service) GetProfile() http.HandlerFunc {
func (s *Service) CreateProfile() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type input struct {
Path string `json:"path"`
Profile *makeshift.Profile `json:"profile"`
}
var (
body, contents []byte
in input
path string
profile *makeshift.Profile
err error
)
@ -105,23 +102,24 @@ func (s *Service) CreateProfile() http.HandlerFunc {
}
// use the request info to build profile
err = json.Unmarshal(body, &in)
err = json.Unmarshal(body, &profile)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, fmt.Sprintf("failed to unmarshal profile: %v", err.Error()), http.StatusBadRequest)
return
}
// serialize just the profile part
contents, err = json.Marshal(in.Profile)
contents, err = json.Marshal(profile)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, fmt.Sprintf("failed to marshal profile: %v", err.Error()), http.StatusBadRequest)
return
}
// create a new profile on disk
err = os.WriteFile(in.Path, contents, os.ModePerm)
path = s.PathForProfileWithID(profile.ID)
err = os.WriteFile(path, contents, os.ModePerm)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@ -129,6 +127,29 @@ func (s *Service) CreateProfile() http.HandlerFunc {
}
}
func (s *Service) DeleteProfile() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
profileID = chi.URLParam(r, "id")
path string
err error
)
if profileID == "default" {
http.Error(w, "cannot delete the default profile", http.StatusBadRequest)
return
}
path = s.PathForProfileWithID(profileID)
err = os.Remove(path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
}
func (s *Service) SetProfileData() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (

View file

@ -181,18 +181,9 @@ func (s *Service) Download() http.HandlerFunc {
func (s *Service) Upload() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
}
}
func (s *Service) UploadPlugin() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
}
}
func (s *Service) UploadProfile() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
_ = s.PathForData() + strings.TrimPrefix(r.URL.Path, "/upload")
)
}
}
@ -239,6 +230,23 @@ func (s *Service) List() http.HandlerFunc {
}
}
func (s *Service) Delete() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
path = s.PathForData() + strings.TrimPrefix(r.URL.Path, "/delete")
err error
)
err = os.RemoveAll(path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
}
func (s *Service) GetStatus(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(map[string]any{

View file

@ -98,15 +98,15 @@ func (s *Service) Serve() error {
} else {
// general
router.Get("/download/*", s.Download())
router.Post("/upload/", s.Upload())
router.Post("/upload/plugin", s.UploadPlugin())
router.Post("/upload/profile", s.UploadProfile())
router.Post("/upload/*", s.Upload())
router.Get("/list/*", s.List())
router.Delete("/delete/*", s.Delete())
// profiles
router.Get("/profiles", s.ListProfiles())
router.Get("/profiles/{id}", s.GetProfile())
router.Post("/profiles/{id}", s.CreateProfile())
router.Delete("/profiles/{id}", s.DeleteProfile())
router.Get("/profiles/{id}/data", s.GetProfileData())
router.Post("/profiles/{id}/data", s.SetProfileData())
router.Delete("/profiles/{id}/data", s.DeleteProfileData())