-
Notifications
You must be signed in to change notification settings - Fork 1
fix: use dynamic ports in tests to avoid port conflicts #21
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
Merged
LuigimonSoft
merged 2 commits into
master
from
codex/add-static-file-server-support-h89jsl
Aug 18, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,4 +19,9 @@ Cargo.lock | |
| /target | ||
|
|
||
| #enviroment variables | ||
| .env | ||
| .env | ||
|
|
||
| # MacOs Finder files | ||
| .DS_Store | ||
| ._* | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Rust Base Backend</title> | ||
| </head> | ||
| <body> | ||
| <h1>Static file served</h1> | ||
| <img src="/image.jpg" alt="Logo" style="width: 200px; height: auto;"> | ||
| <p>Welcome to the Rust Base Backend static file server!</p> | ||
| <p>This page is served from the <code>public/index.html</code> file.</p> | ||
| <p>Check the <code>.gitignore</code> file for ignored files in this project.</p> | ||
| <p>Enjoy your stay!</p> | ||
| <footer> | ||
| <p>© 2023 Rust Base Backend</p> | ||
| <img src="/image.png" alt="Logo" style="width: 50px; height: auto;"> | ||
| <p>Powered by Rust</p> | ||
| </footer> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,21 @@ | ||
| use std::env; | ||
|
|
||
| pub struct Config { | ||
| pub port: u16, | ||
| pub api_base: String | ||
| pub port: u16, | ||
| pub api_base: String, | ||
| pub static_dir: String, | ||
| } | ||
|
|
||
| impl Config { | ||
| pub fn from_env() -> Self { | ||
| Self { | ||
| port: env::var("PORT") | ||
| .unwrap_or_else(|_| "3030".to_string()) | ||
| .parse() | ||
| .expect("PORT must be a number"), | ||
| api_base : env::var("API_BASE").unwrap_or_else(|_| "/api/v1".trim_matches('/').to_string()) | ||
| pub fn from_env() -> Self { | ||
| Self { | ||
| port: env::var("PORT") | ||
| .unwrap_or_else(|_| "3030".to_string()) | ||
| .parse() | ||
| .expect("PORT must be a number"), | ||
| api_base: env::var("API_BASE") | ||
| .unwrap_or_else(|_| "/api/v1".trim_matches('/').to_string()), | ||
| static_dir: env::var("STATIC_DIR").unwrap_or_else(|_| "public".to_string()), | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,66 +1,73 @@ | ||
|
|
||
| use dotenv::dotenv; | ||
| use std::sync::Arc; | ||
| use crate::swagger::serve_swagger; | ||
| use tokio::sync::oneshot; | ||
| use crate::config; | ||
| use crate::controllers; | ||
| use crate::swagger; | ||
|
|
||
|
|
||
| use utoipa::{ | ||
| openapi::security::{ApiKey, ApiKeyValue, SecurityScheme}, | ||
| Modify, OpenApi, openapi::Server | ||
| }; | ||
| use utoipa_swagger_ui::Config; | ||
| use warp::{ | ||
| http::Uri, | ||
| hyper::{Response, StatusCode}, | ||
| path::{FullPath, Tail}, | ||
| Filter, Rejection, Reply, | ||
| }; | ||
|
|
||
| pub async fn run_server() -> (oneshot::Sender<()>, String) { | ||
| dotenv().ok(); | ||
|
|
||
| let config = Arc::new(config::Config::from_env()); | ||
| let config_swagger = Arc::new(Config::from(format!("/{}/api-doc.json", config.api_base))); | ||
| let routes = controllers::base_routes(Arc::clone(&config)); | ||
|
|
||
| let port:u16 = config.port; | ||
| let api_base = config.api_base.trim_matches('/').to_string(); | ||
| let api_segments: Vec<String> = api_base.split('/').map(|s| s.to_string()).collect(); | ||
|
|
||
| let mut api_path = warp::path(api_segments[0].clone()).boxed(); | ||
| for segment in &api_segments[1..] { | ||
| api_path = api_path.and(warp::path(segment.clone())).boxed(); | ||
| } | ||
|
|
||
| let api_doc = api_path.clone() | ||
| .and(warp::path("api-doc.json")) | ||
| .and(warp::get()) | ||
| .map(|| warp::reply::json(&swagger::ApiDoc::openapi())); | ||
|
|
||
| let swagger_ui = api_path.clone() | ||
| .and(warp::path("swagger-ui")) | ||
| .and(warp::get()) | ||
| .and(warp::path::full()) | ||
| .and(warp::path::tail()) | ||
| .and(warp::any().map(move || config_swagger.clone())) | ||
| .and_then(serve_swagger); | ||
|
|
||
| let (tx, rx) = oneshot::channel(); | ||
| let routes = api_doc.or(swagger_ui).or(routes); | ||
|
|
||
| let (_, server) = warp::serve(routes) | ||
| .bind_with_graceful_shutdown(([127,0,0,1], port), async { | ||
| rx.await.ok(); | ||
| }); | ||
|
|
||
| tokio::spawn(server); | ||
|
|
||
| println!("Server starting on port {}", port); | ||
| println!("API base path: /{}", config.api_base); | ||
|
|
||
| (tx, format!("http://127.0.0.1:{}", port)) | ||
| } | ||
| use crate::config; | ||
| use crate::controllers; | ||
| use crate::errors; | ||
| use crate::swagger; | ||
| use crate::swagger::serve_swagger; | ||
| use dotenv::dotenv; | ||
| use std::sync::Arc; | ||
| use tokio::sync::oneshot; | ||
|
|
||
| use utoipa::{ | ||
| openapi::security::{ApiKey, ApiKeyValue, SecurityScheme}, | ||
| openapi::Server, | ||
| Modify, OpenApi, | ||
| }; | ||
| use utoipa_swagger_ui::Config; | ||
| use warp::{ | ||
| http::Uri, | ||
| hyper::{Response, StatusCode}, | ||
| path::{FullPath, Tail}, | ||
| Filter, Rejection, Reply, | ||
| }; | ||
|
|
||
| pub async fn run_server() -> (oneshot::Sender<()>, String) { | ||
| dotenv().ok(); | ||
|
|
||
| let config = Arc::new(config::Config::from_env()); | ||
| let config_swagger = Arc::new(Config::from(format!("/{}/api-doc.json", config.api_base))); | ||
| let routes = controllers::base_routes(Arc::clone(&config)); | ||
| let static_files = warp::fs::dir(config.static_dir.clone()); | ||
|
|
||
| let port: u16 = config.port; | ||
| let api_base = config.api_base.trim_matches('/').to_string(); | ||
| let api_segments: Vec<String> = api_base.split('/').map(|s| s.to_string()).collect(); | ||
|
|
||
| let mut api_path = warp::path(api_segments[0].clone()).boxed(); | ||
| for segment in &api_segments[1..] { | ||
| api_path = api_path.and(warp::path(segment.clone())).boxed(); | ||
| } | ||
|
|
||
| let api_doc = api_path | ||
| .clone() | ||
| .and(warp::path("api-doc.json")) | ||
| .and(warp::get()) | ||
| .and_then(|| async { Ok::<_, Rejection>(warp::reply::json(&swagger::ApiDoc::openapi())) }); | ||
|
|
||
| let swagger_ui = api_path | ||
| .clone() | ||
| .and(warp::path("swagger-ui")) | ||
| .and(warp::get()) | ||
| .and(warp::path::full()) | ||
| .and(warp::path::tail()) | ||
| .and(warp::any().map(move || config_swagger.clone())) | ||
| .and_then(serve_swagger); | ||
|
|
||
| let (tx, rx) = oneshot::channel(); | ||
| let routes = api_doc | ||
| .or(swagger_ui) | ||
| .or(routes) | ||
| .or(static_files) | ||
| .recover(errors::handle_rejection); | ||
|
|
||
| let (addr, server) = | ||
| warp::serve(routes).bind_with_graceful_shutdown(([127, 0, 0, 1], port), async { | ||
| rx.await.ok(); | ||
| }); | ||
|
|
||
| tokio::spawn(server); | ||
|
|
||
| println!("Server starting on port {}", addr.port()); | ||
| println!("API base path: /{}", config.api_base); | ||
|
|
||
| (tx, format!("http://127.0.0.1:{}", addr.port())) | ||
| } | ||
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The closure is unnecessarily complex. Since
warp::reply::json()already returns a valid reply, you can simplify this to.map(|| warp::reply::json(&swagger::ApiDoc::openapi()))