66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"g.fc.run/misc/byre/internal/workflow"
|
|
)
|
|
|
|
const maxWorkflowFileBytes = 1 << 20
|
|
|
|
func (s *Server) ListWorkflows(w http.ResponseWriter, r *http.Request) {
|
|
workflows, err := s.store.ListWorkflows(r.Context())
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, workflows)
|
|
}
|
|
|
|
func (s *Server) GetWorkflow(w http.ResponseWriter, r *http.Request, name string) {
|
|
file, err := s.agents.WorkflowFile(r.Context(), name)
|
|
if err != nil {
|
|
writeAgentError(w, err)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/yaml")
|
|
w.Header().Set("Content-Disposition", "attachment; filename*=UTF-8''"+url.PathEscape(workflow.Filename(name)))
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write(file)
|
|
}
|
|
|
|
func (s *Server) PutWorkflow(w http.ResponseWriter, r *http.Request, name string) {
|
|
file, err := io.ReadAll(http.MaxBytesReader(w, r.Body, maxWorkflowFileBytes))
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, fmt.Errorf("read workflow file: %w", err))
|
|
return
|
|
}
|
|
summary, err := s.agents.SaveWorkflow(r.Context(), name, file, actor(r))
|
|
if err != nil {
|
|
writeAgentError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, summary)
|
|
}
|
|
|
|
func (s *Server) DeleteWorkflow(w http.ResponseWriter, r *http.Request, name string) {
|
|
err := s.store.DeleteWorkflow(r.Context(), name, actor(r))
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) GetTaskWorkflow(w http.ResponseWriter, r *http.Request, taskID string) {
|
|
tw, err := s.agents.TaskWorkflow(r.Context(), taskID)
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, tw)
|
|
}
|