46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
package codexhost
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// Main is the detached bridge's command entry point.
|
|
func Main(args []string) int {
|
|
flags := flag.NewFlagSet("pipelines-codex-host", flag.ContinueOnError)
|
|
dir := flags.String("dir", "", "private per-server host directory")
|
|
upstream := flags.String("upstream", "", "existing Codex App Server Unix socket")
|
|
idle := flags.Duration("idle", 20*time.Minute, "exit after this long detached without pending interactions")
|
|
maxDetached := flags.Duration("max-detached", 24*time.Hour, "maximum detached lifetime, including pending interactions")
|
|
if err := flags.Parse(args); err != nil {
|
|
return 2
|
|
}
|
|
if *dir == "" || *upstream == "" {
|
|
fmt.Fprintln(os.Stderr, "-dir and -upstream are required")
|
|
return 2
|
|
}
|
|
syscall.Umask(0o077)
|
|
signal.Ignore(syscall.SIGHUP, syscall.SIGPIPE)
|
|
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer cancel()
|
|
err := Serve(ctx, Options{Dir: *dir, Upstream: *upstream, Idle: *idle, MaxDetached: *maxDetached, Ready: func(socket string) {
|
|
fmt.Fprintln(os.Stdout, "ready "+socket)
|
|
if null, err := os.OpenFile(os.DevNull, os.O_RDWR, 0); err == nil {
|
|
_ = syscall.Dup2(int(null.Fd()), 0)
|
|
_ = syscall.Dup2(int(null.Fd()), 1)
|
|
null.Close()
|
|
}
|
|
}})
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
fmt.Fprintln(os.Stdout, "error "+err.Error())
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|