spud/daemon/api.go

60 lines
1.4 KiB
Go
Raw Normal View History

2024-10-02 02:56:25 +00:00
package daemon
import (
"context"
"encoding/json"
2024-10-02 02:56:25 +00:00
"net/http"
service "spud/service"
service_definition "spud/service_definition"
2024-10-02 02:56:25 +00:00
)
func GetApiRoutes() map[string]HandlerFuncWithContext {
return map[string]HandlerFuncWithContext{
"/service/": ServiceList,
"/service/{serviceName}/": ServiceDetails,
2024-10-02 02:56:25 +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
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 {
case http.MethodPost:
handleServiceListPost(w, r, c)
2024-10-02 02:56:25 +00:00
default:
handleNotImplemented(w)
2024-10-02 02:56:25 +00:00
}
}
2024-10-02 02:56:25 +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
}