DEVELOPMENT ENVIRONMENT

~liljamo/emerwen

ref: 91b99511153d9e5adb1ffba835cb0706a54676f0 emerwen/emerwen-master/src/main.rs -rw-r--r-- 3.1 KiB
91b99511Jonni Liljamo feat(master): change to protocol Worker type 9 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under GPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */

use clap::Parser;
use emerwen_protocol::emerwen::shared::{
    target::{MethodGet, MethodPing},
    Target, Worker,
};
use tokio::sync::mpsc;
use tracing::{debug, info, level_filters::LevelFilter};
use tracing_subscriber::{prelude::*, EnvFilter};

mod db;
use db::DatabaseHandle;
mod server;

#[derive(Parser)]
#[command(version)]
struct Args {
    /// Enable debug logging
    #[arg(long)]
    debug: bool,

    /// Location of the database file
    #[arg(long, default_value = "./tmp/db.redb")]
    database_file: String,

    /// Location of the "IPC" socket
    #[arg(long, default_value = "./tmp/master.sock")]
    ipc_socket_location: String,

    /// IPC auth token
    #[arg(long, default_value = "ipc_avain_prkl")]
    ipc_auth_token: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Args::parse();

    tracing_subscriber::registry()
        .with(
            tracing_subscriber::fmt::layer()
                .with_file(args.debug)
                .with_line_number(args.debug)
                .with_level(true),
        )
        .with(
            EnvFilter::builder()
                .with_default_directive(if args.debug {
                    LevelFilter::DEBUG.into()
                } else {
                    LevelFilter::INFO.into()
                })
                .from_env_lossy(),
        )
        .init();

    let addr = "127.0.0.1:8000";

    info!(message = "Starting emerwen master...", %addr);
    debug!("Hello, debug!");

    let db_handle = DatabaseHandle::new(std::path::Path::new(&args.database_file))?;
    let (server_tx, server_rx) = mpsc::channel::<server::ServerMessage>(8);

    let db_handle_server = db_handle.clone();
    tokio::spawn(async move {
        let _ = server::run(addr.into(), db_handle_server, server_rx).await;
    });

    let _ = db_handle
        .write_worker(Worker {
            id: 0,
            auth_token: "avain_perkele".into(),
            targets: vec![
                Target {
                    id: 0,
                    addr: "127.0.0.1".into(),
                    interval: 2000,
                    method: Some(MethodPing {}.into()),
                },
                Target {
                    id: 1,
                    addr: "https://liljamo.com/".into(),
                    interval: 2000,
                    method: Some(
                        MethodGet {
                            ok_codes: vec![200],
                        }
                        .into(),
                    ),
                },
                Target {
                    id: 2,
                    addr: "10.1.2.30".into(),
                    interval: 2000,
                    method: Some(MethodPing {}.into()),
                },
            ],
        })
        .await;

    server_tx.send(server::ServerMessage::ReloadWorkers).await?;

    loop {
        let _ = tokio::time::sleep(std::time::Duration::from_millis(5000)).await;
    }

    //Ok(())
}