feat: daemon command stub
This commit is contained in:
parent
da0b3dd240
commit
824ba8ba0b
5 changed files with 103 additions and 0 deletions
|
@ -20,6 +20,8 @@ func GetCli() *cobra.Command {
|
|||
getStopCommand(),
|
||||
// cli/build.go
|
||||
getBuildCommand(),
|
||||
// cli/daemon.go
|
||||
getDaemonCommand(),
|
||||
}
|
||||
|
||||
for _, command := range allCommands {
|
||||
|
|
21
cli/daemon.go
Normal file
21
cli/daemon.go
Normal file
|
@ -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
|
||||
}
|
28
daemon/api.go
Normal file
28
daemon/api.go
Normal file
|
@ -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)
|
||||
}
|
25
daemon/api_test.go
Normal file
25
daemon/api_test.go
Normal file
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
27
daemon/daemon.go
Normal file
27
daemon/daemon.go
Normal file
|
@ -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)
|
||||
}
|
Loading…
Reference in a new issue