refactor: more implementation to refactor
This commit is contained in:
parent
50e6b53091
commit
72be62c78e
2 changed files with 229 additions and 31 deletions
|
|
@ -9,6 +9,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
|
||||
configurator "git.towk2.me/towk/configurator/pkg"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
|
@ -86,22 +87,14 @@ func (s *Service) GetProfile() http.HandlerFunc {
|
|||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
id = chi.URLParam(r, "id")
|
||||
path = s.BuildProfilePath(id)
|
||||
profile *Profile
|
||||
path = s.PathForProfileWithID(id)
|
||||
contents []byte
|
||||
err error
|
||||
)
|
||||
|
||||
profile, err = LoadProfileFromFile(path)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
contents, err = json.Marshal(profile)
|
||||
contents, err = loadProfileContents(path)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = w.Write(contents)
|
||||
|
|
@ -184,7 +177,7 @@ func (s *Service) SetProfileData() http.HandlerFunc {
|
|||
}
|
||||
|
||||
// read the contents the file with profile ID
|
||||
path = s.BuildProfilePath(profile.ID)
|
||||
path = s.PathForProfileWithID(profile.ID)
|
||||
contents, err = os.ReadFile(path)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
|
@ -209,28 +202,104 @@ func (s *Service) SetProfileData() http.HandlerFunc {
|
|||
|
||||
func (s *Service) DeleteProfileData() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
id = chi.URLParam(r, "id")
|
||||
path = s.PathForProfileWithID(id)
|
||||
profile *Profile
|
||||
err error
|
||||
)
|
||||
|
||||
// get the profile
|
||||
profile, err = LoadProfileFromFile(path)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
|
||||
// delete the profile data
|
||||
profile.Data = map[string]any{}
|
||||
|
||||
// save the profile back to the file to update
|
||||
SaveProfileToFile(path, profile)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) GetProfileData() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {}
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
id = chi.URLParam(r, "id")
|
||||
path = s.PathForProfileWithID(id)
|
||||
profile *Profile
|
||||
body []byte
|
||||
err error
|
||||
)
|
||||
|
||||
profile, err = LoadProfileFromFile(path)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// only marshal the profile data and not entire profile
|
||||
body, err = json.Marshal(profile.Data)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// write body to response
|
||||
_, err = w.Write(body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) CreateProfilePath() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {}
|
||||
}
|
||||
// func (s *Service) CreateProfilePath() http.HandlerFunc {
|
||||
// return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Service) DeleteProfilePath() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {}
|
||||
}
|
||||
// w.WriteHeader(http.StatusOK)
|
||||
// }
|
||||
// }
|
||||
|
||||
func (s *Service) GetProfilePath() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {}
|
||||
}
|
||||
// func (s *Service) DeleteProfilePath() http.HandlerFunc {
|
||||
// return func(w http.ResponseWriter, r *http.Request) {}
|
||||
// }
|
||||
|
||||
// func (s *Service) GetProfilePath() http.HandlerFunc {
|
||||
// return func(w http.ResponseWriter, r *http.Request) {}
|
||||
// }
|
||||
|
||||
func (s *Service) GetPlugins() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {}
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
plugins map[string]configurator.Plugin
|
||||
names []string
|
||||
body []byte
|
||||
err error
|
||||
)
|
||||
|
||||
plugins, err = LoadPluginsFromDir(s.PathForPlugins())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
for name := range plugins {
|
||||
names = append(names, name)
|
||||
}
|
||||
|
||||
body, err = json.Marshal(names)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(body)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) CreatePlugins() http.HandlerFunc {
|
||||
|
|
@ -240,3 +309,21 @@ func (s *Service) CreatePlugins() http.HandlerFunc {
|
|||
func (s *Service) DeletePlugins() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {}
|
||||
}
|
||||
|
||||
func loadProfileContents(path string) ([]byte, error) {
|
||||
var (
|
||||
contents []byte
|
||||
profile *Profile
|
||||
err error
|
||||
)
|
||||
profile, err = LoadProfileFromFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load profile from file: %v", err)
|
||||
}
|
||||
|
||||
contents, err = json.Marshal(profile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal profile: %v", err)
|
||||
}
|
||||
return contents, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue