ДЗ smart-house-web завершено
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
use std::process::exit;
|
||||
use tracing::{Level, error, trace};
|
||||
|
||||
/// Ошибка инициализации логгера
|
||||
const CODE_LOGGER_INITIALIZATION_ERROR: i32 = 1;
|
||||
|
||||
/// Ошибка инициализации рантайма Tokio
|
||||
const CODE_TOKIO_RUNTIME_CREATION_ERROR: i32 = 2;
|
||||
|
||||
/// Ошибка привязки слушателя
|
||||
const CODE_LISTENER_BINDING_ERROR: i32 = 3;
|
||||
|
||||
/// Ошибка запуска сервера
|
||||
const CODE_STARTIG_SERVER_ERROR: i32 = 4;
|
||||
|
||||
/// Ошибка установки обработчика сигнала завершения
|
||||
const CODE_CTRL_C_SIGNAL_INSTALL_ERROR: i32 = 5;
|
||||
|
||||
/// Инициализация логирования
|
||||
pub fn init_logger() {
|
||||
use tracing_subscriber::{
|
||||
Layer, filter::Targets, fmt::layer, layer::SubscriberExt, registry, util::SubscriberInitExt,
|
||||
};
|
||||
|
||||
let layer = layer()
|
||||
.compact()
|
||||
.with_thread_names(true)
|
||||
.with_file(false)
|
||||
.with_line_number(false)
|
||||
.with_filter(
|
||||
Targets::new()
|
||||
.with_target("axum::serve", Level::INFO)
|
||||
.with_target("hyper_util::client::legacy", Level::INFO)
|
||||
.with_target("reqwest::retry", Level::INFO)
|
||||
.with_default(Level::TRACE),
|
||||
)
|
||||
.boxed();
|
||||
if let Err(e) = registry().with(vec![layer]).try_init() {
|
||||
eprintln!("Logger initialization failed: {:?}", e);
|
||||
exit(CODE_LOGGER_INITIALIZATION_ERROR);
|
||||
} else {
|
||||
trace!("Logger succesfully initialized");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_runtime() -> tokio::runtime::Runtime {
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
match tokio::runtime::Builder::new_multi_thread()
|
||||
.name("tokio")
|
||||
.thread_name_fn(|| {
|
||||
static LAST_ID: AtomicUsize = AtomicUsize::new(0);
|
||||
let id = LAST_ID.fetch_add(1, Ordering::SeqCst);
|
||||
format!("tkwr-{id}")
|
||||
})
|
||||
.worker_threads(2)
|
||||
.thread_stack_size(256 * 1024)
|
||||
.enable_all()
|
||||
.build()
|
||||
{
|
||||
Ok(runtime) => runtime,
|
||||
Err(e) => {
|
||||
error!("Failed to create Tokio runtime: {:?}", e);
|
||||
exit(crate::CODE_TOKIO_RUNTIME_CREATION_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod server;
|
||||
pub use server::{axum_app, server_main};
|
||||
@@ -0,0 +1,8 @@
|
||||
use backend::{axum_app, build_runtime, init_logger, server_main};
|
||||
|
||||
fn main() {
|
||||
init_logger();
|
||||
let runtime = build_runtime();
|
||||
let app = axum_app();
|
||||
runtime.block_on(server_main(app));
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
use axum::{
|
||||
Router,
|
||||
routing::{delete, get, post, put},
|
||||
};
|
||||
use std::{process::exit, sync::Arc};
|
||||
use tokio::sync::RwLock;
|
||||
use tower_http::cors::*;
|
||||
use tracing::{error, info};
|
||||
|
||||
use smart_house::House;
|
||||
|
||||
/// Тип состояния
|
||||
type ServerState = Arc<RwLock<House>>;
|
||||
|
||||
pub fn axum_app() -> Router {
|
||||
let state: ServerState = Arc::new(RwLock::new(House::default()));
|
||||
axum::Router::new()
|
||||
// Тестовый эндпоинт для экспериментов
|
||||
.route("/debug", get(debug::debug))
|
||||
// API дома
|
||||
.route("/rooms", get(house::get_rooms))
|
||||
.route("/rooms", post(house::post_rooms))
|
||||
// API комнат
|
||||
.route("/room/{name}", get(room::get_room))
|
||||
.route("/room/{name}", put(room::put_room))
|
||||
.route("/room/{name}", delete(room::delete_room))
|
||||
.route("/room/{name}/devices", get(room::get_devices))
|
||||
// API устройств
|
||||
.route("/room/{name}/device/{name}", get(device::get_device))
|
||||
.route("/room/{name}/device/{name}", put(device::put_device))
|
||||
.route("/room/{name}/device/{name}", delete(device::delete_device))
|
||||
// Состояние и роут по-умолчанию
|
||||
.with_state(state)
|
||||
.fallback(fallback)
|
||||
.layer(
|
||||
CorsLayer::new()
|
||||
.allow_origin(Any)
|
||||
.allow_headers(Any)
|
||||
.allow_methods(Any),
|
||||
)
|
||||
}
|
||||
|
||||
/// Основная функция сервера
|
||||
pub async fn server_main(app: Router) {
|
||||
let addr = "127.0.0.1:8081";
|
||||
let listener = match tokio::net::TcpListener::bind(addr).await {
|
||||
Ok(listener) => listener,
|
||||
Err(e) => {
|
||||
error!("Failed to bind listener to {}: {:?}", addr, e);
|
||||
exit(crate::CODE_LISTENER_BINDING_ERROR);
|
||||
}
|
||||
};
|
||||
info!("Starting server at {}...", addr);
|
||||
if let Err(e) = axum::serve(listener, app)
|
||||
.with_graceful_shutdown(shutdown_signal())
|
||||
.await
|
||||
{
|
||||
error!("Failed to start server: {:?}", e);
|
||||
exit(crate::CODE_STARTIG_SERVER_ERROR);
|
||||
};
|
||||
info!("Shutdown server");
|
||||
}
|
||||
|
||||
/// Эндпоинт по-умолчанию
|
||||
async fn fallback() -> axum::response::Response {
|
||||
use axum::response::IntoResponse;
|
||||
(axum::http::StatusCode::NOT_FOUND, "404 NOT FOUND").into_response()
|
||||
}
|
||||
|
||||
/// Аккуратное завершение работы сервера
|
||||
async fn shutdown_signal() {
|
||||
// let timeout = async {
|
||||
// tokio::time::sleep(std::time::Duration::from_secs(10)).await;
|
||||
// info!("10 seconds timeout expired");
|
||||
// };
|
||||
let ctrl_c = async {
|
||||
tokio::signal::ctrl_c().await.map_err(|e| {
|
||||
error!("Can't install Ctrl+C signal handler: {:?}", e);
|
||||
exit(crate::CODE_CTRL_C_SIGNAL_INSTALL_ERROR);
|
||||
});
|
||||
info!("Ctrl+C pressed");
|
||||
};
|
||||
let pending = std::future::pending::<()>();
|
||||
tokio::select! {
|
||||
// _ = timeout => {},
|
||||
_ = ctrl_c => {},
|
||||
_ = pending => {},
|
||||
}
|
||||
}
|
||||
|
||||
mod debug;
|
||||
mod device;
|
||||
mod house;
|
||||
mod room;
|
||||
@@ -0,0 +1,13 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use axum::{Json, extract::State};
|
||||
|
||||
use smart_house::{Device, PowerSocket, Room, Thermometer};
|
||||
|
||||
pub async fn debug(State(_server_state): State<super::ServerState>) -> Json<Room> {
|
||||
let map = HashMap::<String, Device>::from([
|
||||
("thermo".into(), Thermometer::new(20.0).into()),
|
||||
("psock".into(), PowerSocket::new(10.0, false).into()),
|
||||
]);
|
||||
Room::new(map).into()
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
};
|
||||
|
||||
use smart_house::{Device, Room};
|
||||
|
||||
pub async fn get_device(
|
||||
State(server_state): State<super::ServerState>,
|
||||
Path((room, device)): Path<(String, String)>,
|
||||
) -> Result<Json<Device>, StatusCode> {
|
||||
let house = server_state.read().await;
|
||||
let Some(room) = house.get_rooms().get(&room) else {
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
};
|
||||
let Some(device) = room.get_devices().get(&device) else {
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
};
|
||||
Ok(device.clone().into())
|
||||
}
|
||||
|
||||
pub async fn put_device(
|
||||
State(server_state): State<super::ServerState>,
|
||||
Path((room, name)): Path<(String, String)>,
|
||||
Json(device): Json<Device>,
|
||||
) -> StatusCode {
|
||||
let mut house = server_state.write().await;
|
||||
let room = house.get_rooms_mut().entry(room).or_insert(Room::default());
|
||||
room.get_devices_mut().insert(name, device);
|
||||
StatusCode::CREATED
|
||||
}
|
||||
|
||||
pub async fn delete_device(
|
||||
State(server_state): State<super::ServerState>,
|
||||
Path((room, device)): Path<(String, String)>,
|
||||
) -> StatusCode {
|
||||
let mut house = server_state.write().await;
|
||||
let Some(room) = house.get_rooms_mut().get_mut(&room) else {
|
||||
return StatusCode::ACCEPTED;
|
||||
};
|
||||
room.get_devices_mut().remove(&device);
|
||||
StatusCode::ACCEPTED
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use axum::{Json, extract::State, http::StatusCode};
|
||||
|
||||
use smart_house::Room;
|
||||
|
||||
pub async fn get_rooms(
|
||||
State(server_state): State<super::ServerState>,
|
||||
) -> Json<HashMap<String, Room>> {
|
||||
server_state.read().await.get_rooms().clone().into()
|
||||
}
|
||||
|
||||
pub async fn post_rooms(
|
||||
State(server_state): State<super::ServerState>,
|
||||
Json(map): Json<HashMap<String, Room>>,
|
||||
) -> StatusCode {
|
||||
for (name, room) in map.into_iter() {
|
||||
server_state.write().await.add_room(name, room);
|
||||
}
|
||||
StatusCode::CREATED
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
};
|
||||
|
||||
use smart_house::{Device, Room};
|
||||
|
||||
pub async fn get_room(
|
||||
State(server_state): State<super::ServerState>,
|
||||
Path(name): Path<String>,
|
||||
) -> Result<Json<Room>, StatusCode> {
|
||||
let house = server_state.read().await;
|
||||
let Some(room) = house.get_rooms().get(&name) else {
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
};
|
||||
Ok(room.clone().into())
|
||||
}
|
||||
|
||||
pub async fn put_room(
|
||||
State(server_state): State<super::ServerState>,
|
||||
Path(name): Path<String>,
|
||||
Json(room): Json<Room>,
|
||||
) -> StatusCode {
|
||||
let mut house = server_state.write().await;
|
||||
house.add_room(name, room);
|
||||
StatusCode::CREATED
|
||||
}
|
||||
|
||||
pub async fn delete_room(
|
||||
State(server_state): State<super::ServerState>,
|
||||
Path(name): Path<String>,
|
||||
) -> StatusCode {
|
||||
server_state.write().await.del_room(&name);
|
||||
StatusCode::ACCEPTED
|
||||
}
|
||||
|
||||
pub async fn get_devices(
|
||||
State(server_state): State<super::ServerState>,
|
||||
Path(name): Path<String>,
|
||||
) -> Result<Json<HashMap<String, Device>>, StatusCode> {
|
||||
let house = server_state.read().await;
|
||||
let Some(room) = house.get_rooms().get(&name) else {
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
};
|
||||
Ok(room.get_devices().clone().into())
|
||||
}
|
||||
Reference in New Issue
Block a user