193 lines
4.4 KiB
Go
193 lines
4.4 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
type Profile struct {
|
|
ID string `json:"id"` // profile ID
|
|
Description string `json:"description"` // profile description
|
|
Tags []string `json:"tags"` // tags used for ...
|
|
Paths []string `json:"paths"` // paths to download
|
|
Plugins []string `json:"plugins"` // plugins to run
|
|
Data map[string]any `json:"data"` // include render data
|
|
}
|
|
|
|
func (s *Service) GetProfiles() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var (
|
|
path = s.RootPath + PROFILES_RELPATH
|
|
profiles []*Profile
|
|
contents []byte
|
|
err error
|
|
)
|
|
|
|
// walk profiles directory to load all profiles
|
|
err = filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// skip directories
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
// read file contents
|
|
var profile *Profile
|
|
profile, err = LoadProfile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
profiles = append(profiles, profile)
|
|
|
|
fmt.Println(path, info.Size())
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
|
|
// marshal and send all the profiles
|
|
contents, err = json.Marshal(profiles)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("failed to marshal profiles: %v", err), http.StatusInternalServerError)
|
|
}
|
|
|
|
_, err = w.Write(contents)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// func (s *Service) CreateProfiles() http.HandlerFunc {
|
|
// return func(w http.ResponseWriter, r *http.Request) {
|
|
// var (
|
|
// path = chi.URLParam(r, "path")
|
|
// err error
|
|
// )
|
|
|
|
// }
|
|
// }
|
|
|
|
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
|
|
contents []byte
|
|
err error
|
|
)
|
|
|
|
profile, err = LoadProfile(path)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
contents, err = json.Marshal(profile)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
_, err = w.Write(contents)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
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"`
|
|
}
|
|
var (
|
|
body, contents []byte
|
|
in input
|
|
err error
|
|
)
|
|
|
|
body, err = io.ReadAll(r.Body)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// use the request info to build profile
|
|
err = json.Unmarshal(body, &in)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// serialize just the profile part
|
|
contents, err = json.Marshal(in.profile)
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
// create a new profile on disk
|
|
err = os.WriteFile(in.path, contents, os.ModePerm)
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
}
|
|
|
|
func (s *Service) CreateProfileVar() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {}
|
|
}
|
|
|
|
func (s *Service) DeleteProfileVar() 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) {}
|
|
}
|
|
|
|
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) {}
|
|
}
|
|
|
|
func (s *Service) CreatePlugins() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {}
|
|
}
|
|
|
|
func (s *Service) DeletePlugins() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {}
|
|
}
|