complete item 1 task substrate

This commit is contained in:
kami
2026-07-26 18:56:59 +04:00
parent 5ab1b8fdb9
commit 24ee81d538
9 changed files with 1187 additions and 0 deletions
+118
View File
@@ -0,0 +1,118 @@
package main
import (
"encoding/json"
"log"
"net/http"
"orchestra/internal/domain"
"orchestra/internal/store"
"os"
"strconv"
"strings"
"time"
)
func id() string { return domain.NewID() }
func main() {
dir := os.Getenv("ORCHESTRA_DATA")
if dir == "" {
dir = "./data"
}
s, err := store.Open(dir)
if err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
mux.HandleFunc("/v1/tasks", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
json.NewEncoder(w).Encode(s.Tasks())
return
}
if r.Method != "POST" {
http.Error(w, "method not allowed", 405)
return
}
var p map[string]any
if json.NewDecoder(r.Body).Decode(&p) != nil {
http.Error(w, "invalid json", 400)
return
}
b, _ := json.Marshal(p)
e := domain.Event{ID: id(), Type: "TaskCreated", TaskID: id(), Version: 1, Payload: b}
if err := s.Append(e); err != nil {
http.Error(w, err.Error(), 400)
return
}
e = s.Events(0)[len(s.Events(0))-1]
w.WriteHeader(201)
json.NewEncoder(w).Encode(e)
})
mux.HandleFunc("/v1/events", func(w http.ResponseWriter, r *http.Request) {
var n uint64
if x, err := strconv.ParseUint(r.URL.Query().Get("since"), 10, 64); err == nil {
n = x
}
json.NewEncoder(w).Encode(s.Events(n))
})
mux.HandleFunc("/v1/tasks/", func(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if len(parts) != 4 || r.Method != "POST" {
http.Error(w, "not found", http.StatusNotFound)
return
}
taskID, action := parts[2], parts[3]
var e domain.Event
var err error
switch action {
case "lease":
var p struct {
HarnessID string `json:"harness_id"`
TTLSeconds int `json:"ttl_seconds"`
}
if json.NewDecoder(r.Body).Decode(&p) != nil || p.HarnessID == "" {
http.Error(w, "harness_id required", 400)
return
}
if p.TTLSeconds <= 0 {
p.TTLSeconds = 1800
}
e, err = s.Lease(taskID, p.HarnessID, time.Duration(p.TTLSeconds)*time.Second)
case "release", "complete", "block":
t, ok := s.Task(taskID)
if !ok {
http.Error(w, "task not found", 404)
return
}
types := map[string]string{"release": "TaskReleased", "complete": "TaskCompleted", "block": "TaskBlocked"}
e = domain.Event{ID: id(), Type: types[action], TaskID: taskID, Version: t.Version + 1, Payload: json.RawMessage(`{"source":"api"}`)}
err = s.Append(e)
default:
http.Error(w, "unknown action", 404)
return
}
if err != nil {
http.Error(w, err.Error(), 409)
return
}
json.NewEncoder(w).Encode(e)
})
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("ok\n")) })
port := os.Getenv("ORCHESTRA_PORT")
if port == "" {
port = "9145"
}
log.Println("orchestra listening on :" + port)
log.Fatal(http.ListenAndServe(":"+port, auth(mux)))
}
func auth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
surface := strings.ToLower(r.Header.Get("X-Orchestra-Surface"))
if surface == "telegram" || surface == "ntfy" {
if r.Method != "GET" {
http.Error(w, "notify-only surface", 403)
return
}
}
next.ServeHTTP(w, r)
})
}