Skip to content

Commit

Permalink
add query
Browse files Browse the repository at this point in the history
  • Loading branch information
BERADQ committed Mar 26, 2024
1 parent b3afee1 commit bbf0c27
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/plugin
25 changes: 7 additions & 18 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 @@ -7,7 +7,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kokoro = "0.0.5"
kokoro = { version = "0.0.6", path = "../kokoro" }
serde = { version = "1.0.196", features = ["derive"] }
kokoro-plugin-tiny-http-event = {path = "./kokoro-plugin-tiny-http-event"}

Expand Down
23 changes: 23 additions & 0 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use kokoro::{dynamic_plugin::toml::toml, prelude::*};
use kokoro_plugin_tiny_http_event::{http::Response, *};
fn main() -> Result<()> {
let ctx = channel_ctx();
let pf = PluginFinder::new("./plugin");
let plugin = pf.find("kokoro_plugin_tiny_http");
let config = toml! {
host = "0.0.0.0"
port = 1145
};
ctx.plugin_dynamic(plugin, Some(config.into()))?;
ctx.subscribe(hello);
ctx.run_sync();

Ok(())
}

path!(Hello, "/hello");
fn hello(req: PathQuery<Hello>) {
if let Some(req) = req.take() {
req.respond(Response::from_string("Hello World!")).unwrap();
}
}
2 changes: 1 addition & 1 deletion kokoro-plugin-tiny-http-event/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kokoro = "0.0.5"
kokoro = { version = "0.0.6", path = "../../kokoro" }
tiny_http = "0.12.0"
49 changes: 47 additions & 2 deletions kokoro-plugin-tiny-http-event/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use http::{HTTPVersion, Header, Method, Response};
use kokoro::prelude::*;
use std::{io::Read, net::SocketAddr, ops::Deref, sync::RwLock};
use kokoro::{core::query::Query, prelude::*};
use std::{
io::Read,
marker::PhantomData,
net::SocketAddr,
ops::Deref,
sync::{Arc, RwLock},
};
pub use tiny_http as http;
use tiny_http::Request;
#[derive(Event)]
Expand Down Expand Up @@ -51,4 +57,43 @@ impl HttpRequest {
}
}

pub trait Path {
const P: &'static str;
}
pub struct PathQuery<P: Path> {
event: Arc<HttpRequest>,
_p: PhantomData<P>,
}
impl<P: Path> Deref for PathQuery<P> {
type Target = HttpRequest;

fn deref(&self) -> &Self::Target {
&self.event
}
}
impl<P: Path> Query<HttpRequest> for PathQuery<P> {
fn create(n: Arc<HttpRequest>) -> Self {
Self {
event: n,
_p: PhantomData,
}
}

fn sub(n: &dyn Event) -> bool {
if let Some(e) = n.downcast_ref::<HttpRequest>() {
e.url == P::P
} else {
false
}
}
}
#[macro_export]
macro_rules! path {
($name:ident,$path:expr) => {
#[derive(Debug)]
struct $name;
impl Path for $name {
const P: &'static str = $path;
}
};
}
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use kokoro::{default_impl::plugin::anyhow::anyhow, prelude::*};
use kokoro_plugin_tiny_http_event::*;
use serde::Deserialize;
#[derive(Deserialize, DynamicPlugin)]
struct TinyHttp {
host: Option<String>,
port: Option<u32>,
pub struct TinyHttp {
pub host: Option<String>,
pub port: Option<u32>,
}
impl Create for TinyHttp {
fn create(config: Option<toml::Value>) -> Result<Self> {
Expand All @@ -27,7 +27,7 @@ impl Plugin for TinyHttp {
let addr = format!("{host}:{port}");
let client = http::Server::http(addr.clone()).or(Err(anyhow!("服务启动失败")))?;
ctx.spawn(move |ctx, s| {
while !s.is() {
for _ in s {
let r = client.recv();
if let Ok(rq) = r {
ctx.publish(HttpRequest::new(rq));
Expand Down

0 comments on commit bbf0c27

Please sign in to comment.