Skip to content

Commit

Permalink
Simple rest api to begin
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreOucif committed Jul 7, 2023
1 parent 456a731 commit f891d71
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions 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 application/rest-api-adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ version = "1.0.0"
[lib]

[dependencies]
rocket = { version = "=0.5.0-rc.3" }
rocket = { version = "=0.5.0-rc.3", features = ["json"] }
1 change: 1 addition & 0 deletions application/rest-api-adapter/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod user_api;
17 changes: 17 additions & 0 deletions application/rest-api-adapter/src/api/user_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::dto::user_dto_request::UserDtoRequest;
use rocket::serde::json::Json;
use rocket::{get, Build, Rocket};

#[get("/")]
pub fn index() -> &'static str {
"Hello, world!"
}

#[post("/user", format = "application/json", data = "<user_dto_request>")]
async fn post_user(user_dto_request: Json<UserDtoRequest>) {
println!("User name : {}", user_dto_request.name)
}

pub fn build_api() -> Rocket<Build> {
rocket::build().mount("/", routes![index]).mount("/api/v1/", routes![post_user])
}
1 change: 1 addition & 0 deletions application/rest-api-adapter/src/dto/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod user_dto_request;
7 changes: 7 additions & 0 deletions application/rest-api-adapter/src/dto/user_dto_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use rocket::serde::Deserialize;

#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct UserDtoRequest {
pub name: String,
}
11 changes: 2 additions & 9 deletions application/rest-api-adapter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
#[macro_use]
pub extern crate rocket;
use rocket::{get, Build, Rocket};

#[get("/")]
pub fn index() -> &'static str {
"Hello, world!"
}

pub fn build_api() -> Rocket<Build> {
rocket::build().mount("/", routes![index])
}
pub mod api;
pub mod dto;
2 changes: 1 addition & 1 deletion bootstrap/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate rocket;

use rest_api_adapter::build_api;
use rest_api_adapter::api::user_api::build_api;

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
Expand Down

0 comments on commit f891d71

Please sign in to comment.