66 lines
2.3 KiB
Go
66 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/adminweb"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/settings"
|
|
)
|
|
|
|
func newSetupOnlyHandler(runtime *settingsRuntime, token string) (http.Handler, error) {
|
|
if runtime == nil || runtime.admin == nil {
|
|
return nil, errors.New("setup runtime unavailable")
|
|
}
|
|
if strings.TrimSpace(token) == "" {
|
|
return nil, errors.New("DOBOTAI_ADMIN_TOKEN is required in setup-only mode")
|
|
}
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ok", "mode": "setup_required"})
|
|
})
|
|
mux.HandleFunc("GET /version", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(runtimeBuildInfo())
|
|
})
|
|
mux.Handle("/admin/", adminweb.New(runtime.admin, token))
|
|
return mux, nil
|
|
}
|
|
|
|
func runSetupOnly(ctx context.Context, cancel context.CancelFunc, runtime *settingsRuntime, address, tenant string, restartRequested *atomic.Bool) {
|
|
runtime.admin.SetCandidateActivator(&dingTalkCandidateActivator{
|
|
tenant: tenant, repo: runtime.repository, secrets: runtime.secrets, verifier: runtime.verifier,
|
|
bindings: map[string]settings.ResourceBinding{},
|
|
restart: func() { restartRequested.Store(true); cancel() },
|
|
})
|
|
handler, err := newSetupOnlyHandler(runtime, os.Getenv("DOBOTAI_ADMIN_TOKEN"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
server := &http.Server{Addr: address, Handler: handler, ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 15 * time.Second, WriteTimeout: 20 * time.Second, IdleTimeout: 60 * time.Second}
|
|
go func() {
|
|
log.Printf("DobotAI setup-only listener on %s", address)
|
|
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
log.Printf("setup HTTP server failed: %v", err)
|
|
cancel()
|
|
}
|
|
}()
|
|
<-ctx.Done()
|
|
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer shutdownCancel()
|
|
if err := server.Shutdown(shutdownCtx); err != nil {
|
|
log.Printf("setup HTTP shutdown: %v", err)
|
|
}
|
|
if restartRequested.Load() {
|
|
os.Exit(75)
|
|
}
|
|
}
|