refactor: updated cmd and pkg implementations
This commit is contained in:
parent
d88ab2c01f
commit
fbed466c3d
10 changed files with 287 additions and 196 deletions
|
|
@ -1,12 +1,16 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.towk2.me/towk/makeshift/pkg/util"
|
||||
"github.com/cavaliergopher/grab/v3"
|
||||
|
|
@ -107,6 +111,43 @@ func (c *Client) UploadMultipartFile(uri, key, path string) (*http.Response, err
|
|||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) LoadCertificateFromPath(path string) error {
|
||||
cacert, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read certificate at path: %s", path)
|
||||
}
|
||||
certPool := x509.NewCertPool()
|
||||
certPool.AppendCertsFromPEM(cacert)
|
||||
err = c.LoadCertificateFromPool(certPool)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not initialize certificate from pool: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) LoadCertificateFromPool(certPool *x509.CertPool) error {
|
||||
// make sure we have a valid cert pool
|
||||
if certPool == nil {
|
||||
return fmt.Errorf("invalid cert pool")
|
||||
}
|
||||
|
||||
// make sure that we can access the internal client
|
||||
c.Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
RootCAs: certPool,
|
||||
InsecureSkipVerify: false,
|
||||
},
|
||||
DisableKeepAlives: true,
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 120 * time.Second,
|
||||
KeepAlive: 120 * time.Second,
|
||||
}).Dial,
|
||||
TLSHandshakeTimeout: 120 * time.Second,
|
||||
ResponseHeaderTimeout: 120 * time.Second,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mustOpen(f string) *os.File {
|
||||
r, err := os.Open(f)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 (
|
||||
|
|
|
|||
|
|
@ -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{
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue