-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: load static assets manually so all other requests rely on wasm_handler #226
Conversation
@@ -25,7 +25,7 @@ pub async fn handle_assets(req: &HttpRequest) -> Result<NamedFile, Error> { | |||
// Same as before, but the file is located at ./about.html | |||
let html_ext_path = root_path.join(format!("public{uri_path}.html")); | |||
|
|||
if file_path.exists() { | |||
if file_path.exists() && !uri_path.is_empty() && uri_path != "/" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this new condition to avoid returning the public
folder information.
if let Some(route) = selected_route { | ||
// First, check if there's an existing static file. Static assets have more priority | ||
// than dynamic routes. However, I cannot set the static assets as the first service | ||
// as it's captures everything. | ||
if route.is_dynamic() { | ||
if let Ok(existing_file) = handle_assets(&req).await { | ||
return existing_file.into_response(&req); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're explicitly mounting the static files in certain routes, this is not required anymore.
let workers = WORKERS | ||
.read() | ||
.expect("error locking worker lock for reading"); | ||
|
||
// Configure the KV store when required | ||
for route in serve_options.base_routes.routes.iter() { | ||
let worker = workers | ||
.get(&route.worker) | ||
.expect("unexpected missing worker"); | ||
|
||
// Configure KV | ||
if let Some(namespace) = worker.config.data_kv_namespace() { | ||
data.kv.create_store(&namespace); | ||
} | ||
} | ||
|
||
// Pre-create the KV namespaces | ||
let data_connectors = Data::new(RwLock::new(data)); | ||
|
||
// Static assets | ||
let mut static_assets = | ||
StaticAssets::new(&serve_options.root_path, &serve_options.base_routes.prefix); | ||
static_assets | ||
.load() | ||
.expect("Error loading the static assets"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I extracted all the initialization befure the HttpServer::new
call. Actix runs this block on every thread, so we avoid calculating static assets multiple times.
@ereslibre don't review this yet, as I need to rethink about this issue. I may close this PR and open a more well-defined issue with a proper set of goals and definition |
For now, I'm going to close this PR! |
This is the first PR to complete #175. Before this PR, the default service that captured any non-processed request was the
actix-files
service (viaFiles
). Since we wanted to mount static files into/
,Files
was capturing al/.*
routes. For workers,wws
was mounting their routes explicitly.In #175, our intention is the opposite. We want
workers_handler
to manage any unknown route, so we can add new routes when the server is running. My first approach was to find a way to pass a request toworker_handler
fromFiles
using thedefault_handler
. TheFiles
service triggers this handler when it doesn't find any matching file. However, the types didn't match and it was an extra step before processing the request.For this reason, I changed the approach and set all the static assets routes manually. Then,
wws
sends any unknown route to thewasm_handler
to detect if there's any worker that manages it. In fact, this is a more correct approach as static files have a higher priority than workers (like dynamic).