From a1a9c6407f3af0bbe7f089a8f249d785ade06691 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Thu, 14 Aug 2025 07:40:30 -0600 Subject: [PATCH] refactor: added more implementation details --- pkg/client/client.go | 27 +++++++++++++++++++++++++++ pkg/plugin.go | 6 ++++-- pkg/plugins/jinja2.go | 19 +++++++++++++++++++ pkg/plugins/smd.go | 1 + pkg/service/routes.go | 32 ++++++++++++++++++++++++++++++++ pkg/service/service.go | 5 +++-- 6 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 pkg/client/client.go create mode 100644 pkg/plugins/jinja2.go create mode 100644 pkg/plugins/smd.go diff --git a/pkg/client/client.go b/pkg/client/client.go new file mode 100644 index 0000000..6832193 --- /dev/null +++ b/pkg/client/client.go @@ -0,0 +1,27 @@ +package client + +import "net/http" + +type HTTPBody []byte +type HTTPHeader map[string]string +type HTTPEnvelope struct { + Path string + Method string + Header HTTPHeader + Body HTTPBody + CACert string +} + +type Client struct { + BaseURI string +} + +func New(uri string) Client { + return Client{ + BaseURI: uri, + } +} + +func (c *Client) MakeRequest(env HTTPEnvelope) (*http.Response, []byte, error) { + http.DefaultTransport.(*http.Transport) +} diff --git a/pkg/plugin.go b/pkg/plugin.go index 602cfce..4ad4544 100644 --- a/pkg/plugin.go +++ b/pkg/plugin.go @@ -1,14 +1,16 @@ package configurator type Plugin interface { - // plugin data Name() string Version() string Description() string Metadata() map[string]string - // run the plugin Init() error Run() error Cleanup() error } + +func RunPlugin() { + +} diff --git a/pkg/plugins/jinja2.go b/pkg/plugins/jinja2.go new file mode 100644 index 0000000..9cb515d --- /dev/null +++ b/pkg/plugins/jinja2.go @@ -0,0 +1,19 @@ +package plugin + +type Jinja2 struct{} + +func Name() string { return "jinja2" } +func Version() string { return "test" } +func Description() string { return "Renders Jinja 2 templates" } +func Metadata() map[string]string { + return map[string]string{ + "author.name": "David J. Allen", + "author.email": "davidallendj@gmail.com", + } +} + +func Init() { + // initialize Jinja2 (gonja) +} +func Run() {} +func Cleanup() {} diff --git a/pkg/plugins/smd.go b/pkg/plugins/smd.go new file mode 100644 index 0000000..b0736c3 --- /dev/null +++ b/pkg/plugins/smd.go @@ -0,0 +1 @@ +package plugin diff --git a/pkg/service/routes.go b/pkg/service/routes.go index f0e55ab..21781df 100644 --- a/pkg/service/routes.go +++ b/pkg/service/routes.go @@ -4,10 +4,42 @@ import ( "encoding/json" "fmt" "net/http" + "os" + "path/filepath" + "strings" + + "git.towk2.me/towk/configurator/pkg/util" ) func (s *Service) Download() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + var ( + path = strings.TrimPrefix(r.URL.Path, "/download") + fileInfo os.FileInfo + out *os.File + err error + ) + + fmt.Printf("download path: %v\n", path) + + // determine if path is directory, file, or exists + if fileInfo, err = os.Stat(filepath.Clean(path)); err != nil { + if fileInfo.IsDir() { + // recursively walk dir acompressednd get all filenames + // download directory as archive + out, err = os.Create(fmt.Sprintf("%s.tar", path)) + if err != nil { + + } + err = util.CreateArchive([]string{path}, out) + if err != nil { + + } + + } else { + // download individual file + } + } } } diff --git a/pkg/service/service.go b/pkg/service/service.go index 9a1545a..50483af 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -3,6 +3,7 @@ package service import ( "encoding/json" "fmt" + "net/http" "os" "time" @@ -53,7 +54,7 @@ func (s *Service) Serve() error { } else { // general - router.Get("/download", s.Download()) + router.Get("/download/*", s.Download()) router.Post("/upload", s.Upload()) router.Get("/list", s.List()) @@ -77,7 +78,7 @@ func (s *Service) Serve() error { // always available public routes go here router.HandleFunc("/status", s.GetStatus) - return nil + return http.ListenAndServe(":8080", router) } func (s *Service) requireAuth() bool {