2023-03-14 04:11:49 +01:00
|
|
|
use std::io;
|
2021-10-07 05:04:19 +02:00
|
|
|
|
|
|
|
use actix_web::{
|
|
|
|
middleware,
|
|
|
|
web::{self, ReqData},
|
|
|
|
App, HttpResponse, HttpServer,
|
|
|
|
};
|
|
|
|
|
|
|
|
mod add_msg;
|
2023-03-14 04:11:49 +01:00
|
|
|
use self::add_msg::{AddMsg, Msg};
|
2021-10-07 05:04:19 +02:00
|
|
|
|
|
|
|
// wrap route in our middleware factory
|
|
|
|
async fn index(msg: Option<ReqData<Msg>>) -> HttpResponse {
|
|
|
|
if let Some(msg_data) = msg {
|
|
|
|
let Msg(message) = msg_data.into_inner();
|
|
|
|
HttpResponse::Ok().body(message)
|
|
|
|
} else {
|
|
|
|
HttpResponse::InternalServerError().body("No message found.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> io::Result<()> {
|
2023-03-14 04:11:49 +01:00
|
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
|
|
|
|
|
|
|
log::info!("starting HTTP server at http://localhost:8080");
|
2021-10-07 05:04:19 +02:00
|
|
|
|
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
|
|
|
.wrap(middleware::Logger::default())
|
|
|
|
.service(web::resource("/on").wrap(AddMsg::enabled()).to(index))
|
|
|
|
.service(web::resource("/off").wrap(AddMsg::disabled()).to(index))
|
|
|
|
})
|
|
|
|
.bind(("127.0.0.1", 8080))?
|
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|