Add command line arguments
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

To not rely on hardcoded values introduce command line arguments.
This commit is contained in:
finga 2022-04-23 22:23:19 +02:00
parent b20ed73290
commit bd0dce1bce
3 changed files with 161 additions and 11 deletions

110
Cargo.lock generated
View file

@ -135,6 +135,45 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "3.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c167e37342afc5f33fd87bbc870cedd020d2a6dffa05d45ccd9241fbdd146db"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"lazy_static",
"strsim",
"termcolor",
"textwrap",
]
[[package]]
name = "clap_derive"
version = "3.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "189ddd3b5d32a70b35e7686054371742a937b0d99128e76dde6340210e966669"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "env_logger"
version = "0.9.0"
@ -203,6 +242,18 @@ dependencies = [
"pin-utils",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
@ -281,6 +332,16 @@ dependencies = [
"want",
]
[[package]]
name = "indexmap"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f647032dfaa1f8b6dc29bd3edb7bbef4861b8b8007ebb118d6db284fd59f6ee"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "itoa"
version = "1.0.1"
@ -379,6 +440,7 @@ version = "0.1.0-dev"
dependencies = [
"anyhow",
"axum",
"clap",
"env_logger",
"log",
"serialport",
@ -446,6 +508,12 @@ dependencies = [
"libc",
]
[[package]]
name = "os_str_bytes"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
[[package]]
name = "percent-encoding"
version = "2.1.0"
@ -490,6 +558,30 @@ version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.37"
@ -587,6 +679,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.91"
@ -613,6 +711,12 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
[[package]]
name = "tokio"
version = "1.17.0"
@ -733,6 +837,12 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "want"
version = "0.3.0"

View file

@ -15,3 +15,4 @@ log = "0.4"
env_logger = "0.9"
tokio = { version = "1.17", features = ["rt-multi-thread", "macros"] }
axum = "0.5"
clap = { version = "3.1", features = ["derive"] }

View file

@ -1,9 +1,10 @@
use anyhow::{bail, Error, Result};
use axum::{extract::Extension, routing::get, Router, Server};
use clap::Parser;
use log::{debug, info, trace};
use std::{
io::{BufRead, BufReader},
net::SocketAddr,
net::{IpAddr, SocketAddr},
str::FromStr,
sync::{
atomic::{AtomicU32, AtomicUsize, Ordering},
@ -13,6 +14,32 @@ use std::{
};
use tokio::{spawn, task::JoinHandle, try_join};
#[derive(Parser)]
#[clap(
about,
version,
author,
infer_subcommands(true),
propagate_version(true)
)]
struct Args {
/// The port of the serial device, usually in '/dev/...'.
#[clap(short, long, default_value = "/dev/serial0")]
serial_port: String,
/// The baudrate of the serial connection.
#[clap(short, long, default_value = "9600")]
baud_rate: u32,
/// Timeout of the serial connection in ms.
#[clap(short, long, default_value = "1000")]
timeout: u64,
/// The IPv4 or IPv6 address where the metrics are served.
#[clap(short, long, default_value = "127.0.0.1")]
address: IpAddr,
/// The port where the metrics are served.
#[clap(short, long, default_value = "9111")]
port: u16,
}
#[derive(Default)]
struct AtomicF32 {
storage: AtomicU32,
@ -83,12 +110,14 @@ fn parse(input: &str) -> Result<(usize, usize, f32, Speed)> {
}
#[allow(clippy::unused_async, clippy::similar_names)]
async fn listen_serial(metrics: Arc<Metrics>) -> Result<()> {
let port_name = "/dev/serial0";
let baud_rate = 9600;
async fn listen_serial(
port_name: String,
baud_rate: u32,
timeout: u64,
metrics: Arc<Metrics>,
) -> Result<()> {
let port = serialport::new(port_name, baud_rate)
.timeout(Duration::from_millis(1000))
.timeout(Duration::from_millis(timeout))
.open()?;
let mut port = BufReader::new(port);
@ -129,12 +158,12 @@ mightyohm_gc_mode {}
)
}
async fn listen_http(metrics: Arc<Metrics>) -> Result<()> {
async fn listen_http(address: IpAddr, port: u16, metrics: Arc<Metrics>) -> Result<()> {
let app = Router::new()
.route("/metrics", get(get_metrics))
.layer(Extension(metrics));
let addr = SocketAddr::from(([0, 0, 0, 0], 9111));
info!("Listening on 0.0.0.0:9111");
let addr = SocketAddr::from((address, port));
info!("Listening on {}:{}", address, port);
Ok(Server::bind(&addr).serve(app.into_make_service()).await?)
}
@ -153,11 +182,21 @@ where
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let args = Args::parse();
let metrics = Arc::new(Metrics::default());
try_join!(
flatten(spawn(listen_serial(Arc::clone(&metrics)))),
flatten(spawn(listen_http(Arc::clone(&metrics))))
flatten(spawn(listen_serial(
args.serial_port,
args.baud_rate,
args.timeout,
Arc::clone(&metrics)
))),
flatten(spawn(listen_http(
args.address,
args.port,
Arc::clone(&metrics)
)))
)?;
Ok(())