refactor: more implementation to refactor and deleted files

This commit is contained in:
David Allen 2025-08-04 22:32:41 -06:00
parent ba684bd149
commit 50e6b53091
Signed by: towk
GPG key ID: 0430CDBE22619155
17 changed files with 84 additions and 945 deletions

View file

@ -10,6 +10,7 @@ import (
"path/filepath"
"github.com/go-chi/chi/v5"
"github.com/tidwall/sjson"
)
type Profile struct {
@ -43,7 +44,7 @@ func (s *Service) GetProfiles() http.HandlerFunc {
// read file contents
var profile *Profile
profile, err = LoadProfile(path)
profile, err = LoadProfileFromFile(path)
if err != nil {
return err
}
@ -91,7 +92,7 @@ func (s *Service) GetProfile() http.HandlerFunc {
err error
)
profile, err = LoadProfile(path)
profile, err = LoadProfileFromFile(path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
@ -114,8 +115,8 @@ 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 *Profile `json:"profile"`
Path string `json:"path"`
Profile *Profile `json:"profile"`
}
var (
body, contents []byte
@ -137,37 +138,85 @@ func (s *Service) CreateProfile() http.HandlerFunc {
}
// serialize just the profile part
contents, err = json.Marshal(in.profile)
contents, err = json.Marshal(in.Profile)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// create a new profile on disk
err = os.WriteFile(in.path, contents, os.ModePerm)
err = os.WriteFile(in.Path, contents, os.ModePerm)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}
}
func (s *Service) CreateProfileVar() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {}
func (s *Service) SetProfileData() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
body, contents []byte
newContents string
profile *Profile
path string
err error
)
body, err = io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = json.Unmarshal(body, &profile)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// make sure the request data sets an ID
if profile.ID == "" {
http.Error(w, "ID must be set to a non-empty value", http.StatusBadRequest)
return
}
// read the contents the file with profile ID
path = s.BuildProfilePath(profile.ID)
contents, err = os.ReadFile(path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// modify the data of the profile's contents
newContents, err = sjson.Set(string(contents), "data", profile.Data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// write only the data to the file with ID
err = os.WriteFile(path, []byte(newContents), os.ModePerm)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func (s *Service) DeleteProfileVar() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {}
func (s *Service) DeleteProfileData() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
}
}
func (s *Service) GetProfileData() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {}
}
func (s *Service) GetProfileVar() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {}
}
func (s *Service) CreateProfilePath() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {}
}

View file

@ -62,10 +62,9 @@ func (s *Service) Serve() error {
// router.Post("/profiles", s.CreateProfiles())
router.Get("/profile/{id}", s.GetProfile())
router.Post("/profile/{id}", s.CreateProfile())
router.Post("/profile/{id}/data/{varname}", s.CreateProfileVar())
router.Delete("/profile/{id}/data/{varname}", s.DeleteProfileVar())
router.Get("/profile/{id}/data", s.GetProfileData())
router.Get("/profile/{id}/data/{varname}", s.GetProfileVar())
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())
@ -89,11 +88,11 @@ func (s *Service) FetchJwks(uri string) {
}
func LoadProfile(path string) (*Profile, error) {
func LoadProfileFromFile(path string) (*Profile, error) {
return LoadFromJSONFile[Profile](path)
}
func LoadPlugin(path string) (*configurator.Plugin, error) {
func LoadPluginFromFile(path string) (*configurator.Plugin, error) {
return LoadFromJSONFile[configurator.Plugin](path)
}