added webapi sketch
This commit is contained in:
parent
6c6e131e61
commit
b3e25cd706
5 changed files with 852 additions and 35 deletions
795
Cargo.lock
generated
795
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -6,13 +6,21 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-trait = "0.1.83"
|
async-trait = "0.1.83"
|
||||||
atoi_radix10 = "0.0.1"
|
atoi_radix10 = "0.0.1"
|
||||||
|
axum = { version = "0.7.7", features = ["ws"] }
|
||||||
|
axum-extra = { version = "0.9.4", features = ["typed-header"] }
|
||||||
bytes = "1.6.0"
|
bytes = "1.6.0"
|
||||||
chrono = "0.4.38"
|
chrono = "0.4.38"
|
||||||
debug_print = "1.0.0"
|
debug_print = "1.0.0"
|
||||||
|
futures = "0.3.31"
|
||||||
|
futures-util = { version = "0.3.31", features = ["sink", "std"] }
|
||||||
|
headers = "0.4.0"
|
||||||
image = "0.25.2"
|
image = "0.25.2"
|
||||||
rand = "*"
|
rand = "*"
|
||||||
tokio = { version = "1.38", features = ["full"] }
|
tokio = { version = "1.38", features = ["full"] }
|
||||||
tokio-test = "*"
|
tokio-test = "*"
|
||||||
|
tower-http = { version = "0.6.1", features = ["fs", "trace"] }
|
||||||
|
tracing = "0.1.40"
|
||||||
|
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "*"
|
tempfile = "*"
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ pub mod flutclient;
|
||||||
pub mod grid;
|
pub mod grid;
|
||||||
pub mod protocols;
|
pub mod protocols;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
pub mod webapi;
|
||||||
|
|
||||||
mod color;
|
mod color;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::VecDeque,
|
collections::VecDeque,
|
||||||
fs::{create_dir_all, File},
|
fs::{create_dir_all, File},
|
||||||
io::{self, Error, ErrorKind},
|
io,
|
||||||
path::Path,
|
path::Path,
|
||||||
sync::{atomic::AtomicU64, Arc},
|
sync::Arc,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -17,9 +17,8 @@ use flurry::{
|
||||||
};
|
};
|
||||||
use image::{codecs::jpeg::JpegEncoder, GenericImageView, SubImage};
|
use image::{codecs::jpeg::JpegEncoder, GenericImageView, SubImage};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter},
|
|
||||||
net::TcpListener,
|
net::TcpListener,
|
||||||
time::{interval, sleep, timeout, Instant},
|
time::interval
|
||||||
};
|
};
|
||||||
|
|
||||||
/// This function logs the current amount of changed pixels to stdout every second
|
/// This function logs the current amount of changed pixels to stdout every second
|
||||||
|
|
|
||||||
76
src/webapi.rs
Normal file
76
src/webapi.rs
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
|
use axum::{
|
||||||
|
extract::{ws::{Message, WebSocket, WebSocketUpgrade}, ConnectInfo},
|
||||||
|
response::IntoResponse,
|
||||||
|
routing::any,
|
||||||
|
Router,
|
||||||
|
};
|
||||||
|
use axum_extra::TypedHeader;
|
||||||
|
use futures::StreamExt;
|
||||||
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
use tower_http::{
|
||||||
|
services::ServeDir,
|
||||||
|
trace::{DefaultMakeSpan, TraceLayer},
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
async fn serve() {
|
||||||
|
// diagnostics
|
||||||
|
tracing_subscriber::registry()
|
||||||
|
.with(
|
||||||
|
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
|
||||||
|
format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.with(tracing_subscriber::fmt::layer())
|
||||||
|
.init();
|
||||||
|
|
||||||
|
let app = Router::new()
|
||||||
|
.route("/imgstream", any(ws_handler))
|
||||||
|
// logging middleware
|
||||||
|
.layer(
|
||||||
|
TraceLayer::new_for_http()
|
||||||
|
.make_span_with(DefaultMakeSpan::default().include_headers(true)),
|
||||||
|
);
|
||||||
|
|
||||||
|
// run it with hyper
|
||||||
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
||||||
|
|
||||||
|
axum::serve(
|
||||||
|
listener,
|
||||||
|
app.into_make_service_with_connect_info::<SocketAddr>(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async fn ws_handler(
|
||||||
|
ws: WebSocketUpgrade,
|
||||||
|
user_agent: Option<TypedHeader<headers::UserAgent>>,
|
||||||
|
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let user_agent = if let Some(TypedHeader(user_agent)) = user_agent {
|
||||||
|
user_agent.to_string()
|
||||||
|
} else {
|
||||||
|
String::from("Unknown browser")
|
||||||
|
};
|
||||||
|
println!("`{user_agent}` at {addr} connected.");
|
||||||
|
|
||||||
|
// finalize the upgrade process by returning upgrade callback.
|
||||||
|
// we can customize the callback by sending additional info such as address.
|
||||||
|
ws.on_upgrade(move |socket| img_stream(socket, addr))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn img_stream(mut socket: WebSocket, who: SocketAddr) {
|
||||||
|
let (mut sender, mut receiver) = socket.split();
|
||||||
|
|
||||||
|
while true {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue