Skip to content

Commit

Permalink
feat: apply clean architecture to project
Browse files Browse the repository at this point in the history
  • Loading branch information
jayden-dang committed Jul 10, 2024
1 parent dc205ad commit b84daf4
Show file tree
Hide file tree
Showing 20 changed files with 338 additions and 285 deletions.
447 changes: 209 additions & 238 deletions Cargo.lock

Large diffs are not rendered by default.

26 changes: 9 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
[package]
name = "rust-microservice"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.7.5"
config = "0.14.0"
dotenv = "0.15.0"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.118"
sqlx = { version = "0.7.4", features = ["postgres", "runtime-tokio-native-tls"] }
thiserror = "1.0.61"
tokio = { version = "1.38.0", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
uuid = { version = "1.9.1", features = ["v4", "fast-rng"] }
[workspace]
members = [
"crates/api",
"crates/app",
"crates/core",
"crates/domain",
"crates/infra",
"crates/utils"
]
3 changes: 3 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
[config]

[tasks.build]
command = "cargo"
args = ["build"]

[tasks.dev]
command = "cargo"
install_crate = "cargo-nextest"
cwd = "./crates/app"
dependencies = ["test"]
args = ["watch", "-x" , "run"]

Expand Down
6 changes: 6 additions & 0 deletions crates/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "api"
version = "0.1.0"
edition = "2021"

[dependencies]
14 changes: 14 additions & 0 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

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

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
19 changes: 19 additions & 0 deletions crates/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "app"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.7.5"
dotenv = "0.15.0"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
tracing = "0.1.40"


core = {path = "../core"}
infra = {path = "../infra"}
sqlx = { version = "0.7.4", features = ["postgres", "runtime-tokio-rustls"] }
tracing-subscriber = "0.3.18"
tokio = { version = "1.38.0", features = ["full"] }
uuid = { version = "1.10.0", features = ["v4", "fast-rng"] }
13 changes: 2 additions & 11 deletions src/main.rs → crates/app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::Arc;
use core::{config::ProdConfig, error::AppError, AppResult};
use infra::initialed_db;

use axum::{
extract::{Path, Request, State},
Expand All @@ -9,11 +10,9 @@ use axum::{
Json, Router,
};
use dotenv::dotenv;
use rust_microservice::{configs::ProdConfig, dbs::initialed_db, errors::AppError, AppResult};
use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::{prelude::FromRow, PgPool};
use tracing::info;
use uuid::Uuid;

#[tokio::main]
Expand All @@ -30,15 +29,12 @@ async fn main() {
.layer(middleware::map_response(mw_map_response)) // 1
.layer(middleware::from_fn_with_state(pool.clone(), mw_auth)) // 2
.with_state(pool);
info!("Connect Database successfully");

info!("Server is running on port: {}", cfg.web.addr);
let listener = tokio::net::TcpListener::bind(cfg.web.addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
}

pub async fn say_hello(Path(msg): Path<String>) -> AppResult<Json<serde_json::Value>> {
info!("->> Function Say Hello");
if msg.is_empty() {
Err(AppError::NotFound)
} else {
Expand Down Expand Up @@ -68,14 +64,9 @@ pub async fn get_user(State(db): State<PgPool>, Path(id): Path<UserId>) -> AppRe

pub async fn mw_map_response(uri: Uri, req_method: Method, res: Response) -> Response {
let uuid = Uuid::new_v4();
info!("->> MAP RESPONSE");
info!("->> UUid: {}", uuid.to_string());
info!("->> Method: {}", req_method.to_string());
info!("->> Uri: {}", uri.to_string());
(StatusCode::ACCEPTED, res).into_response()
}

pub async fn mw_auth(req: Request, next: Next) -> AppResult<Response> {
info!("->> MIDDLEWARE AUTH");
Ok(next.run(req).await)
}
13 changes: 13 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "core"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.7.5"
config = "0.14.0"
dotenv = "0.15.0"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
sqlx = { version = "0.7.4", features = ["postgres"] }
thiserror = "1.0.61"
2 changes: 1 addition & 1 deletion src/configs.rs → crates/core/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dotenv::var;
use serde::Deserialize;

use crate::{errors::AppError, AppResult};
use crate::{error::AppError, AppResult};

#[derive(Deserialize)]
pub struct WebConfig {
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use error::AppError;

pub mod config;
pub mod error;

pub type AppResult<T> = std::result::Result<T, AppError>;
6 changes: 6 additions & 0 deletions crates/domain/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "domain"
version = "0.1.0"
edition = "2021"

[dependencies]
14 changes: 14 additions & 0 deletions crates/domain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

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

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
7 changes: 7 additions & 0 deletions crates/infra/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "infra"
version = "0.1.0"
edition = "2021"

[dependencies]
sqlx = { version = "0.7.4", features = ["postgres", "runtime-tokio-rustls"] }
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions crates/utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "utils"
version = "0.1.0"
edition = "2021"

[dependencies]
14 changes: 14 additions & 0 deletions crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

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

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
20 changes: 9 additions & 11 deletions https.org
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,26 @@ GET :host/hello
// HTTP/1.1 202 Accepted
// content-type: application/json
// content-length: 15
// date: Mon, 08 Jul 2024 11:06:06 GMT
// Request duration: 0.002391s
// date: Wed, 10 Jul 2024 08:55:00 GMT
// Request duration: 0.033504s
#+END_SRC

#+RESULTS:

** User
#+BEGIN_SRC restclient :var host=host :var headers=headers
GET :host/user/1111
GET :host/user/1000
:headers
#+END_SRC

#+RESULTS:
#+BEGIN_SRC js
{
"pk_user_id": 1111,
"username": "aaaa"
"pk_user_id": 1000,
"username": "user1"
}
// GET http://localhost:3333/user/1111
// GET http://localhost:3333/user/1000
// HTTP/1.1 202 Accepted
// content-type: application/json
// content-length: 37
// date: Wed, 10 Jul 2024 07:10:31 GMT
// Request duration: 0.007594s
// content-length: 38
// date: Wed, 10 Jul 2024 08:55:08 GMT
// Request duration: 0.013101s
#+END_SRC
7 changes: 0 additions & 7 deletions src/lib.rs

This file was deleted.

0 comments on commit b84daf4

Please sign in to comment.