2024-10-04 04:03:12 +00:00
|
|
|
package webclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
daemon "spud/daemon"
|
|
|
|
service_definition "spud/service_definition"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HttpClient interface {
|
2024-10-05 00:28:09 +00:00
|
|
|
Do(*http.Request) (*http.Response, error)
|
2024-10-04 04:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type WebClient struct {
|
|
|
|
httpClient HttpClient
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWebClient(host string, port int) *WebClient {
|
|
|
|
return &WebClient{
|
|
|
|
httpClient: &http.Client{},
|
|
|
|
Host: host,
|
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c WebClient) getBaseUrl() string {
|
|
|
|
return fmt.Sprintf("%s:%d", c.Host, c.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c WebClient) CreateService(def service_definition.ServiceDefinition) error {
|
|
|
|
payload := daemon.ServiceListPayload{
|
|
|
|
Definition: def,
|
|
|
|
}
|
|
|
|
|
|
|
|
serializedPayload, _ := json.Marshal(payload)
|
|
|
|
|
2024-10-05 00:31:38 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, c.getBaseUrl()+"/service/", bytes.NewBuffer(serializedPayload))
|
|
|
|
_, e := c.httpClient.Do(req)
|
2024-10-04 04:03:12 +00:00
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
2024-10-05 00:28:09 +00:00
|
|
|
|
|
|
|
func (c WebClient) StopService(name string) error {
|
|
|
|
req, _ := http.NewRequest(http.MethodDelete, c.getBaseUrl()+"/service/"+name+"/", nil)
|
|
|
|
_, e := c.httpClient.Do(req)
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|