Skip to content

Commit 308e566

Browse files
committed
Updated how api is structured in codora-framework-bot
1 parent 581d265 commit 308e566

File tree

40 files changed

+373
-775
lines changed

40 files changed

+373
-775
lines changed

crates/codora-framework-bot/__dump.rs

Lines changed: 0 additions & 184 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use std::time::Duration;
2+
use tokio::sync::watch;
3+
use tracing::{info, trace};
4+
use tracing_subscriber::EnvFilter;
5+
6+
#[allow(unreachable_code)]
7+
#[tokio::main]
8+
async fn main() -> anyhow::Result<()> {
9+
tracing_subscriber::fmt()
10+
.with_env_filter(EnvFilter::new("trace"))
11+
.init();
12+
13+
let ctrl_handler = async {
14+
tokio::signal::ctrl_c()
15+
.await
16+
.inspect(|_| trace!("Signal Recieved, Resolving now!!!"))
17+
.expect("Failed to install CTRL-C handler");
18+
};
19+
let (signal_sender, signal_reciever) = watch::channel(());
20+
21+
// This will resolve to drop the reciever when recieved
22+
tokio::task::spawn(async {
23+
ctrl_handler.await;
24+
drop(signal_reciever);
25+
});
26+
27+
let worker = async {
28+
// This will continue running
29+
loop {
30+
tokio::time::sleep(Duration::from_secs(1)).await;
31+
info!("[Worker]: working ...")
32+
}
33+
};
34+
35+
let close_signal_sender = async {
36+
// Update all the reciever;
37+
signal_sender
38+
.send(())
39+
.expect("Failed to send");
40+
signal_sender.closed().await;
41+
};
42+
43+
tokio::select! {
44+
_ = worker => {
45+
info!("[Worker]: should never execute");
46+
}
47+
result = close_signal_sender => {
48+
info!("[Signal]: {:?}, shutting down and this is called because there's no reciever", result);
49+
}
50+
}
51+
Ok(())
52+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use http_body_util::Full;
2+
use hyper::{Request, Response, body::Bytes, server::conn::http2, service::service_fn};
3+
use hyper_util::rt::{TokioExecutor, TokioIo};
4+
use std::{convert::Infallible, pin::pin};
5+
use tokio::net::TcpListener;
6+
7+
async fn handler(_: Request<impl hyper::body::Body>) -> Result<Response<Full<Bytes>>, Infallible> {
8+
Ok(Response::new(Full::new(Bytes::from("Hello World!"))))
9+
}
10+
11+
#[allow(unreachable_code)]
12+
#[tokio::main]
13+
async fn main() -> anyhow::Result<()> {
14+
tracing_subscriber::fmt::init();
15+
16+
let listener = TcpListener::bind("127.0.0.1:3000").await?;
17+
18+
loop {
19+
let (io, _) = listener.accept().await?;
20+
let tokio_io = TokioIo::new(io);
21+
// let resolve_now = async {};
22+
23+
tokio::task::spawn(async move {
24+
let conn = http2::Builder::new(TokioExecutor::new())
25+
.enable_connect_protocol()
26+
.serve_connection(tokio_io, service_fn(handler));
27+
28+
let mut conn = pin!(conn);
29+
// let mut resolve_now = pin!(resolve_now);
30+
31+
loop {
32+
tokio::select! {
33+
result = conn.as_mut() => {
34+
if let Err(_err) = result {
35+
tracing::trace!("failed to serve connection: {_err:#}");
36+
}
37+
break;
38+
}
39+
// _ = &mut resolve_now => {
40+
// tracing::trace!("signal received in task, starting graceful shutdown");
41+
// conn.as_mut().graceful_shutdown();
42+
// }
43+
}
44+
}
45+
46+
Ok::<_, anyhow::Error>(())
47+
});
48+
}
49+
Ok(())
50+
}

crates/codora-framework-bot/src/adapter/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
pub mod discord;
33

44
#[cfg(feature = "codora-framework-bot-telegram")]
5+
/// Telegram
6+
///
57
pub mod telegram;

crates/codora-framework-worker/src/lib.rs renamed to crates/codora-framework-bot/src/adapter/telegram/data/message/mod.rs

File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod message;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pub trait Handler {
2+
type Error;
3+
4+
fn handle(&self) -> impl Future<Output = Result<(), Self::Error>>;
5+
}
6+
7+
#[derive(Debug)]
8+
pub struct HandlerFromFunc<F> {
9+
handler: F,
10+
// interceptor and other's goes in here
11+
}
12+
13+
pub fn handler_from_func<F, Fut>(func: F) -> HandlerFromFunc<F>
14+
where
15+
F: FnOnce() -> Fut,
16+
// This is just a template fix later
17+
Fut: Future<Output = Result<(), &'static str>>,
18+
{
19+
HandlerFromFunc { handler: func }
20+
}
21+
22+
impl<T> Handler for HandlerFromFunc<T> {
23+
// Change this to valid error
24+
type Error = &'static str;
25+
26+
async fn handle(&self) -> Result<(), Self::Error> {
27+
todo!()
28+
}
29+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub fn from_fn<F, Future>(func: F)
2+
where
3+
F: FnOnce() -> Future,
4+
Future: std::future::Future<Output = ()>,
5+
{
6+
todo!()
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub(super) mod from_fn;

0 commit comments

Comments
 (0)