-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove redundant to_string calls, use ? for option
- Loading branch information
Showing
13 changed files
with
147 additions
and
119 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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,79 @@ | ||
use actix_web::dev::Server; | ||
use actix_web::web::Data; | ||
use actix_web::{App, HttpServer}; | ||
use mongodb::options::{ClientOptions, ServerApi, ServerApiVersion}; | ||
use mongodb::{Client, Collection}; | ||
use std::net::SocketAddr; | ||
use std::sync::Arc; | ||
use utoipa::OpenApi; | ||
use utoipa_swagger_ui::SwaggerUi; | ||
|
||
use crate::conf::AppConf; | ||
use app::endpoints::{create_user, get_user}; | ||
use app::models::*; | ||
use domain::commands::{CreateUserCommand, ICommandHandler}; | ||
use domain::queries::{GetUserQuery, IQueryHandler, User}; | ||
use domain_impl::handlers::{CreateUserCommandHandler, GetUserQueryHandler}; | ||
use infra::repositories::UserRepository; | ||
|
||
#[derive(OpenApi)] | ||
#[openapi( | ||
paths(app::endpoints::create_user, app::endpoints::get_user), | ||
components( | ||
schemas(CreateUserRequest, CreatedUserIdResponse, ErrorResponse), | ||
schemas(UserResponse, ErrorResponse) | ||
) | ||
)] | ||
struct ApiDoc; | ||
pub struct Composition { | ||
pub server: Server, | ||
pub addrs: Vec<SocketAddr>, | ||
} | ||
|
||
impl Composition { | ||
pub async fn new(conf: &AppConf) -> Result<Composition, std::io::Error> { | ||
let user_repository_arc = Arc::new(create_user_repository(conf).await); | ||
let command_handler: Arc<dyn ICommandHandler<CreateUserCommand, Option<String>>> = | ||
Arc::new(CreateUserCommandHandler { | ||
repo: user_repository_arc.clone(), | ||
}); | ||
let query_handler: Arc<dyn IQueryHandler<GetUserQuery, Option<User>>> = | ||
Arc::new(GetUserQueryHandler { | ||
repo: user_repository_arc.clone(), | ||
}); | ||
|
||
let openapi = ApiDoc::openapi(); | ||
|
||
let addr = conf.get_api_address(); | ||
let http_server = HttpServer::new(move || { | ||
App::new() | ||
.service( | ||
SwaggerUi::new("/swagger-ui/{_:.*}") | ||
.url("/api-docs/openapi.json", openapi.clone()), | ||
) | ||
.service(create_user) | ||
.service(get_user) | ||
.app_data(Data::from(command_handler.clone())) | ||
.app_data(Data::from(query_handler.clone())) | ||
}) | ||
.bind(&addr) | ||
.unwrap_or_else(|_| panic!("Failed to bind to the host: {}", &addr)); | ||
|
||
let addrs = http_server.addrs(); | ||
let server = http_server.run(); | ||
|
||
Ok(Self { server, addrs }) | ||
} | ||
} | ||
|
||
async fn create_user_repository(conf: &AppConf) -> UserRepository { | ||
let mut client_options = ClientOptions::parse(&conf.db_uri).await.unwrap(); | ||
|
||
let server_api = ServerApi::builder().version(ServerApiVersion::V1).build(); | ||
client_options.server_api = Some(server_api); | ||
|
||
let client = Client::with_options(client_options).unwrap(); | ||
|
||
let collection: Collection<User> = client.database(&conf.db_name).collection("users"); | ||
UserRepository::new(collection) | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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,4 +1,4 @@ | ||
#[path = "composition.rs"] | ||
pub mod composition; | ||
#[path = "conf.rs"] | ||
pub mod conf; | ||
#[path = "factory.rs"] | ||
pub mod factory; |
This file contains 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,11 +1,11 @@ | ||
use host::conf::read_app_conf; | ||
use host::factory::create_server; | ||
use host::composition::Composition; | ||
use host::conf::AppConf; | ||
|
||
#[actix_web::main] | ||
async fn main() -> std::io::Result<()> { | ||
std::env::set_var("RUST_LOG", "debug"); | ||
env_logger::init(); | ||
|
||
let conf = read_app_conf(); | ||
create_server(&conf).await.unwrap().server.await | ||
let conf = AppConf::new(); | ||
Composition::new(&conf).await?.server.await | ||
} |
This file contains 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 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