structure for rest methods in progress
This commit is contained in:
4
src/handler/alarm.rs
Normal file
4
src/handler/alarm.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
#[axum::debug_handler]
|
||||
pub async fn create_alarm() -> String {
|
||||
"hello world".to_string()
|
||||
}
|
||||
1
src/handler/mod.rs
Normal file
1
src/handler/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod alarm;
|
||||
43
src/main.rs
43
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<dyn std::error::Error>> {
|
||||
mod handler;
|
||||
mod router;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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<dyn std::error::Error>> {
|
||||
|
||||
6
src/router/alarm.rs
Normal file
6
src/router/alarm.rs
Normal file
@@ -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))
|
||||
}
|
||||
8
src/router/mod.rs
Normal file
8
src/router/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use axum::Router;
|
||||
|
||||
mod alarm;
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.nest("/alarm", alarm::router())
|
||||
}
|
||||
Reference in New Issue
Block a user