|
@@ -1,56 +1,41 @@
|
|
|
-use std::sync::Mutex;
|
|
|
|
|
-
|
|
|
|
|
-use actix_web::{dev::Service as _, get, post, web, HttpResponse, HttpServer, Responder};
|
|
|
|
|
-use utils::strings::StaticString;
|
|
|
|
|
|
|
+use utils::{
|
|
|
|
|
+ http::{self, Request, Response},
|
|
|
|
|
+ strings::StaticString,
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
const MAX_MESSAGE_LENGTH: usize = 2048;
|
|
const MAX_MESSAGE_LENGTH: usize = 2048;
|
|
|
const MESSAGE_HISTORY_LENGTH: usize = 64;
|
|
const MESSAGE_HISTORY_LENGTH: usize = 64;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
#[derive(Debug)]
|
|
|
-struct AppdataInner {
|
|
|
|
|
|
|
+struct Appdata {
|
|
|
messages: [StaticString<MAX_MESSAGE_LENGTH>; MESSAGE_HISTORY_LENGTH],
|
|
messages: [StaticString<MAX_MESSAGE_LENGTH>; MESSAGE_HISTORY_LENGTH],
|
|
|
index: usize,
|
|
index: usize,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-impl AppdataInner {
|
|
|
|
|
|
|
+impl Appdata {
|
|
|
pub fn new() -> Self {
|
|
pub fn new() -> Self {
|
|
|
Self {
|
|
Self {
|
|
|
messages: core::array::from_fn(|_| StaticString::new()),
|
|
messages: core::array::from_fn(|_| StaticString::new()),
|
|
|
index: 0,
|
|
index: 0,
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-#[derive(Debug)]
|
|
|
|
|
-struct Appdata {
|
|
|
|
|
- inner: Mutex<AppdataInner>,
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-impl Appdata {
|
|
|
|
|
- pub fn new() -> Self {
|
|
|
|
|
- Self {
|
|
|
|
|
- inner: Mutex::new(AppdataInner::new()),
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- pub fn insert_message(&self, message: String) -> Result<(), ()> {
|
|
|
|
|
|
|
+ pub fn insert_message(&mut self, message: String) -> Result<(), ()> {
|
|
|
if message.len() > MAX_MESSAGE_LENGTH {
|
|
if message.len() > MAX_MESSAGE_LENGTH {
|
|
|
return Err(());
|
|
return Err(());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- let mut guard = self.inner.lock().unwrap();
|
|
|
|
|
- let index = guard.index;
|
|
|
|
|
- guard.messages[index] = message.into();
|
|
|
|
|
- guard.index = (index + 1) % guard.messages.len();
|
|
|
|
|
|
|
+ let index = self.index;
|
|
|
|
|
+ self.messages[index] = message.into();
|
|
|
|
|
+ self.index = (index + 1) % self.messages.len();
|
|
|
|
|
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pub fn get_messages(&self) -> Vec<String> {
|
|
pub fn get_messages(&self) -> Vec<String> {
|
|
|
- let guard = self.inner.lock().unwrap();
|
|
|
|
|
let mut messages = vec![];
|
|
let mut messages = vec![];
|
|
|
|
|
|
|
|
- for message in guard.messages.iter() {
|
|
|
|
|
|
|
+ for message in self.messages.iter() {
|
|
|
if message.is_empty() {
|
|
if message.is_empty() {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
@@ -61,47 +46,40 @@ impl Appdata {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-#[get("/")]
|
|
|
|
|
-async fn index_get(data: web::Data<Appdata>) -> impl Responder {
|
|
|
|
|
- let string = data.get_messages().join(",");
|
|
|
|
|
|
|
+fn index_get_sync(state: &mut Appdata, _: Request) -> Response {
|
|
|
|
|
+ let string = state.get_messages().join(",");
|
|
|
println!("Sending `{}` characters", string.len());
|
|
println!("Sending `{}` characters", string.len());
|
|
|
- HttpResponse::Ok().body(string)
|
|
|
|
|
|
|
+ Response::ok().body(&string)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-#[post("/")]
|
|
|
|
|
-async fn index_post(body: web::Payload, data: web::Data<Appdata>) -> impl Responder {
|
|
|
|
|
- let bytes = body.to_bytes().await.unwrap();
|
|
|
|
|
- let str = String::from_utf8_lossy(&bytes).into_owned();
|
|
|
|
|
|
|
+fn index_post_sync(state: &mut Appdata, request: Request) -> Response {
|
|
|
|
|
+ let body = request.body;
|
|
|
|
|
+
|
|
|
|
|
+ println!("Received ({}): {:#?}", body.len(), body);
|
|
|
|
|
|
|
|
- println!("Received ({}): {:#?}", str.len(), str);
|
|
|
|
|
|
|
+ match state.insert_message(body.clone()) {
|
|
|
|
|
+ Ok(()) => Response::ok().body(&body),
|
|
|
|
|
+ Err(()) => Response::bad_request(),
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- match data.insert_message(str.clone()) {
|
|
|
|
|
- Ok(()) => HttpResponse::Ok().body(str),
|
|
|
|
|
- Err(()) => HttpResponse::BadRequest().body(""),
|
|
|
|
|
|
|
+fn router(state: &mut Appdata, request: Request) -> Response {
|
|
|
|
|
+ if request.url != "/" {
|
|
|
|
|
+ return Response::not_found();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ match request.method {
|
|
|
|
|
+ http::Method::Get => index_get_sync(state, request),
|
|
|
|
|
+ http::Method::Post => index_post_sync(state, request),
|
|
|
|
|
+ http::Method::Other => Response::method_not_allowed(),
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-#[actix_web::main]
|
|
|
|
|
-async fn main() -> std::io::Result<()> {
|
|
|
|
|
|
|
+fn main() -> http::Result<()> {
|
|
|
println!("Starting...");
|
|
println!("Starting...");
|
|
|
|
|
|
|
|
- let appdata = web::Data::new(Appdata::new());
|
|
|
|
|
-
|
|
|
|
|
- HttpServer::new(move || {
|
|
|
|
|
- actix_web::App::new()
|
|
|
|
|
- .app_data(appdata.clone())
|
|
|
|
|
- .wrap_fn(|req, srv| {
|
|
|
|
|
- // println!("{:#?}", LoggedRequest::new(&req));
|
|
|
|
|
- let fut = srv.call(req);
|
|
|
|
|
- async {
|
|
|
|
|
- let res = fut.await?;
|
|
|
|
|
- Ok(res)
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
- .service(index_get)
|
|
|
|
|
- .service(index_post)
|
|
|
|
|
- })
|
|
|
|
|
- .bind(("127.0.0.1", 8080))?
|
|
|
|
|
- .run()
|
|
|
|
|
- .await
|
|
|
|
|
|
|
+ let appdata = Appdata::new();
|
|
|
|
|
+ http::run("127.0.0.1:8080", appdata, router)?;
|
|
|
|
|
+
|
|
|
|
|
+ Ok(())
|
|
|
}
|
|
}
|