Browse Source

wip server

main
Nareshkumar Rao 7 months ago
parent
commit
4410a3ea14
  1. 17
      Cargo.lock
  2. 3
      Cargo.toml
  3. 8
      src/server.rs

17
Cargo.lock

@ -340,6 +340,7 @@ dependencies = [
"serde", "serde",
"tokio", "tokio",
"toml", "toml",
"tower-http",
] ]
[[package]] [[package]]
@ -928,6 +929,22 @@ dependencies = [
"tracing", "tracing",
] ]
[[package]]
name = "tower-http"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5"
dependencies = [
"bitflags",
"bytes",
"http",
"http-body",
"http-body-util",
"pin-project-lite",
"tower-layer",
"tower-service",
]
[[package]] [[package]]
name = "tower-layer" name = "tower-layer"
version = "0.3.2" version = "0.3.2"

3
Cargo.toml

@ -13,4 +13,5 @@ serde = { "version" = "1.0", features = ["derive"] }
toml = "0.8" toml = "0.8"
axum = { "version" = "0.7", features = ["macros"] } axum = { "version" = "0.7", features = ["macros"] }
tokio = { "version" = "1.37" } tokio = { "version" = "1.37" }
chrono = "0.4.38"
chrono = "0.4"
tower-http = { "version" = "0.5", features = ["cors"] }

8
src/server.rs

@ -1,11 +1,12 @@
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, State},
http::StatusCode,
http::{Method, StatusCode},
response::IntoResponse, response::IntoResponse,
routing::get, routing::get,
Json, Router, Json, Router,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tower_http::cors::{Any, CorsLayer};
use crate::{ use crate::{
actuators, actuators,
@ -22,11 +23,16 @@ pub async fn run_server(program_state: ProgramStateShared) {
} }
fn setup_router(program_state: ProgramStateShared) -> Router { fn setup_router(program_state: ProgramStateShared) -> Router {
let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(Any);
Router::new() Router::new()
.route("/info", get(info_handler)) .route("/info", get(info_handler))
.route("/switch/:device/:state", get(switch_handler)) .route("/switch/:device/:state", get(switch_handler))
.route("/pump/:quantity", get(pump_handler)) .route("/pump/:quantity", get(pump_handler))
.with_state(program_state) .with_state(program_state)
.layer(cors)
} }
async fn pump_handler( async fn pump_handler(

Loading…
Cancel
Save