From 824ba8ba0b9e93b4a71bf9e9aa31f4d22f067a7d Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Tue, 1 Oct 2024 22:56:25 -0400 Subject: [PATCH] feat: daemon command stub --- cli/cli.go | 2 ++ cli/daemon.go | 21 +++++++++++++++++++++ daemon/api.go | 28 ++++++++++++++++++++++++++++ daemon/api_test.go | 25 +++++++++++++++++++++++++ daemon/daemon.go | 27 +++++++++++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 cli/daemon.go create mode 100644 daemon/api.go create mode 100644 daemon/api_test.go create mode 100644 daemon/daemon.go diff --git a/cli/cli.go b/cli/cli.go index bef0816..02b39ae 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -20,6 +20,8 @@ func GetCli() *cobra.Command { getStopCommand(), // cli/build.go getBuildCommand(), + // cli/daemon.go + getDaemonCommand(), } for _, command := range allCommands { diff --git a/cli/daemon.go b/cli/daemon.go new file mode 100644 index 0000000..7bfa5d0 --- /dev/null +++ b/cli/daemon.go @@ -0,0 +1,21 @@ +package cli + +import ( + "github.com/spf13/cobra" + daemon "spud/daemon" +) + +func getDaemonCommand() *cobra.Command { + startDaemon := &cobra.Command{ + Use: "daemon", + Short: "Starts a daemon instance.", + RunE: func(cmd *cobra.Command, args []string) error { + d := daemon.NewDaemon("", 8000) + + d.Start() + return nil + }, + } + + return startDaemon +} diff --git a/daemon/api.go b/daemon/api.go new file mode 100644 index 0000000..0f594d0 --- /dev/null +++ b/daemon/api.go @@ -0,0 +1,28 @@ +package daemon + +import ( + "net/http" +) + +func GetApiRoutes() map[string]http.HandlerFunc { + return map[string]http.HandlerFunc{ + "/service": ServiceList, + } +} + +func ServiceList(w http.ResponseWriter, r *http.Request) { + var returnPayload []byte + var returnCode int + + switch r.Method { + case "POST": + returnPayload = []byte("Create service: Not implemented") + returnCode = 501 + default: + returnPayload = []byte("Not implemented") + returnCode = 501 + } + + w.WriteHeader(returnCode) + w.Write(returnPayload) +} diff --git a/daemon/api_test.go b/daemon/api_test.go new file mode 100644 index 0000000..62e54d4 --- /dev/null +++ b/daemon/api_test.go @@ -0,0 +1,25 @@ +package daemon + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestServiceListUnsupportedMethods(t *testing.T) { + for _, method := range []string{http.MethodGet, http.MethodPut, http.MethodHead} { + t.Run(method, func(t *testing.T) { + req := httptest.NewRequest(method, "/service", nil) + resp := httptest.NewRecorder() + + ServiceList(resp, req) + + response := resp.Result() + + if response.StatusCode != 501 { + t.Errorf("Expected status 501, got %d.", response.StatusCode) + } + }) + } + +} diff --git a/daemon/daemon.go b/daemon/daemon.go new file mode 100644 index 0000000..1c36579 --- /dev/null +++ b/daemon/daemon.go @@ -0,0 +1,27 @@ +package daemon + +import ( + "fmt" + "net/http" +) + +type Daemon struct { + Host string + Port int +} + +func NewDaemon(host string, port int) *Daemon { + return &Daemon{Host: host, Port: port} +} + +func (d Daemon) GetListenAddress() string { + return fmt.Sprintf("%s:%d", d.Host, d.Port) +} + +func (d Daemon) Start() { + for route, handler := range GetApiRoutes() { + http.HandleFunc(route, handler) + } + + http.ListenAndServe(d.GetListenAddress(), nil) +}