Skip to content

Commit

Permalink
feat: program on layer
Browse files Browse the repository at this point in the history
  • Loading branch information
edinsonjim committed Feb 23, 2025
1 parent beb7664 commit 2bd3cc0
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 29 deletions.
17 changes: 6 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ serde = { version = "1.0.215", features = ["derive"] }
axum = { version = "0.7" }
thiserror = { version = "1.0" }
tower-http = { version = "0.6.2", features = ["trace"] }
tower = { version = "0.5.2", features = ["util"] }
lumx_core = { path = "./lumx_core" }
lumx_axum = { path = "./lumx_axum" }
1 change: 1 addition & 0 deletions lumx_axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ tokio = { workspace = true }
tracing = { workspace = true }
serde = { workspace = true }
tower-http = { workspace = true, features = ["trace"] }
tower = { workspace = true }
1 change: 1 addition & 0 deletions lumx_axum/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod router;
pub use axum;
pub mod extractor;
pub mod middleware;
pub mod plugin;
pub mod program;
pub mod state;
Expand Down
1 change: 1 addition & 0 deletions lumx_axum/src/middleware/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod state;
52 changes: 52 additions & 0 deletions lumx_axum/src/middleware/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use axum::extract::Request;
use lumx_core::program::Program;
use std::sync::Arc;
use std::task::{Context, Poll};
use tower::{Layer, Service};

#[derive(Clone)]
pub struct StateLayer {
program: Arc<Program>,
}

impl StateLayer {
pub fn new(program: Arc<Program>) -> Self {
Self { program }
}
}

impl<S> Layer<S> for StateLayer {
type Service = StateLayerService<S>;

fn layer(&self, inner: S) -> Self::Service {
StateLayerService {
inner,
program: self.program.clone(),
}
}
}

#[derive(Clone)]
pub struct StateLayerService<S> {
inner: S,
program: Arc<Program>,
}

impl<S, B> Service<Request<B>> for StateLayerService<S>
where
S: Service<Request<B>>,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}

fn call(&mut self, mut req: Request<B>) -> Self::Future {
req.extensions_mut().insert(self.program.clone());

self.inner.call(req)
}
}
9 changes: 7 additions & 2 deletions lumx_axum/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::{router::RouterRef, state::AppState};
use axum::{async_trait, Extension, Router};
use lumx_core::{
plugable::plugin::Plugin,
Expand All @@ -10,6 +9,9 @@ use std::{net::SocketAddr, sync::Arc};
use tower_http::trace::TraceLayer;
use tracing::debug;

use crate::middleware::state::StateLayer;
use crate::{router::RouterRef, state::AppState};

pub struct WebPlugin;

#[async_trait]
Expand All @@ -35,7 +37,10 @@ impl WebPlugin {

debug!(?router, "registered routes");
let router = router
.layer(Extension(AppState { app }))
.layer(Extension(AppState {
app: Arc::clone(&app),
}))
.layer(StateLayer::new(Arc::clone(&app)))
.layer(TraceLayer::new_for_http());

println!("Listening on {}", listener.local_addr().unwrap());
Expand Down
2 changes: 1 addition & 1 deletion lumx_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod plugable;
pub mod program;
pub mod scheduler;
pub mod tracing;
pub mod tracer;
pub mod types;

pub use async_trait;
Expand Down
15 changes: 11 additions & 4 deletions lumx_core/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
plugin::{Plugin, PluginRef},
},
scheduler::Scheduler,
tracer,
types::ProgramFailure,
};

Expand Down Expand Up @@ -217,10 +218,13 @@ impl ProgramBuilder {
// 1. read env variables
dotenvy::dotenv().ok();

// 2. build plugins
// 2. init and subscribe tracer
tracer::init();

// 3. build plugins
self.build_plugins().await;

// 3. schedule
// 4. schedule
self.schedule().await
}

Expand All @@ -230,10 +234,13 @@ impl ProgramBuilder {
// 1. read env variables
dotenvy::dotenv().ok();

// 2. build plugins
// 2. init and subscribe tracer
tracer::init();

// 3. build plugins
self.build_plugins().await;

// 3. build program
// 4. build program
self.build_program()
}

Expand Down
11 changes: 11 additions & 0 deletions lumx_core/src/tracer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

pub fn init() {
tracing_subscriber::registry()
.with(
EnvFilter::try_from_default_env()
.unwrap_or_else(|_| format!("{}=info", env!("CARGO_CRATE_NAME")).into()),
)
.with(fmt::layer())
.init();
}
11 changes: 0 additions & 11 deletions lumx_core/src/tracing.rs

This file was deleted.

0 comments on commit 2bd3cc0

Please sign in to comment.