| 257 | } |
| 258 | |
| 259 | func startServer(router *gin.Engine, port string) error { |
| 260 | server := &http.Server{ |
| 261 | Addr: ":" + port, |
| 262 | Handler: router, |
| 263 | } |
| 264 | |
| 265 | // Start server in goroutine |
| 266 | go func() { |
| 267 | logrus.Infof("Server started on port %s", port) |
| 268 | if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 269 | logrus.Fatalf("Server error: %v", err) |
| 270 | } |
| 271 | }() |
| 272 | |
| 273 | // Wait for interrupt signal |
| 274 | quit := make(chan os.Signal, 1) |
| 275 | signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) |
| 276 | <-quit |
| 277 | |
| 278 | logrus.Info("Shutting down server...") |
| 279 | |
| 280 | // Give outstanding requests 30 seconds to complete |
| 281 | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 282 | defer cancel() |
| 283 | |
| 284 | if err := server.Shutdown(ctx); err != nil { |
| 285 | logrus.Errorf("Server forced to shutdown: %v", err) |
| 286 | return err |
| 287 | } |
| 288 | |
| 289 | logrus.Info("Server exited gracefully") |
| 290 | return nil |
| 291 | } |
| 292 | |
| 293 | // Renamed from initializeObservability to better reflect its purpose |
| 294 | func initializeTelemetryAndObservability(ctx context.Context, cfg *config.Configuration) (posthog.Client, func(context.Context) error, error) { |