cmd/gateway/main.go
Source
- Package:
main - File:
cmd/gateway/main.go - GitHub: https://github.com/theroutercompany/api_router/blob/main/cmd/gateway/main.go
Overview
What: Legacy/compatibility entrypoint that runs the gateway runtime using the default config loader.
Why: Maintains backwards compatibility for environments or scripts that still invoke go run ./cmd/gateway.
How: Calls gatewayconfig.Load(), builds a gatewayruntime.Runtime, runs it, and flushes logs on exit.
Contents
Imports
import block 1
import (
"context"
"errors"
"fmt"
"log"
gatewayconfig "github.com/theroutercompany/api_router/pkg/gateway/config"
gatewayruntime "github.com/theroutercompany/api_router/pkg/gateway/runtime"
pkglog "github.com/theroutercompany/api_router/pkg/log"
)
Functions and Methods
main
What: Program entrypoint that calls run(context.Background()) and exits on error.
Why: Keeps main minimal while allowing the actual logic to be unit-testable in run.
How: Delegates to run and uses log.Fatalf on failure.
func main() {
if err := run(context.Background()); err != nil {
log.Fatalf("gateway failed: %v", err)
}
}
Walkthrough
The list below documents the statements inside the function body, including nested blocks and inline closures.
- L15:
if err := run(context.Background()); err != nil { log.Fatalf("gateway failed: %v", err) }- What: Branches conditionally.
- Why: Handles different execution paths based on runtime state.
- How: Evaluates the condition and executes the matching branch.
- Nested steps:
- L15:
err := run(context.Background())- What: Defines err.
- Why: Keeps intermediate state available for later steps in the function.
- How: Evaluates the right-hand side expressions and stores results in the left-hand variables.
- L16:
log.Fatalf("gateway failed: %v", err)- What: Calls log.Fatalf.
- Why: Performs side effects or delegates work to a helper.
- How: Executes the expression statement.
- L15:
run
What: Loads config, builds the runtime, runs it, and handles shutdown/log flush semantics.
Why: Centralizes the legacy run loop and normalizes cancellation to a clean exit.
How: Calls gatewayconfig.Load(), constructs gatewayruntime.New(cfg), defers pkglog.Sync(), then calls rt.Run(ctx) and treats context.Canceled as nil.
func run(ctx context.Context) error {
cfg, err := gatewayconfig.Load()
if err != nil {
return fmt.Errorf("load config: %w", err)
}
rt, err := gatewayruntime.New(cfg)
if err != nil {
return fmt.Errorf("build runtime: %w", err)
}
defer func() {
if syncErr := pkglog.Sync(); syncErr != nil {
log.Printf("logger sync failed: %v", syncErr)
}
}()
if err := rt.Run(ctx); err != nil {
if errors.Is(err, context.Canceled) {
return nil
}
return err
}
return nil
}
Walkthrough
The list below documents the statements inside the function body, including nested blocks and inline closures.
- L21:
cfg, err := gatewayconfig.Load()- What: Defines cfg, err.
- Why: Keeps intermediate state available for later steps in the function.
- How: Evaluates the right-hand side expressions and stores results in the left-hand variables.
- L22:
if err != nil { return fmt.Errorf("load config: %w", err) }- What: Branches conditionally.
- Why: Short-circuits early when a precondition is not met or an error/edge case is detected.
- How: Evaluates the condition and executes the matching branch.
- Nested steps:
- L23:
return fmt.Errorf("load config: %w", err)- What: Returns from the current function.
- Why: Ends the current execution path and hands control back to the caller.
- How: Executes a
returnstatement (possibly returning values).
- L23:
- L26:
rt, err := gatewayruntime.New(cfg)- What: Defines rt, err.
- Why: Keeps intermediate state available for later steps in the function.
- How: Evaluates the right-hand side expressions and stores results in the left-hand variables.
- L27:
if err != nil { return fmt.Errorf("build runtime: %w", err) }- What: Branches conditionally.
- Why: Short-circuits early when a precondition is not met or an error/edge case is detected.
- How: Evaluates the condition and executes the matching branch.
- Nested steps:
- L28:
return fmt.Errorf("build runtime: %w", err)- What: Returns from the current function.
- Why: Ends the current execution path and hands control back to the caller.
- How: Executes a
returnstatement (possibly returning values).
- L28:
- L31:
defer func() { if syncErr := pkglog.Sync(); syncErr != nil { log.Printf("logger sync failed: %v", syncErr) } }()- What: Defers a call for cleanup.
- Why: Ensures the deferred action runs even on early returns.
- How: Schedules the call to run when the surrounding function returns.
- Nested steps:
- L31:
func() { if syncErr := pkglog.Sync(); syncErr != nil { log.Printf("logger sync failed: %v", syncErr) } }- What: Defines an inline function (closure).
- Why: Encapsulates callback logic and may capture variables from the surrounding scope.
- How: Declares a
funcliteral and uses it as a value (for example, as an HTTP handler or callback). - Nested steps:
- L32:
if syncErr := pkglog.Sync(); syncErr != nil { log.Printf("logger sync failed: %v", syncErr) }- What: Branches conditionally.
- Why: Handles different execution paths based on runtime state.
- How: Evaluates the condition and executes the matching branch.
- Nested steps:
- L32:
syncErr := pkglog.Sync()- What: Defines syncErr.
- Why: Keeps intermediate state available for later steps in the function.
- How: Evaluates the right-hand side expressions and stores results in the left-hand variables.
- L33:
log.Printf("logger sync failed: %v", syncErr)- What: Calls log.Printf.
- Why: Performs side effects or delegates work to a helper.
- How: Executes the expression statement.
- L32:
- L32:
- L31:
- L37:
if err := rt.Run(ctx); err != nil { if errors.Is(err, context.Canceled) { return nil } return err }- What: Branches conditionally.
- Why: Short-circuits early when a precondition is not met or an error/edge case is detected.
- How: Evaluates the condition and executes the matching branch.
- Nested steps:
- L37:
err := rt.Run(ctx)- What: Defines err.
- Why: Keeps intermediate state available for later steps in the function.
- How: Evaluates the right-hand side expressions and stores results in the left-hand variables.
- L38:
if errors.Is(err, context.Canceled) { return nil }- What: Branches conditionally.
- Why: Short-circuits early when a precondition is not met or an error/edge case is detected.
- How: Evaluates the condition and executes the matching branch.
- Nested steps:
- L39:
return nil- What: Returns from the current function.
- Why: Ends the current execution path and hands control back to the caller.
- How: Executes a
returnstatement (possibly returning values).
- L39:
- L41:
return err- What: Returns from the current function.
- Why: Ends the current execution path and hands control back to the caller.
- How: Executes a
returnstatement (possibly returning values).
- L37:
- L43:
return nil- What: Returns from the current function.
- Why: Ends the current execution path and hands control back to the caller.
- How: Executes a
returnstatement (possibly returning values).