-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho.rs
88 lines (78 loc) · 2.5 KB
/
echo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use async_tungstenite::tungstenite::http::Request;
use blunt::server::AppContext;
use blunt::webhandler::WebHandler;
use blunt::websocket::{WebSocketHandler, WebSocketMessage};
use hyper::header::CONTENT_TYPE;
use hyper::http::HeaderValue;
use hyper::{Body, Response};
use std::sync::Arc;
use tracing::info;
use tracing_subscriber::{EnvFilter, FmtSubscriber};
use uuid::Uuid;
#[tokio::main]
async fn main() -> hyper::Result<()> {
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "info,blunt=trace");
}
// just for a nice compact tracing messages
FmtSubscriber::builder()
.with_env_filter(EnvFilter::from_default_env())
.with_thread_ids(true)
.with_target(true)
.with_ansi(true)
.compact()
.init();
// just what's actually needed
let web = HelloWorldWeb::default();
::blunt::builder()
.for_path_with_ctor("/echo", |ctx| EchoServer { ctx })
.for_web_path("/world", web)
.build()
.bind("127.0.0.1:3000".parse().expect("Invalid Socket Addr"))
.await?;
Ok(())
}
#[derive(Debug)]
pub struct EchoServer {
ctx: AppContext,
}
#[blunt::async_trait]
impl WebSocketHandler for EchoServer {
async fn on_open(&mut self, session_id: Uuid) {
info!("new connection open with id: {}", session_id);
self.ctx.session(session_id).await.and_then(|s| {
s.send(WebSocketMessage::Text(String::from(
"Welcome to Echo server!",
)))
.ok()
});
}
async fn on_message(&mut self, session_id: Uuid, msg: WebSocketMessage) {
info!(
"echo back for session id {}, with message: {}",
session_id, msg
);
self.ctx
.session(session_id)
.await
.and_then(|s| s.send(msg).ok());
}
async fn on_close(&mut self, session_id: Uuid, _msg: WebSocketMessage) {
info!("connection closed for session id {}", session_id);
}
}
#[derive(Debug, Default)]
pub struct HelloWorldWeb;
#[blunt::async_trait]
impl WebHandler for HelloWorldWeb {
async fn handle(&mut self, request: Request<Body>) -> Arc<hyper::Result<Response<Body>>> {
let message = format!("Hello World from path: {}", request.uri().path());
Arc::new(Ok(Response::builder()
.header(
CONTENT_TYPE,
HeaderValue::from_static("text/plain; charset=utf-8"),
)
.body(Body::from(message))
.unwrap()))
}
}