Backend: Updated Axum, Tower-http, Added Axum-Server. Fixed Router which no longer accepts Result<T>. Fixed Database files with info_id default value.

This commit is contained in:
Yohan Boujon 2024-01-08 13:27:49 +01:00
parent ee72389679
commit 1aa48a15ac
10 changed files with 37 additions and 25 deletions

View file

@ -6,11 +6,12 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
axum = { version = "0.6.20", features = ["form"] }
axum = "0.7.3"
axum-server = "0.6.0"
axum-error = "0.2.0"
dotenv = "0.15.0"
serde = { version = "1.0.192", features = ["derive"] }
sqlx = { version = "0.7.2", features = ["runtime-tokio", "tls-rustls", "postgres", "chrono"] }
tokio = { version = "1.34.0", features = ["full"] }
tower-http = { version = "0.4.4", features = ["cors"] }
tower-http = { version = "0.5.0", features = ["cors"] }
chrono = { version = "0.4", features = ["serde"] }

View file

@ -6,7 +6,7 @@
CREATE TABLE public.softwares (
id serial4 NOT NULL,
info_id int4 NOT NULL DEFAULT nextval('softwares_skills_id_seq'::regclass),
info_id int4 NOT NULL,
software text NOT NULL,
icon text NOT NULL,
type_icon text NOT NULL,

View file

@ -10,7 +10,7 @@ CREATE TABLE public.project (
title text NULL,
description text NULL,
github_link text NULL,
info_id int4 NOT NULL DEFAULT nextval('project_id_skills_seq'::regclass),
info_id int4 NOT NULL,
picture_name text NULL,
type_project text NULL,
CONSTRAINT project_pkey PRIMARY KEY (id),

View file

@ -6,7 +6,7 @@
CREATE TABLE public.programming_languages (
id serial4 NOT NULL,
info_id int4 NOT NULL DEFAULT nextval('programming_languages_skills_id_seq'::regclass),
info_id int4 NOT NULL,
lang text NOT NULL,
icon text NOT NULL,
type_icon text NOT NULL,

View file

@ -6,7 +6,7 @@
CREATE TABLE public.languages (
id serial4 NOT NULL,
info_id int4 NOT NULL DEFAULT nextval('languages_skills_id_seq'::regclass),
info_id int4 NOT NULL,
lang text NOT NULL,
icon_alpha varchar(3) NOT NULL,
"level" text NOT NULL,

10
backend/db/install_db.sh Executable file
View file

@ -0,0 +1,10 @@
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: $0 <database_user>"
exit 1
fi
for file in *.sql; do
psql -U $1 -d cv -a -f "$file"
done

View file

@ -1,7 +1,7 @@
use axum::{
extract::{Path, State},
routing::{get, post},
Form, Json, Router,
routing::get,
Json, Router,
};
use axum_error::Result;
use dotenv::dotenv;
@ -33,49 +33,50 @@ async fn main() -> Result<()> {
// Server
let address = SocketAddr::from(([0, 0, 0, 0], 8000));
Ok(axum::Server::bind(&address)
Ok(axum_server::bind(address)
.serve(router.into_make_service())
.await?)
}
macro_rules! gather_data {
macro_rules! _gather_data {
($data_type:ty, $sql_cmd:expr, $pool:expr) => {
sqlx::query_as!($data_type, $sql_cmd).fetch_all($pool).await
};
}
async fn info(State(pool): State<PgPool>) -> Result<Json<Vec<Info>>> {
async fn info(State(pool): State<PgPool>) -> Json<Vec<Info>>
{
let datas = sqlx::query_as!(
Info,
"SELECT id, full_name, phone_number, email, softskills, interests, birth_year FROM public.info"
)
.fetch_all(&pool)
.await?;
Ok(Json(datas))
.await.unwrap_or(vec![]);
Json(datas)
}
async fn education(State(pool): State<PgPool>) -> Result<Json<Vec<Education>>> {
async fn education(State(pool): State<PgPool>) -> Json<Vec<Education>> {
let datas = sqlx::query_as!(Education, "SELECT * FROM public.education")
.fetch_all(&pool)
.await?;
Ok(Json(datas))
.await.unwrap_or(vec![]);
Json(datas)
}
async fn experience(State(pool): State<PgPool>) -> Result<Json<Vec<Experience>>> {
async fn experience(State(pool): State<PgPool>) -> Json<Vec<Experience>> {
let datas = sqlx::query_as!(Experience, "SELECT * FROM public.experience")
.fetch_all(&pool)
.await?;
Ok(Json(datas))
.await.unwrap_or(vec![]);
Json(datas)
}
async fn skills(State(pool): State<PgPool>, Path(id): Path<i32>) -> Result<Json<(Vec<Project>,Vec<ProgrammingLanguages>,Vec<Softwares>,Vec<Languages>)>> {
async fn skills(Path(id): Path<i32>, State(pool): State<PgPool>) -> Json<(Vec<Project>,Vec<ProgrammingLanguages>,Vec<Softwares>,Vec<Languages>)> {
let project = sqlx::query_as!(
Project,
"SELECT date_done, title, description, github_link, picture_name, type_project FROM public.project WHERE project.info_id = $1 ORDER BY date_done DESC",
id
)
.fetch_all(&pool)
.await?;
.await.unwrap_or(vec![]);
let programming_languages = sqlx::query_as!(
ProgrammingLanguages,
@ -83,7 +84,7 @@ async fn skills(State(pool): State<PgPool>, Path(id): Path<i32>) -> Result<Json<
id
)
.fetch_all(&pool)
.await?;
.await.unwrap_or(vec![]);
let softwares = sqlx::query_as!(
Softwares,
@ -91,7 +92,7 @@ async fn skills(State(pool): State<PgPool>, Path(id): Path<i32>) -> Result<Json<
id
)
.fetch_all(&pool)
.await?;
.await.unwrap_or(vec![]);
let languages = sqlx::query_as!(
Languages,
@ -99,7 +100,7 @@ async fn skills(State(pool): State<PgPool>, Path(id): Path<i32>) -> Result<Json<
id
)
.fetch_all(&pool)
.await?;
.await.unwrap_or(vec![]);
Ok(Json((project,programming_languages,softwares,languages)))
Json((project,programming_languages,softwares,languages))
}