Skip to content

Commit 730447f

Browse files
committed
wip: found issue on tokio blockon
1 parent 9ffa604 commit 730447f

File tree

11 files changed

+226
-2204
lines changed

11 files changed

+226
-2204
lines changed

Cargo.lock

Lines changed: 81 additions & 2151 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
[workspace]
22
members = [
3-
"crates/file-explorer",
4-
"crates/file-explorer-plugin",
5-
"crates/file-explorer-proto",
6-
"crates/file-explorer-ui",
3+
# "crates/file-explorer",
4+
# "crates/file-explorer-plugin",
5+
# "crates/file-explorer-proto",
6+
# "crates/file-explorer-ui",
7+
"crates/hello-world-plugin",
78
"crates/http-server",
89
"crates/http-server-plugin",
910
]

crates/file-explorer-plugin/src/lib.rs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct FileExplorerPlugin {
6262

6363
#[async_trait]
6464
impl Plugin for FileExplorerPlugin {
65-
async fn call(&self, parts: Parts, body: Bytes) -> Result<Response<Full<Bytes>>, PluginError> {
65+
fn call(&self, parts: Parts, body: Bytes) -> Result<Response<Full<Bytes>>, PluginError> {
6666
self.rt
6767
.block_on(async move { self.handle(parts, body).await })
6868
}
@@ -84,37 +84,38 @@ impl FileExplorerPlugin {
8484
parts: Parts,
8585
body: Bytes,
8686
) -> Result<Response<Full<Bytes>>, PluginError> {
87-
tracing::info!("Handling request: {:?}", parts);
88-
89-
if parts.uri.path().starts_with("/api/v1") {
90-
self.handle_api(parts, body).await
91-
} else {
92-
let path = parts.uri.path();
93-
let path = path.strip_prefix('/').unwrap_or(path);
94-
95-
if let Some(file) = Assets::get(path) {
96-
let content_type = mime_guess::from_path(path).first_or_octet_stream();
97-
let content_type = HeaderValue::from_str(content_type.as_ref()).unwrap();
98-
let body = Full::new(Bytes::from(file.data.to_vec()));
99-
let mut response = Response::new(body);
100-
let mut headers = response.headers().clone();
101-
102-
headers.append(CONTENT_TYPE, content_type);
103-
*response.headers_mut() = headers;
104-
105-
return Ok(response);
106-
}
107-
108-
let index = Assets::get("index.html").unwrap();
109-
let body = Full::new(Bytes::from(index.data.to_vec()));
110-
let mut response = Response::new(body);
111-
let mut headers = response.headers().clone();
112-
113-
headers.append(CONTENT_TYPE, "text/html".try_into().unwrap());
114-
*response.headers_mut() = headers;
115-
116-
Ok(response)
117-
}
87+
Ok(Response::new(Full::new(Bytes::from("Unsupported method"))))
88+
// tracing::info!("Handling request: {:?}", parts);
89+
90+
// if parts.uri.path().starts_with("/api/v1") {
91+
// self.handle_api(parts, body).await
92+
// } else {
93+
// let path = parts.uri.path();
94+
// let path = path.strip_prefix('/').unwrap_or(path);
95+
96+
// if let Some(file) = Assets::get(path) {
97+
// let content_type = mime_guess::from_path(path).first_or_octet_stream();
98+
// let content_type = HeaderValue::from_str(content_type.as_ref()).unwrap();
99+
// let body = Full::new(Bytes::from(file.data.to_vec()));
100+
// let mut response = Response::new(body);
101+
// let mut headers = response.headers().clone();
102+
103+
// headers.append(CONTENT_TYPE, content_type);
104+
// *response.headers_mut() = headers;
105+
106+
// return Ok(response);
107+
// }
108+
109+
// let index = Assets::get("index.html").unwrap();
110+
// let body = Full::new(Bytes::from(index.data.to_vec()));
111+
// let mut response = Response::new(body);
112+
// let mut headers = response.headers().clone();
113+
114+
// headers.append(CONTENT_TYPE, "text/html".try_into().unwrap());
115+
// *response.headers_mut() = headers;
116+
117+
// Ok(response)
118+
// }
118119
}
119120

120121
async fn handle_api(
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "hello-world-plugin"
3+
version = "0.0.0"
4+
authors = ["Esteban Borai <[email protected]>"]
5+
edition = "2021"
6+
publish = false
7+
8+
[lib]
9+
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
anyhow = { workspace = true }
13+
async-trait = { workspace = true }
14+
bytes = { workspace = true }
15+
futures = { workspace = true }
16+
http = { workspace = true }
17+
http-body-util = { workspace = true }
18+
hyper = { workspace = true }
19+
serde = { workspace = true, features = ["derive"] }
20+
serde_json = { workspace = true }
21+
tokio = { workspace = true, features = ["fs", "rt-multi-thread", "signal", "macros"] }
22+
tracing = { workspace = true }
23+
tracing-subscriber = { workspace = true, features = ["env-filter"] }
24+
25+
http-server-plugin = { workspace = true }

crates/hello-world-plugin/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.PHONY: default
2+
3+
default:
4+
@echo No default target.
5+
6+
release:
7+
@echo Building hello-world-plugin
8+
cargo b --release
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use std::path::PathBuf;
2+
use std::sync::Arc;
3+
4+
use anyhow::Result;
5+
use async_trait::async_trait;
6+
use http::request::Parts;
7+
use http_body_util::Full;
8+
use hyper::body::Bytes;
9+
use hyper::Response;
10+
use tokio::runtime::Handle;
11+
12+
use http_server_plugin::{export_plugin, Plugin, PluginError, PluginRegistrar};
13+
14+
export_plugin!(register);
15+
16+
const PLUGIN_NAME: &str = "hello-world";
17+
18+
#[allow(improper_ctypes_definitions)]
19+
extern "C" fn register(_: PathBuf, rt: Arc<Handle>, registrar: &mut dyn PluginRegistrar) {
20+
tracing_subscriber::fmt()
21+
.with_max_level(tracing::Level::INFO)
22+
.init();
23+
24+
registrar.register_function(
25+
PLUGIN_NAME,
26+
Arc::new(HelloWorldPlugin::new(rt)),
27+
);
28+
}
29+
30+
struct HelloWorldPlugin {
31+
rt: Arc<Handle>,
32+
}
33+
34+
#[async_trait]
35+
impl Plugin for HelloWorldPlugin {
36+
#[no_mangle]
37+
fn call(&self, parts: Parts, body: Bytes) -> Result<Response<Full<Bytes>>, PluginError> {
38+
println!("Test block on:");
39+
self.handle(parts, body)
40+
}
41+
}
42+
43+
impl HelloWorldPlugin {
44+
fn new(rt: Arc<Handle>) -> Self {
45+
Self {
46+
rt,
47+
}
48+
}
49+
50+
fn handle(
51+
&self,
52+
_: Parts,
53+
_: Bytes,
54+
) -> Result<Response<Full<Bytes>>, PluginError> {
55+
Ok(Response::new(Full::new(Bytes::from("Hello World"))))
56+
}
57+
}

crates/http-server-plugin/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ pub mod config;
33
use std::path::PathBuf;
44
use std::sync::Arc;
55

6-
use async_trait::async_trait;
76
use http::request::Parts;
87
use http_body_util::Full;
98
use hyper::body::Bytes;
@@ -13,9 +12,8 @@ use tokio::runtime::Handle;
1312
pub static CORE_VERSION: &str = env!("CARGO_PKG_VERSION");
1413
pub static RUSTC_VERSION: &str = env!("RUSTC_VERSION");
1514

16-
#[async_trait]
1715
pub trait Plugin: Send + Sync {
18-
async fn call(&self, parts: Parts, body: Bytes) -> Result<Response<Full<Bytes>>, PluginError>;
16+
fn call(&self, parts: Parts, body: Bytes) -> Result<Response<Full<Bytes>>, PluginError>;
1917
}
2018

2119
#[derive(Debug)]

crates/http-server/src/bin/cli/command/setup.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fs::{create_dir_all, write};
1+
use std::fs::{create_dir_all, remove_file, write};
22

33
use anyhow::Result;
44
use clap::Parser;
@@ -33,6 +33,14 @@ impl SetupOpt {
3333
"Installing File Explorer Plugin at: {}",
3434
file_explorer_plugin_path.display()
3535
);
36+
37+
if let Err(err) = remove_file(&file_explorer_plugin_path) {
38+
eprint!(
39+
"Failed to delete previous plugin version at {}\nError: {err}",
40+
file_explorer_plugin_path.display()
41+
);
42+
}
43+
3644
write(&file_explorer_plugin_path, FILE_EXPLORER_PLUGIN_BYTES)?;
3745
}
3846

crates/http-server/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub const HTTP_SERVER_HOME_DIRNAME: &str = ".http-server-rs";
99
pub const HTTP_SERVER_PLUGINS_DIRNAME: &str = "plugins";
1010
pub const DEFAULT_PLUGIN_NAME: &str = "file_explorer.plugin.httprs";
1111
pub const FILE_EXPLORER_PLUGIN_BYTES: &[u8] =
12-
include_bytes!("../inline/file_explorer.plugin.httprs");
12+
include_bytes!("../../../target/release/libhello_world_plugin.dylib");
1313

1414
pub fn install_path() -> Result<PathBuf> {
1515
let Some(home) = home_dir() else {

crates/http-server/src/server/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ impl Server {
7474
let svc = tower::service_fn(|req: Request<Incoming>| async {
7575
let (parts, body) = req.into_parts();
7676
let body = body.collect().await.unwrap().to_bytes();
77-
78-
match plugin_store.run("file-explorer", parts, body).await {
77+
println!("Exec: hello-world");
78+
match plugin_store.run("hello-world", parts, body).await {
7979
Ok(res) => Ok::<
8080
Response<http_body_util::Full<hyper::body::Bytes>>,
8181
Infallible,

0 commit comments

Comments
 (0)