Skip to content

Commit

Permalink
feat: document the pet clinic using axum
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Tran committed May 5, 2024
1 parent ac3fb81 commit 34b1f90
Show file tree
Hide file tree
Showing 20 changed files with 273 additions and 40 deletions.
80 changes: 80 additions & 0 deletions axum-petclinic-service/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion axum-petclinic-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ axum = { version = "0.7.5", features = ["macros"] }
derive_builder = {version = "0.20.0"}
tokio = { version = "1.37.0", features = ["full"] }
serde = { version = "1.0.199", features = ["derive"] }
sqlx = { version = "0.7.4", features = [ "runtime-tokio", "tls-rustls", "postgres", "chrono" ] }
tower-http = { version = "0.5.2", features = ["trace", "timeout"] }
tracing = { version = "0.1.40" }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
serde_json = "1.0.116"
validator = { version = "0.18.1", features = ["derive"] }
sqlx = { version = "0.7.4", features = ["sqlx-postgres"]}
chrono = "0.4.38"

[dev-dependencies]
Expand Down
22 changes: 21 additions & 1 deletion axum-petclinic-service/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Pet Clinic REST API using Axum & Tokio
# Pet Clinic REST API using [Axum](https://www.youtube.com/watch?v=Wnb_n5YktO8&t=716s) & Tokio

## Introduction
* Tokio is an asynchronous runtime for the Rust programming language. It provides the building blocks needed for writing network applications. It gives you the ability to write fast, reliable, and highly concurrent applications.
* Axum is a web framework built on top of Tokio and Tower. It is designed to be both ergonomic and performant. It is built on top of hyper, a fast and low-level HTTP implementation in Rust.

[Send + Sync](https://www.youtube.com/watch?v=yOezcP-XaIw)

## Technologies Stacks
* [Rust](https://www.rust-lang.org/)
* [Tokio](https://tokio.rs/)
Expand Down Expand Up @@ -188,3 +190,21 @@ make watch-test-change





## List of example repositories
https://github.com/sapati/shelter-project/tree/ep-12
https://github.com/MSC29/clean-architecture-rust
https://github.com/flosse/clean-architecture-with-rust
https://jameseastham.co.uk/post/software-development/hexagaonal-architecture-rust/
https://kerkour.com/rust-web-application-clean-architecture
https://github.com/skerkour/black-hat-rust
https://github.com/skerkour
https://www.youtube.com/watch?v=CHRNj5oubwc
https://youtube.com/watch?v=Lrayq0UW7nA
https://www.youtube.com/watch?v=3biW5NkNnrk&t=455s
https://www.youtube.com/watch?v=br6nGvqT48w
https://github.com/Quentin-Piot/axum-diesel-real-world/tree/master
https://github.com/yuk1ty/stock-metrics
https://github.com/asomers/mockall/issues/271

Binary file added axum-petclinic-service/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions axum-petclinic-service/src/api/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn routes() -> Router {
Router::new().route("/info", get(info))
}

async fn info() -> Result<Json<InfoResponse>, Error> {
async fn info() -> Json<InfoResponse> {
let info = InfoResponseBuilder::default()
.version("0.1.0".to_string())
.name("Rust API".to_string())
Expand All @@ -32,5 +32,5 @@ async fn info() -> Result<Json<InfoResponse>, Error> {
.build()
.unwrap();

Ok(Json(info))
Json(info)
}
1 change: 1 addition & 0 deletions axum-petclinic-service/src/api/owner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use axum::extract::Path;
use axum::routing::get;

mod model;
mod service;

pub fn routes() -> Router {
// let app_state = AppState { mc };
Expand Down
36 changes: 28 additions & 8 deletions axum-petclinic-service/src/api/owner/service.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
use crate::error::Error;
use crate::repository::owner;
use crate::repository::schema::Owner;

// trait Servicer {
// fn new() -> Self;
// // new(owner_repo: OwnerRepository) -> Self;
// // fn list_owners(&self) -> Result<Vec<Owner>, Error>;
// fn new(&self, ) -> Self;
// async fn find_owners(&self) -> Result<Vec<Owner>, Error>;

// }

// impl Servicer {
// pub fn new() -> Self {
// Self {}
// }
// }
#[derive(Debug)]
struct Service {
repo: owner::Repository,
}

impl Service {
// fn new(&self) -> Self {
// Self {repo: owner::Repository::new()}
// }
//
//
// async fn find_owners(&self) -> Result<Vec<Owner>, Error> {
// self.repo.list_owners()
// }
async pub fn new() -> Self {
Self {
repo: owner::Repository::new(),
}
}
async fn find_owners(&self) -> Result<Vec<Owner>, Error> {
self.repo.list_owners()
}
}
2 changes: 2 additions & 0 deletions axum-petclinic-service/src/api/pet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod service;

use serde::{Deserialize, Serialize};
use crate::repository::schema;

Expand Down
13 changes: 13 additions & 0 deletions axum-petclinic-service/src/api/pet/service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::error::Error;
use crate::repository::schema::{Pet};

// trait Servicer {
// async fn new() -> Self;
// async fn find_pets(&self) -> Result<Vec<Pet>, Error>;
//
// }

#[derive(Debug, Clone)]
struct Service {
// repo: OwnerRepository,
}
2 changes: 2 additions & 0 deletions axum-petclinic-service/src/api/vet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod service;

use serde::{Deserialize, Serialize};
use crate::repository::schema;

Expand Down
Empty file.
1 change: 1 addition & 0 deletions axum-petclinic-service/src/api/visit/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod service;
Empty file.
15 changes: 15 additions & 0 deletions axum-petclinic-service/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use sqlx::{Pool, Postgres};
use sqlx::postgres::PgPoolOptions;
use crate::error::Error;

pub async fn create_connection() -> Result<Pool<Postgres>, Error> {
let conn_string = "postgresql://postgres:mysecretpassword@localhost:5432/petclinic?sslmode=disable";
PgPoolOptions::new()
.min_connections(1)
.max_connections(5)
.connect(conn_string)
.await
.map_err(|e| {
Error::new("create_connection", &format!("{:?}", e))
})
}
Loading

0 comments on commit 34b1f90

Please sign in to comment.