-
Notifications
You must be signed in to change notification settings - Fork 1
test: remove sample images #20
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
Closed
Closed
Changes from all commits
Commits
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,6 @@ Cargo.lock | |
| /target | ||
|
|
||
| #enviroment variables | ||
| .env | ||
| .env | ||
| # swagger ui | ||
| swagger-ui.zip | ||
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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Rust Base Backend</title> | ||
| </head> | ||
| <body> | ||
| <h1>Static file served</h1> | ||
| </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,75 @@ | ||
|
|
||
| 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::swagger; | ||
| use crate::swagger::serve_swagger; | ||
| use crate::errors; | ||
| 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 (_, 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)) | ||
| } | ||
Oops, something went wrong.
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 should be simplified to use
warp::reply::jsondirectly without the unnecessary async block and Result wrapping.