refactor: inject service client / podman in daemon
This commit is contained in:
parent
824ba8ba0b
commit
01df6c4dd5
7 changed files with 84 additions and 10 deletions
|
@ -10,7 +10,7 @@ func getDaemonCommand() *cobra.Command {
|
||||||
Use: "daemon",
|
Use: "daemon",
|
||||||
Short: "Starts a daemon instance.",
|
Short: "Starts a daemon instance.",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
d := daemon.NewDaemon("", 8000)
|
d := daemon.NewDaemon("", 8000, nil)
|
||||||
|
|
||||||
d.Start()
|
d.Start()
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
package daemon
|
package daemon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetApiRoutes() map[string]http.HandlerFunc {
|
func GetApiRoutes() map[string]HandlerFuncWithContext {
|
||||||
return map[string]http.HandlerFunc{
|
return map[string]HandlerFuncWithContext{
|
||||||
"/service": ServiceList,
|
"/service": ServiceList,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ServiceList(w http.ResponseWriter, r *http.Request) {
|
func ServiceList(w http.ResponseWriter, r *http.Request, c context.Context) {
|
||||||
var returnPayload []byte
|
var returnPayload []byte
|
||||||
var returnCode int
|
var returnCode int
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package daemon
|
package daemon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -12,7 +13,7 @@ func TestServiceListUnsupportedMethods(t *testing.T) {
|
||||||
req := httptest.NewRequest(method, "/service", nil)
|
req := httptest.NewRequest(method, "/service", nil)
|
||||||
resp := httptest.NewRecorder()
|
resp := httptest.NewRecorder()
|
||||||
|
|
||||||
ServiceList(resp, req)
|
ServiceList(resp, req, context.Background())
|
||||||
|
|
||||||
response := resp.Result()
|
response := resp.Result()
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,37 @@
|
||||||
package daemon
|
package daemon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
service "spud/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Daemon struct {
|
type Daemon struct {
|
||||||
Host string
|
Host string
|
||||||
Port int
|
Port int
|
||||||
|
Services service.ServiceClient
|
||||||
|
Routes map[string]http.HandlerFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDaemon(host string, port int) *Daemon {
|
type HandlerFuncWithContext = func(w http.ResponseWriter, r *http.Request, c context.Context)
|
||||||
return &Daemon{Host: host, Port: port}
|
|
||||||
|
func handleFuncWithContext(h HandlerFuncWithContext, c context.Context) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
h(w, r, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDaemon(host string, port int, serviceClient service.ServiceClient) *Daemon {
|
||||||
|
d := &Daemon{Host: host, Port: port}
|
||||||
|
|
||||||
|
if serviceClient == nil {
|
||||||
|
d.Services = &service.PodmanServiceClient{}
|
||||||
|
} else {
|
||||||
|
d.Services = serviceClient
|
||||||
|
}
|
||||||
|
|
||||||
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Daemon) GetListenAddress() string {
|
func (d Daemon) GetListenAddress() string {
|
||||||
|
@ -19,8 +39,9 @@ func (d Daemon) GetListenAddress() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Daemon) Start() {
|
func (d Daemon) Start() {
|
||||||
|
daemonContext := context.WithValue(context.Background(), "client", d.Services)
|
||||||
for route, handler := range GetApiRoutes() {
|
for route, handler := range GetApiRoutes() {
|
||||||
http.HandleFunc(route, handler)
|
http.HandleFunc(route, handleFuncWithContext(handler, daemonContext))
|
||||||
}
|
}
|
||||||
|
|
||||||
http.ListenAndServe(d.GetListenAddress(), nil)
|
http.ListenAndServe(d.GetListenAddress(), nil)
|
||||||
|
|
31
daemon/daemon_test.go
Normal file
31
daemon/daemon_test.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package daemon
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
service "spud/service"
|
||||||
|
service_definition "spud/service_definition"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DummyClient struct{}
|
||||||
|
|
||||||
|
func (c DummyClient) Create(d service_definition.ServiceDefinition) {}
|
||||||
|
func (c DummyClient) Stop(d string) {}
|
||||||
|
|
||||||
|
func TestDaemonDefaultsToPodmanClient(t *testing.T) {
|
||||||
|
d := NewDaemon("host", 0, nil)
|
||||||
|
|
||||||
|
clientType := reflect.TypeOf(d.Services).String()
|
||||||
|
if clientType != reflect.TypeOf(&service.PodmanServiceClient{}).String() {
|
||||||
|
t.Errorf("Expected podman client, got %s instead.", clientType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDaemonUsesInjectedClientIfProvided(t *testing.T) {
|
||||||
|
d := NewDaemon("host", 0, DummyClient{})
|
||||||
|
|
||||||
|
clientType := reflect.TypeOf(d.Services).String()
|
||||||
|
if clientType != reflect.TypeOf(DummyClient{}).String() {
|
||||||
|
t.Errorf("Expected dummy client, got %s instead.", clientType)
|
||||||
|
}
|
||||||
|
}
|
10
service/client.go
Normal file
10
service/client.go
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
service_definition "spud/service_definition"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ServiceClient interface {
|
||||||
|
Create(service_definition.ServiceDefinition)
|
||||||
|
Stop(string)
|
||||||
|
}
|
|
@ -7,6 +7,16 @@ import (
|
||||||
service_definition "spud/service_definition"
|
service_definition "spud/service_definition"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type PodmanServiceClient struct{}
|
||||||
|
|
||||||
|
func (c PodmanServiceClient) Create(definition service_definition.ServiceDefinition) {
|
||||||
|
CreateService(definition)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c PodmanServiceClient) Stop(name string) {
|
||||||
|
StopService(name)
|
||||||
|
}
|
||||||
|
|
||||||
func CreateService(definition service_definition.ServiceDefinition) {
|
func CreateService(definition service_definition.ServiceDefinition) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue