From 542d7e5d611726c91f855073efe5bb63c1ad44be Mon Sep 17 00:00:00 2001 From: Owen Date: Wed, 19 Nov 2025 16:24:07 -0500 Subject: [PATCH] Break out start and stop API Former-commit-id: 196d1cdee7290f1eadcc33e0fd0ac8c82a05d744 --- api/api.go | 15 +++++++++++++++ main.go | 3 +++ olm/olm.go | 24 ++++++++++++++++++++---- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/api/api.go b/api/api.go index a370b82..b8c848e 100644 --- a/api/api.go +++ b/api/api.go @@ -114,6 +114,7 @@ func (s *API) Start() error { mux.HandleFunc("/switch-org", s.handleSwitchOrg) mux.HandleFunc("/disconnect", s.handleDisconnect) mux.HandleFunc("/exit", s.handleExit) + mux.HandleFunc("/health", s.handleHealth) s.server = &http.Server{ Handler: mux, @@ -309,6 +310,20 @@ func (s *API) handleStatus(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(resp) } +// handleHealth handles the /health endpoint +func (s *API) handleHealth(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(map[string]string{ + "status": "ok", + }) +} + // handleExit handles the /exit endpoint func (s *API) handleExit(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { diff --git a/main.go b/main.go index 4656636..548cd42 100644 --- a/main.go +++ b/main.go @@ -214,6 +214,9 @@ func runOlmMainWithArgs(ctx context.Context, args []string) { } olm.Init(ctx, olmConfig) + if err := olm.StartApi(); err != nil { + logger.Fatal("Failed to start API server: %v", err) + } if config.ID != "" && config.Secret != "" && config.Endpoint != "" { tunnelConfig := olm.TunnelConfig{ diff --git a/olm/olm.go b/olm/olm.go index 4c067e8..d403ed0 100644 --- a/olm/olm.go +++ b/olm/olm.go @@ -101,10 +101,6 @@ func Init(ctx context.Context, config GlobalConfig) { apiServer.SetVersion(config.Version) - if err := apiServer.Start(); err != nil { - logger.Fatal("Failed to start HTTP server: %v", err) - } - // Set up API handlers apiServer.SetHandlers( // onConnect @@ -907,3 +903,23 @@ func StopTunnel() { logger.Info("Tunnel process stopped") } + +func StopApi() error { + if apiServer != nil { + err := apiServer.Stop() + if err != nil { + return fmt.Errorf("failed to stop API server: %w", err) + } + } + return nil +} + +func StartApi() error { + if apiServer != nil { + err := apiServer.Start() + if err != nil { + return fmt.Errorf("failed to start API server: %w", err) + } + } + return nil +}