main.rs

Overview

The main.rs file serves as the entry point for the Acki-Nacki GraphQL server application. Its primary role is to parse command-line arguments, initialize tracing/logging, configure necessary runtime settings, and start the web server to accept incoming GraphQL requests. This file integrates several modules and external crates to bootstrap the application, including configuration defaults, schema definitions, HTTP server setup, and SDK client initialization.

Detailed Explanation

Struct: Args

The Args struct defines the command-line interface of the application using the clap::Parser derive macro. It specifies three optional parameters that control server behavior:

Field Name

Description

Type

Notes

db

Path to the database file (bm-archive.db)

Option<String>

Defaults to a constant path if unset

listen

Host address and TCP port for the server to bind to (e.g., 127.0.0.1:3000)

Option<String>

Defaults to 127.0.0.1:3000 if unset

bm_api_socket

Path or identifier for the BM API Unix socket or endpoint

Option<String>

Required; panics if not set

The struct uses attributes to support environment variable overrides and flexible command-line argument counts (num_args = 0..=1).

Usage Example:

cargo run -- --db ./data/bm-archive.db --listen 0.0.0.0:8080 --bm_api_socket /tmp/bm_api.sock

This command sets custom DB path, listen address, and BM API socket.


Function: main

#[tokio::main]
async fn main() -> anyhow::Result<()>

The asynchronous main function is the program's entry point. It performs the following steps:

  1. Initialize tracing/logging:
    Calls init_tracing() from the helpers module to set up runtime logging/tracing instrumentation.

  2. Parse CLI arguments:
    Uses Args::parse() to read and validate command-line parameters.

  3. Resolve configuration values:

    • Converts the optional db argument into a PathBuf, falling back on defaults::PATH_TO_DB if not provided.

    • Resolves the listen address, defaulting to defaults::LISTEN.

    • Retrieves the bm_api_socket argument, panicking if it is absent.

  4. Initialize SDK client:
    Calls init_sdk(Some(bm_api_socket)) from the gql_server crate to create an SDK client instance for communicating with the BM API.

  5. Start web server:
    Invokes web::start(listen, db, sdk_client).await to launch the HTTP server, which listens for incoming GraphQL queries and interacts with the database and the BM API through the SDK client.

  6. Returns:
    An anyhow::Result<()> indicating success or failure of the asynchronous server startup.


Modules and Imports


Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
Args[Args Parsing]
InitTracing[Init Tracing]
InitSDK[Init SDK Client]
StartServer[Start Web Server]
main --> InitTracing
main --> Args
Args --> DBPath[Resolve DB Path]
Args --> ListenAddr[Resolve Listen Addr]
Args --> BMApiSocket[Require BM API Socket]
BMApiSocket --> InitSDK
InitSDK --> StartServer
DBPath --> StartServer
ListenAddr --> StartServer

This flowchart visualizes the startup workflow in main.rs showing how command-line arguments are parsed and resolved, tracing is initialized, the SDK client is instantiated, and the web server is started with these configurations.