[BE] Rocket basic structure & CORS

master
Araozu 2023-08-25 11:52:03 -05:00
parent acb29940fc
commit a2ad5fde38
7 changed files with 2833 additions and 0 deletions

2751
backend/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
backend/Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "backend"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = "0.11.20"
rocket = { version = "=0.5.0-rc.3" , features = ["json", "msgpack", "uuid"] }
sqlx = { version = "0.7.1", features = [ "runtime-tokio", "tls-rustls" ] }

View File

@ -0,0 +1 @@
pub mod person;

View File

@ -0,0 +1,14 @@
use rocket::{http::ContentType, serde::json::Json, Response};
use crate::model::person::Person;
#[get("/person/<dni>")]
pub async fn get_by_dni(dni: i32) -> Json<Person> {
Json(Person {
person_id: 1,
person_dni: format!("{}", dni),
person_names: "Juan".to_string(),
person_paternal_surname: "Perez".to_string(),
person_maternal_surname: "Gomez".to_string(),
})
}

43
backend/src/main.rs Normal file
View File

@ -0,0 +1,43 @@
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::Header;
#[macro_use]
extern crate rocket;
mod controller;
mod model;
struct Cors;
#[rocket::async_trait]
impl Fairing for Cors {
fn info(&self) -> Info {
Info {
name: "CORS",
kind: Kind::Response,
}
}
#[cfg(debug_assertions)]
async fn on_response<'r>(
&self,
_: &'r rocket::Request<'_>,
response: &mut rocket::Response<'r>,
) {
response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
response.set_header(Header::new(
"Access-Control-Allow-Methods",
"POST, GET, OPTIONS",
));
response.set_header(Header::new("Access-Control-Allow-Headers", "Content-Type"));
}
}
#[launch]
fn rocket() -> _ {
rocket::build()
.attach(Cors {})
.mount("/api", routes![controller::person::get_by_dni])
}

1
backend/src/model/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod person;

View File

@ -0,0 +1,11 @@
use rocket::serde::Serialize;
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
pub struct Person {
pub person_id: i32,
pub person_dni: String,
pub person_names: String,
pub person_paternal_surname: String,
pub person_maternal_surname: String,
}