Start starts the OAuth callback server. It sets up the HTTP handlers for the callback and success endpoints, and begins listening on the specified port. Returns: - error: An error if the server fails to start
()
| 67 | // Returns: |
| 68 | // - error: An error if the server fails to start |
| 69 | func (s *OAuthServer) Start() error { |
| 70 | s.mu.Lock() |
| 71 | defer s.mu.Unlock() |
| 72 | |
| 73 | if s.running { |
| 74 | return fmt.Errorf("server is already running") |
| 75 | } |
| 76 | |
| 77 | // Check if port is available |
| 78 | if !s.isPortAvailable() { |
| 79 | return fmt.Errorf("port %d is already in use", s.port) |
| 80 | } |
| 81 | |
| 82 | mux := http.NewServeMux() |
| 83 | mux.HandleFunc("/auth/callback", s.handleCallback) |
| 84 | mux.HandleFunc("/success", s.handleSuccess) |
| 85 | |
| 86 | s.server = &http.Server{ |
| 87 | Addr: fmt.Sprintf(":%d", s.port), |
| 88 | Handler: mux, |
| 89 | ReadTimeout: 10 * time.Second, |
| 90 | WriteTimeout: 10 * time.Second, |
| 91 | } |
| 92 | |
| 93 | s.running = true |
| 94 | |
| 95 | // Start server in goroutine |
| 96 | go func() { |
| 97 | if err := s.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { |
| 98 | s.errorChan <- fmt.Errorf("server failed to start: %w", err) |
| 99 | } |
| 100 | }() |
| 101 | |
| 102 | // Give server a moment to start |
| 103 | time.Sleep(100 * time.Millisecond) |
| 104 | |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | // Stop gracefully stops the OAuth callback server. |
| 109 | // It performs a graceful shutdown of the HTTP server with a timeout. |
no test coverage detected