Skip to content

Commit

Permalink
engine macro
Browse files Browse the repository at this point in the history
  • Loading branch information
levkk committed Oct 30, 2024
1 parent d1dedd2 commit 5731f91
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 6 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ members = [
"rwf-macros",
"rwf-tests",
"examples/django",
"examples/request-tracking",
"examples/request-tracking", "examples/engine",
]
8 changes: 8 additions & 0 deletions examples/engine/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "engine"
version = "0.1.0"
edition = "2021"

[dependencies]
rwf = { path = "../../rwf" }
tokio = { version = "1", features = ["full"] }
26 changes: 26 additions & 0 deletions examples/engine/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use rwf::{
controller::Engine,
http::{self},
prelude::*,
Server,
};

#[derive(Default)]
struct Index;

#[async_trait]
impl Controller for Index {
async fn handle(&self, _request: &Request) -> Result<Response, Error> {
Ok(Response::new().text("Engine"))
}
}

#[tokio::main]
async fn main() -> Result<(), http::Error> {
Logger::init();

let engine = Engine::new(vec![route!("/index" => Index)]);
Server::new(vec![engine!("/engine" => engine)])
.launch("0.0.0.0:8000")
.await
}
2 changes: 1 addition & 1 deletion rwf-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ pub fn engine(input: TokenStream) -> TokenStream {
let engine = iter.next().unwrap();

quote! {
#engine.remount(rwf::http::Path::parse(route).unwrap()).wildcard(#route)
#engine.remount(&rwf::http::Path::parse(#route).unwrap()).wildcard(#route)
}
.into()
}
Expand Down
4 changes: 1 addition & 3 deletions rwf/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ impl Default for Config {
tty: std::io::stderr().is_terminal(),
default_auth: AuthHandler::new(AllowAll {}),
session_duration: Duration::days(4),
default_middleware: MiddlewareSet::without_default(vec![
RequestTracker::new().middleware()
]),
default_middleware: MiddlewareSet::without_default(vec![]),
cache_templates,
websocket: Websocket::default(),
log_queries: var("RWF_LOG_QUERIES").is_ok(),
Expand Down
3 changes: 3 additions & 0 deletions rwf/src/controller/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ pub struct Engine {
}

impl Engine {
/// Create new engine for the given routes.
pub fn new(handlers: Vec<Handler>) -> Self {
Self {
router: Router::new(handlers).unwrap(),
mount: Path::parse("/").unwrap(),
}
}

/// Move the engine to this mount point.
pub fn remount(mut self, mount: &Path) -> Self {
self.mount = mount.clone();
self
}

/// Get the engine mount point.
pub fn mount(&self) -> &Path {
&self.mount
}
Expand Down
2 changes: 1 addition & 1 deletion rwf/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ pub use async_trait::async_trait;
pub use time::{Duration, OffsetDateTime};
pub use tokio;

pub use macros::{context, crud, render, rest, route, engine};
pub use macros::{context, crud, engine, render, rest, route};
pub use rwf_macros as macros;

0 comments on commit 5731f91

Please sign in to comment.