diff --git a/Cargo.lock b/Cargo.lock index be6b13f..0e38a49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,6 +30,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b52af3cb4058c895d37317bb27508dccc8e5f2d39454016b297bf4a400597b8" dependencies = [ "axum-core", + "axum-macros", "bytes", "form_urlencoded", "futures-util", @@ -75,6 +76,17 @@ dependencies = [ "tracing", ] +[[package]] +name = "axum-macros" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "604fde5e028fea851ce1d8570bbdc034bec850d157f7569d10f347d06808c05c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "bitflags" version = "2.10.0" @@ -611,6 +623,7 @@ dependencies = [ "chrono", "cron_tab", "gpio-cdev", + "tokio", ] [[package]] @@ -662,9 +675,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.48.0" +version = "1.49.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff360e02eab121e0bc37a2d3b4d4dc622e6eda3a8e5253d5435ecf5bd4c68408" +checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86" dependencies = [ "libc", "mio", diff --git a/Cargo.toml b/Cargo.toml index ea093d9..3d1c623 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2024" gpio-cdev = "0.6.0" cron_tab = { version = "0.2", features = ["async"] } chrono = "0.4.42" -axum = "0.8.8" +axum = { version = "0.8.8", features = ["macros"] } +tokio = "1.49.0" diff --git a/src/handler/alarm.rs b/src/handler/alarm.rs new file mode 100644 index 0000000..a2cf06f --- /dev/null +++ b/src/handler/alarm.rs @@ -0,0 +1,4 @@ +#[axum::debug_handler] +pub async fn create_alarm() -> String { + "hello world".to_string() +} diff --git a/src/handler/mod.rs b/src/handler/mod.rs new file mode 100644 index 0000000..bfe5e9a --- /dev/null +++ b/src/handler/mod.rs @@ -0,0 +1 @@ +pub mod alarm; diff --git a/src/main.rs b/src/main.rs index 94f2f0d..cee015e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,26 +1,41 @@ -use std::{sync::{Arc, Mutex}, thread}; +use std::{ + sync::{Arc, Mutex}, + thread, +}; -use gpio_cdev::{Chip, LineRequestFlags}; use cron_tab::Cron; +use gpio_cdev::{Chip, LineRequestFlags}; -fn main() -> Result<(), Box> { +mod handler; +mod router; + +#[tokio::main] +async fn main() -> Result<(), Box> { let chip = Arc::new(Mutex::new(Chip::new("/dev/gpiochip0")?)); - let mut cron = Cron::new(chrono::Local); - let static_time = "0 30 7 * * *"; + let mut cron = Cron::new(chrono::Local); + let static_time = "0 30 7 * * *"; - println!("Adding alarm {}", static_time); - let job_result = cron.add_fn(static_time, move || { - let mut guard = chip.lock().unwrap(); - alarm(&mut*guard).unwrap(); - }); + println!("Adding alarm {}", static_time); + let job_result = cron.add_fn(static_time, move || { + let mut guard = chip.lock().unwrap(); + alarm(&mut *guard).unwrap(); + }); - let _ = job_result.expect("Failed to add job"); + let _ = job_result.expect("Failed to add job"); - cron.start(); + cron.start(); - println!("Started snooze-pal"); + start_server().await; - loop { } + Ok(()) +} + +async fn start_server() { + let router = router::router(); + let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap(); + + println!("Listening on http://0.0.0.0:8080"); + axum::serve(listener, router).await.unwrap(); } fn alarm(chip: &mut Chip) -> Result<(), Box> { diff --git a/src/router/alarm.rs b/src/router/alarm.rs new file mode 100644 index 0000000..3cde634 --- /dev/null +++ b/src/router/alarm.rs @@ -0,0 +1,6 @@ +use axum::{Router, routing::post}; +use crate::handler::alarm::create_alarm; + +pub fn router() -> Router { + Router::new().route("/", post(create_alarm)) +} diff --git a/src/router/mod.rs b/src/router/mod.rs new file mode 100644 index 0000000..2223d78 --- /dev/null +++ b/src/router/mod.rs @@ -0,0 +1,8 @@ +use axum::Router; + +mod alarm; + +pub fn router() -> Router { + Router::new() + .nest("/alarm", alarm::router()) +}