这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 105 additions & 1 deletion 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 @@ -25,7 +25,7 @@ lazy_static = "1.4.0"
env_logger = "0.9.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.85"
glob = "0.3.0"
wax = "0.5.0"
toml = "0.5.9"
base64 = "0.13.1"
clap = { version = "4.0.10", features = ["derive"] }
Expand Down
12 changes: 4 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async fn wasm_handler(req: HttpRequest, body: Bytes) -> HttpResponse {
let result: HttpResponse;

// First, we need to identify the best suited route
let selected_route = router::retrieve_best_route(routes, req.path());
let selected_route = routes.retrieve_best_route(req.path());

if let Some(route) = selected_route {
// First, check if there's an existing static file. Static assets have more priority
Expand Down Expand Up @@ -209,13 +209,9 @@ async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();

// Initialize the prefix
let prefix = router::format_prefix(&args.prefix);

// Initialize the routes
println!("⚙️ Loading routes from: {}", &args.path.display());
let routes = Data::new(Routes {
routes: router::initialize_routes(&args.path, &prefix),
});
let routes = Data::new(Routes::new(&args.path, &args.prefix));

let data = Data::new(RwLock::new(DataConnectors { kv: KV::new() }));

Expand Down Expand Up @@ -259,7 +255,7 @@ async fn main() -> std::io::Result<()> {
}

// Serve static files from the static folder
let mut static_prefix = prefix.clone();
let mut static_prefix = routes.prefix.clone();
if static_prefix.is_empty() {
static_prefix = String::from("/");
}
Expand Down
101 changes: 101 additions & 0 deletions src/router/files.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::ffi::OsStr;
use std::path::{Component, Path, PathBuf};
use wax::{Glob, WalkEntry};

/// Manages the files associated to a Wasm Workers Run.
/// It uses glob patterns to detect the workers and
/// provide utilities to work with public folders and
/// other related resources.
pub struct Files {
root: PathBuf,
has_public: bool,
}

impl Files {
/// Initializes a new files instance. It will detect
/// relevant resources for WWS like the public folder.
pub fn new(root: &Path) -> Self {
Self {
root: root.to_path_buf(),
has_public: root.join(Path::new("public")).exists(),
}
}

/// Walk through all the different files associated to this
/// project using a Glob pattern
pub fn walk(&self) -> Vec<WalkEntry> {
let glob =
Glob::new("**/*.{wasm,js}").expect("Failed to read the files in the current directory");

glob.walk(&self.root)
.filter_map(|el| match el {
Ok(entry) if !self.is_in_public_folder(entry.path()) => Some(entry),
_ => None,
})
.collect()
}

/// Checks if the given filepath is inside the "public" folder.
/// It will return an early false if the project doesn't have
/// a public folder.
fn is_in_public_folder(&self, path: &Path) -> bool {
if !self.has_public {
return false;
}

path.components().any(|c| match c {
Component::Normal(os_str) => os_str == OsStr::new("public"),
_ => false,
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[cfg(not(target_os = "windows"))]
#[test]
fn unix_is_in_public_folder() {
let tests = [
("public/index.js", true),
("examples/public/index.js", true),
("examples/public/other.js", true),
("public.js", false),
("examples/public.js", false),
("./examples/public.js", false),
("./examples/index.js", false),
];

for t in tests {
assert_eq!(
Files::new(Path::new("./tests/data")).is_in_public_folder(Path::new(t.0)),
t.1
)
}
}

#[cfg(target_os = "windows")]
#[test]
fn win_is_in_public_folder() {
let tests = [
("public\\index.js", true),
("examples\\public\\index.js", true),
("examples\\public\\other.js", true),
("public.js", false),
("examples\\public.js", false),
(".\\examples\\public.js", false),
(".\\examples\\index.js", false),
];

for t in tests {
assert_eq!(
Files::new(Path::new(".\\tests\\data")).is_in_public_folder(Path::new(t.0)),
t.1
)
}
}
}
8 changes: 8 additions & 0 deletions src/router/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

mod files;
mod route;
mod routes;

pub(crate) use routes::Routes;
Loading