Start starts the HTTP/WebSocket server
(ctx context.Context)
| 80 | |
| 81 | // Start starts the HTTP/WebSocket server |
| 82 | func (b *ElectronBridge) Start(ctx context.Context) error { |
| 83 | b.ctx, b.cancel = context.WithCancel(ctx) |
| 84 | |
| 85 | mux := http.NewServeMux() |
| 86 | |
| 87 | // REST API endpoints (same as Tauri) |
| 88 | mux.HandleFunc("/api/chat", b.handleChat) |
| 89 | mux.HandleFunc("/api/cancel", b.handleCancel) |
| 90 | mux.HandleFunc("/api/approve", b.handleApprove) |
| 91 | mux.HandleFunc("/api/status", b.handleStatus) |
| 92 | mux.HandleFunc("/api/history", b.handleHistory) |
| 93 | mux.HandleFunc("/api/config", b.handleConfig) |
| 94 | |
| 95 | // WebSocket endpoint |
| 96 | mux.HandleFunc("/ws", b.handleWebSocket) |
| 97 | |
| 98 | // SSE endpoint (fallback) |
| 99 | mux.HandleFunc("/api/events", b.handleSSE) |
| 100 | |
| 101 | // Health check |
| 102 | mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { |
| 103 | w.WriteHeader(http.StatusOK) |
| 104 | _ = json.NewEncoder(w).Encode(map[string]any{ |
| 105 | "status": "ok", |
| 106 | "framework": "electron", |
| 107 | "port": b.port, |
| 108 | }) |
| 109 | }) |
| 110 | |
| 111 | // CORS middleware |
| 112 | handler := corsMiddleware(mux) |
| 113 | |
| 114 | b.server = &http.Server{ |
| 115 | Addr: fmt.Sprintf("127.0.0.1:%d", b.port), |
| 116 | Handler: handler, |
| 117 | } |
| 118 | |
| 119 | // Start server in goroutine |
| 120 | go func() { |
| 121 | ln, err := net.Listen("tcp", b.server.Addr) |
| 122 | if err != nil { |
| 123 | return |
| 124 | } |
| 125 | _ = b.server.Serve(ln) // Server error logged by http.Server |
| 126 | }() |
| 127 | |
| 128 | return nil |
| 129 | } |
| 130 | |
| 131 | // Stop stops the HTTP/WebSocket server |
| 132 | func (b *ElectronBridge) Stop(ctx context.Context) error { |
nothing calls this directly
no test coverage detected