summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorChia <Chia@93.nz>2026-08-04 19:58:52 +1200
committerChia <Chia@93.nz>2026-08-04 20:43:23 +1200
commit5b651488b081b65fda8a323f228e139adb79a35d (patch)
tree08baf40efb8fe103b32721cd991ff712323e3173 /cmd
Build AI gateway control plane and admin UI
Diffstat (limited to '')
-rw-r--r--cmd/migrate/main.go28
-rw-r--r--cmd/mockupstream/main.go65
2 files changed, 93 insertions, 0 deletions
diff --git a/cmd/migrate/main.go b/cmd/migrate/main.go
new file mode 100644
index 0000000..20452f9
--- /dev/null
+++ b/cmd/migrate/main.go
@@ -0,0 +1,28 @@
+package main
+
+import (
+ "context"
+ "flag"
+ "fmt"
+ "os"
+ "time"
+
+ "aigw/internal/controlplane"
+)
+
+func main() {
+ environment := flag.String("database-url-env", "AIGW_DATABASE_URL", "environment variable containing the PostgreSQL URL")
+ flag.Parse()
+ databaseURL := os.Getenv(*environment)
+ if databaseURL == "" {
+ fmt.Fprintf(os.Stderr, "%s is empty\n", *environment)
+ os.Exit(1)
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ defer cancel()
+ if err := controlplane.MigrateDatabase(ctx, databaseURL); err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(1)
+ }
+ fmt.Println("control-plane schema is up to date")
+}
diff --git a/cmd/mockupstream/main.go b/cmd/mockupstream/main.go
new file mode 100644
index 0000000..cf695bb
--- /dev/null
+++ b/cmd/mockupstream/main.go
@@ -0,0 +1,65 @@
+// Command mockupstream is a local-only upstream used for manual gateway checks.
+package main
+
+import (
+ "encoding/json"
+ "flag"
+ "fmt"
+ "log"
+ "net/http"
+ "time"
+)
+
+func main() {
+ address := flag.String("address", "127.0.0.1:18080", "listen address")
+ flag.Parse()
+ mux := http.NewServeMux()
+ mux.HandleFunc("POST /v1/chat/completions", openAI)
+ mux.HandleFunc("POST /v1/messages", anthropic)
+ server := &http.Server{Addr: *address, Handler: mux, ReadHeaderTimeout: 5 * time.Second}
+ log.Printf("mock upstream listening on http://%s", *address)
+ log.Fatal(server.ListenAndServe())
+}
+
+func openAI(w http.ResponseWriter, r *http.Request) {
+ var request struct {
+ Model string `json:"model"`
+ Stream bool `json:"stream"`
+ }
+ if json.NewDecoder(r.Body).Decode(&request) != nil {
+ http.Error(w, "invalid JSON", http.StatusBadRequest)
+ return
+ }
+ if request.Stream {
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.Header().Set("Cache-Control", "no-cache")
+ fmt.Fprintf(w, "data: {\"id\":\"chatcmpl-local\",\"object\":\"chat.completion.chunk\",\"model\":%q,\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"hello from mock upstream\"},\"finish_reason\":null}]}\n\n", request.Model)
+ w.(http.Flusher).Flush()
+ time.Sleep(50 * time.Millisecond)
+ fmt.Fprint(w, "data: {\"id\":\"chatcmpl-local\",\"object\":\"chat.completion.chunk\",\"choices\":[],\"usage\":{\"prompt_tokens\":4,\"completion_tokens\":4,\"total_tokens\":8}}\n\ndata: [DONE]\n\n")
+ return
+ }
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "id": "chatcmpl-local", "object": "chat.completion", "model": request.Model,
+ "choices": []any{map[string]any{"index": 0, "message": map[string]string{"role": "assistant", "content": "hello from mock upstream"}, "finish_reason": "stop"}},
+ "usage": map[string]int{"prompt_tokens": 4, "completion_tokens": 4, "total_tokens": 8},
+ })
+}
+
+func anthropic(w http.ResponseWriter, r *http.Request) {
+ var request struct {
+ Model string `json:"model"`
+ }
+ if json.NewDecoder(r.Body).Decode(&request) != nil {
+ http.Error(w, "invalid JSON", http.StatusBadRequest)
+ return
+ }
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "id": "msg_local", "type": "message", "role": "assistant", "model": request.Model,
+ "content": []any{map[string]string{"type": "text", "text": "hello from mock upstream"}},
+ "stop_reason": "end_turn", "stop_sequence": nil,
+ "usage": map[string]int{"input_tokens": 4, "output_tokens": 4},
+ })
+}