4
0
Fork 0
mirror of https://github.com/zzzzDev4/ias-tea-axum.git synced 2025-04-21 07:41:21 +02:00
backend/src/main.rs

53 lines
2 KiB
Rust

// Invoke-WebRequest -Uri https://ias-tea-axum.shuttleapp.rs/addtea -Method POST -Body '{ "teaname": DemoTea, "rfidcode": "lo1rfId42", "watertemp": 200, "steepingseconds": 10}' -ContentType 'application/json'
// cargo shuttle resource delete database::shared::postgres
// cargo shuttle resource list --show-secrets
use axum::{
routing::{get, post, delete},
Router,
};
use axum::http::Method;
use data::MyState;
use sqlx::PgPool;
use tower_http::services::ServeFile;
mod data;
mod requests;
use crate::requests::{get_requests::*, post_requests::*};
#[shuttle_runtime::main]
async fn main(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_axum::ShuttleAxum {
sqlx::migrate!()
.run(&pool)
.await
.expect("Failed to run migrations");
let state = MyState { pool };
let router = Router::new()
.nest_service("/", ServeFile::new("assets/index.html"))
// configuration
.route("/configuration", get(retrieve_config))
.route("/configuration", post(update_config))
// brewing events
.route("/brewing-events", get(retrieve_brewing_events))
// types of tea (update tea metadata, no log entry for steeping time)
.route("/types-of-tea", get(retrieve_types_of_tea))
.route("/types-of-tea/:id", post(update_tea_meta_by_id))
.route("/types-of-tea/:id", get(retrieve_tea_by_id))
// trigger log entry of steeping time
.route(
"/tea-steeping-time-log/:id",
post(update_tea_steeping_time_log_by_id),
)
// teavail-device
.route("/tea-by-rfid/:rfid", get(retrieve_tea_by_rfid))
.route("/add-tea", post(add_tea))
.route("/delete-tea/:id", delete(delete_tea_by_id))
//.route("/update-tea", post(update_tea_by_rfid)) // probably already covered by "update_tea_by_id"
.with_state(state)
.layer(tower_http::cors::CorsLayer::new()
.allow_methods([Method::GET, Method::POST, Method::DELETE])
.allow_origin(tower_http::cors::Any));
Ok(router.into())
}