* serverCommands returns the Cobra command responsible for starting the LedgerForge server. It sets up the API routes, traces, and TypeSense client before launching the server. */
(b *ledgerforgeInstance)
| 317 | It sets up the API routes, traces, and TypeSense client before launching the server. |
| 318 | */ |
| 319 | func serverCommands(b *ledgerforgeInstance) *cobra.Command { |
| 320 | // Define the `start` command for starting the server |
| 321 | cmd := &cobra.Command{ |
| 322 | Use: "start", |
| 323 | Short: "start ledgerforge server", // Short description of the command |
| 324 | Run: func(cmd *cobra.Command, args []string) { |
| 325 | ctx := context.Background() |
| 326 | |
| 327 | // Load configuration |
| 328 | cfg, err := config.Fetch() |
| 329 | if err != nil { |
| 330 | logrus.Error(err) |
| 331 | } |
| 332 | |
| 333 | // Initialize telemetry and observability before the router, |
| 334 | // so MetricsHandler() is available when routes are registered. |
| 335 | phClient, shutdown, err := initializeTelemetryAndObservability(ctx, cfg) |
| 336 | if err != nil { |
| 337 | logrus.Fatal(err) |
| 338 | } |
| 339 | if shutdown != nil { |
| 340 | defer func() { |
| 341 | if err := shutdown(ctx); err != nil { |
| 342 | logrus.Errorf("Error during shutdown: %v", err) |
| 343 | } |
| 344 | }() |
| 345 | } |
| 346 | if phClient != nil { |
| 347 | defer phClient.Close() |
| 348 | } |
| 349 | |
| 350 | // Initialize router (after OTel so /metrics handler is available) |
| 351 | router := initializeRouter(b) |
| 352 | |
| 353 | // Initialize TypeSense |
| 354 | tsClient, err := initializeTypeSense(ctx, cfg) |
| 355 | if err != nil { |
| 356 | logrus.Errorf("TypeSense initialization error: %v", err) |
| 357 | } else if tsClient != nil { |
| 358 | search.TryReindexIfNeeded(ctx, tsClient, b.ledgerforge.GetDataSource()) |
| 359 | } |
| 360 | |
| 361 | // Close database connection pool on shutdown |
| 362 | defer func() { |
| 363 | ds, err := database.GetDBConnection(cfg) |
| 364 | if err == nil && ds != nil { |
| 365 | logrus.Info("Closing database connection pool...") |
| 366 | if err := ds.Close(); err != nil { |
| 367 | logrus.Errorf("Error closing database connection: %v", err) |
| 368 | } |
| 369 | } |
| 370 | }() |
| 371 | |
| 372 | // Start lineage outbox processor |
| 373 | // This worker processes pending lineage entries that were captured atomically with transactions |
| 374 | lineageProcessor := ledgerforge.NewLineageOutboxProcessor(b.ledgerforge) |
| 375 | lineageProcessor.Start(ctx) |
| 376 | defer lineageProcessor.Stop() |
no test coverage detected