DEVELOPMENT ENVIRONMENT

~liljamo/canwa

ref: a05128d215529422a7424f1265f53010620ace59 canwa/src/main.rs -rw-r--r-- 2.5 KiB
a05128d2Jonni Liljamo feat: alertmanager webhook route 10 days ago
                                                                                
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) 2025 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */

use std::sync::Arc;

use axum::{
    Router,
    response::Html,
    routing::{get, post},
};
use clap::Parser;
use reqwest::StatusCode;
use tokio::net::TcpListener;
use tower_http::trace::TraceLayer;
use tracing::info;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

mod config;
use config::Config;
mod state;
use state::State;

mod routes;
mod service;

const LICENSE_HTML: &str = include_str!("../static/license.html");

#[derive(Parser)]
#[command(version)]
struct Args {
    /// Config file location.
    #[arg(short, long, default_value = "./canwa.toml")]
    config: String,
    /// Don't run the main program, useful for config validation.
    #[arg(long)]
    dry_run: bool,
}

#[tokio::main]
async fn main() {
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
                format!(
                    "{}=debug,tower_http=debug,axum::rejection=trace",
                    env!("CARGO_CRATE_NAME")
                )
                .into()
            }),
        )
        .with(tracing_subscriber::fmt::layer())
        .init();

    let args: Args = Args::parse();
    let config: Config = Config::from_path(&args.config).await.unwrap();
    let state: Arc<State> = Arc::new(State::from_config(&config).unwrap());

    if args.dry_run {
        std::process::exit(0);
    }

    let router = Router::new()
        .route("/", get(|| async { "canwa" }))
        .route(
            "/license",
            get(|| async { (StatusCode::OK, Html::from(LICENSE_HTML)) }),
        )
        .route(
            "/message",
            post({
                let shared_state = Arc::clone(&state);
                move |headers, body| routes::message::message(shared_state, headers, body)
            }),
        )
        .route(
            "/alertmanager/v4",
            post({
                let shared_state = Arc::clone(&state);
                move |headers, body| {
                    routes::alertmanager::alertmanager_v4(shared_state, headers, body)
                }
            }),
        )
        .layer(TraceLayer::new_for_http())
        .with_state(state);

    let addr = format!("{}:{}", config.interface, config.port);
    info!(msg="serving http", %addr);
    let listener = TcpListener::bind(addr).await.unwrap();
    axum::serve(listener, router).await.unwrap();
}