2024-10-02 02:56:25 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2024-10-03 03:03:57 +00:00
|
|
|
"context"
|
2024-10-03 03:47:33 +00:00
|
|
|
"encoding/json"
|
2024-10-02 02:56:25 +00:00
|
|
|
"net/http"
|
2024-10-03 03:47:33 +00:00
|
|
|
service "spud/service"
|
|
|
|
service_definition "spud/service_definition"
|
2024-10-02 02:56:25 +00:00
|
|
|
)
|
|
|
|
|
2024-10-03 03:03:57 +00:00
|
|
|
func GetApiRoutes() map[string]HandlerFuncWithContext {
|
|
|
|
return map[string]HandlerFuncWithContext{
|
2024-10-03 03:47:33 +00:00
|
|
|
"/service/": ServiceList,
|
|
|
|
"/service/{serviceName}/": ServiceDetails,
|
2024-10-02 02:56:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 03:47:33 +00:00
|
|
|
type ServiceListPayload struct {
|
|
|
|
Definition service_definition.ServiceDefinition `json:"definition"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleServiceListPost(w http.ResponseWriter, r *http.Request, c context.Context) {
|
|
|
|
client := c.Value("client").(service.ServiceClient)
|
|
|
|
var p ServiceListPayload
|
|
|
|
json.NewDecoder(r.Body).Decode(&p)
|
|
|
|
client.Create(p.Definition)
|
|
|
|
w.WriteHeader(201)
|
|
|
|
json.NewEncoder(w).Encode(p.Definition)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleServiceDetailDelete(w http.ResponseWriter, r *http.Request, c context.Context) {
|
|
|
|
client := c.Value("client").(service.ServiceClient)
|
|
|
|
serviceName := r.PathValue("serviceName")
|
|
|
|
client.Stop(serviceName)
|
|
|
|
|
|
|
|
w.WriteHeader(204)
|
|
|
|
}
|
2024-10-02 02:56:25 +00:00
|
|
|
|
2024-10-03 03:47:33 +00:00
|
|
|
func handleNotImplemented(w http.ResponseWriter) {
|
|
|
|
w.WriteHeader(501)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ServiceList(w http.ResponseWriter, r *http.Request, c context.Context) {
|
2024-10-02 02:56:25 +00:00
|
|
|
switch r.Method {
|
2024-10-03 03:47:33 +00:00
|
|
|
case http.MethodPost:
|
|
|
|
handleServiceListPost(w, r, c)
|
2024-10-02 02:56:25 +00:00
|
|
|
default:
|
2024-10-03 03:47:33 +00:00
|
|
|
handleNotImplemented(w)
|
2024-10-02 02:56:25 +00:00
|
|
|
}
|
2024-10-03 03:47:33 +00:00
|
|
|
}
|
2024-10-02 02:56:25 +00:00
|
|
|
|
2024-10-03 03:47:33 +00:00
|
|
|
func ServiceDetails(w http.ResponseWriter, r *http.Request, c context.Context) {
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodDelete:
|
|
|
|
handleServiceDetailDelete(w, r, c)
|
|
|
|
default:
|
|
|
|
handleNotImplemented(w)
|
|
|
|
}
|
2024-10-02 02:56:25 +00:00
|
|
|
}
|