File size: 1,769 Bytes
44dceed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Vehicle Routing Quickstart - Axum Server
//!
//! Run with: cargo run -p vehicle-routing
//! Then open: http://localhost:8082

use owo_colors::OwoColorize;
use std::net::SocketAddr;
use std::path::PathBuf;
use tower_http::cors::{Any, CorsLayer};
use tower_http::services::ServeDir;
use tracing_subscriber::EnvFilter;
use vehicle_routing::console;

#[tokio::main]
async fn main() {
    // Initialize tracing (logs from vehicle_routing at INFO level)
    tracing_subscriber::fmt()
        .with_env_filter(
            EnvFilter::from_default_env()
                .add_directive("vehicle_routing=info".parse().unwrap()),
        )
        .init();

    // Print colorful banner
    console::print_banner();

    // CORS for development
    let cors = CorsLayer::new()
        .allow_origin(Any)
        .allow_methods(Any)
        .allow_headers(Any);

    // Determine static files path (works from workspace root or example dir)
    let static_path = if PathBuf::from("examples/vehicle-routing/static").exists() {
        "examples/vehicle-routing/static"
    } else {
        "static"
    };

    // Build router with static file fallback
    let app = vehicle_routing::api::create_router()
        .fallback_service(ServeDir::new(static_path))
        .layer(cors);

    // Bind and serve
    let addr = SocketAddr::from(([0, 0, 0, 0], 7860));
    println!(
        "{} Server listening on {}",
        "▸".bright_green(),
        format!("http://{}", addr).bright_cyan().underline()
    );
    println!(
        "{} Open {} in your browser\n",
        "▸".bright_green(),
        "http://localhost:7860".bright_cyan().underline()
    );

    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
    axum::serve(listener, app).await.unwrap();
}