[WIP] A simple wake-on-lan service
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor server.rs

+84 -69
-69
src/server.rs
··· 1 - use std::{net::IpAddr, pin::Pin, sync::Arc, time::Instant}; 2 - 3 - use axum::{ 4 - Json, Router, 5 - body::Body, 6 - extract::{Query, Request, State}, 7 - handler::Handler, 8 - http::Response, 9 - middleware::{self}, 10 - routing::{get, post}, 11 - }; 12 - use serde::Deserialize; 13 - 14 - use crate::{config::Config, mac::MacAddress}; 15 - 16 - include!(concat!(env!("OUT_DIR"), "/mod.rs")); 17 - 18 - #[derive(Deserialize)] 19 - struct WakeRequest { 20 - #[serde(deserialize_with = "crate::utils::deserialize_mac")] 21 - mac: MacAddress, 22 - } 23 - 24 - async fn wake(Json(req): Json<WakeRequest>) -> Result<Json<bool>, Response<String>> { 25 - println!("Waking {}", req.mac); 26 - req.mac.wake().await.map(|_| Json(true)).map_err(|err| { 27 - Response::builder() 28 - .status(500) 29 - .body(err.to_string()) 30 - // unwrap is safe since this will always be a valid respose 31 - .unwrap() 32 - }) 33 - } 34 - 35 - #[derive(Deserialize)] 36 - struct PingRequest { 37 - #[serde(deserialize_with = "crate::utils::deserialize_ip")] 38 - ip: IpAddr, 39 - } 40 - 41 - async fn ping(Query(req): Query<PingRequest>) -> Result<Json<bool>, Response<String>> { 42 - println!("Pinging {}", req.ip); 43 - 44 - Ok(Json(false)) 45 - } 46 - 47 - async fn config(State(conf): State<Arc<Config>>) -> Json<Config> { 48 - Json((*conf).clone()) 49 - } 50 - 51 - async fn log(req: Request, next: axum::middleware::Next) -> axum::response::Response { 52 - let start = Instant::now(); 53 - let method = req.method().to_string(); 54 - let uri = req.uri().to_string(); 55 - let res = next.run(req).await; 56 - 57 - println!("({:?}) [{}] {}", start.elapsed(), method, uri); 58 - res 59 - } 60 - 61 - pub fn router(conf: Config) -> Router { 62 - Router::new() 63 - .route("/wake", post(wake)) 64 - .route("/config", get(config)) 65 - .route("/ping", get(ping)) 66 - .with_state(Arc::new(conf)) 67 - .merge(dist::main()) 68 - .layer(middleware::from_fn(log)) 69 - }
+9
src/server/config.rs
··· 1 + use std::sync::Arc; 2 + 3 + use axum::{Json, extract::State}; 4 + 5 + use crate::config::Config; 6 + 7 + pub async fn main(State(conf): State<Arc<Config>>) -> Json<Config> { 8 + Json((*conf).clone()) 9 + }
+13
src/server/log.rs
··· 1 + use std::time::Instant; 2 + 3 + use axum::extract::Request; 4 + 5 + pub async fn main(req: Request, next: axum::middleware::Next) -> axum::response::Response { 6 + let start = Instant::now(); 7 + let method = req.method().to_string(); 8 + let uri = req.uri().to_string(); 9 + let res = next.run(req).await; 10 + 11 + println!("({:?}) [{}] {}", start.elapsed(), method, uri); 12 + res 13 + }
+25
src/server/mod.rs
··· 1 + use std::sync::Arc; 2 + 3 + use axum::{ 4 + Router, 5 + middleware::{self}, 6 + routing::{get, post}, 7 + }; 8 + 9 + use crate::config::Config; 10 + 11 + include!(concat!(env!("OUT_DIR"), "/mod.rs")); 12 + mod config; 13 + mod log; 14 + mod ping; 15 + mod wake; 16 + 17 + pub fn router(conf: Config) -> Router { 18 + Router::new() 19 + .route("/wake", post(wake::main)) 20 + .route("/config", get(config::main)) 21 + .route("/ping", get(ping::main)) 22 + .with_state(Arc::new(conf)) 23 + .merge(dist::main()) 24 + .layer(middleware::from_fn(log::main)) 25 + }
+16
src/server/ping.rs
··· 1 + use std::net::IpAddr; 2 + 3 + use axum::{Json, extract::Query, http::Response}; 4 + use serde::Deserialize; 5 + 6 + #[derive(Deserialize)] 7 + pub struct PingRequest { 8 + #[serde(deserialize_with = "crate::utils::deserialize_ip")] 9 + ip: IpAddr, 10 + } 11 + 12 + pub async fn main(Query(req): Query<PingRequest>) -> Result<Json<bool>, Response<String>> { 13 + println!("Pinging {}", req.ip); 14 + 15 + Ok(Json(false)) 16 + }
+21
src/server/wake.rs
··· 1 + use axum::{Json, response::Response}; 2 + use serde::Deserialize; 3 + 4 + use crate::mac::MacAddress; 5 + 6 + #[derive(Deserialize)] 7 + pub struct WakeRequest { 8 + #[serde(deserialize_with = "crate::utils::deserialize_mac")] 9 + mac: MacAddress, 10 + } 11 + 12 + pub async fn main(Json(req): Json<WakeRequest>) -> Result<Json<bool>, Response<String>> { 13 + println!("Waking {}", req.mac); 14 + req.mac.wake().await.map(|_| Json(true)).map_err(|err| { 15 + Response::builder() 16 + .status(500) 17 + .body(err.to_string()) 18 + // unwrap is safe since this will always be a valid respose 19 + .unwrap() 20 + }) 21 + }