From 8d748b56e64b99d8627c5508d99a2e6f382f593a Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Sun, 12 Nov 2023 14:37:25 +0100 Subject: [PATCH] Backend: Created info table in database. Created README instructions for psql. Boilerplate for Rust. --- backend/Cargo.toml | 3 +- backend/README.md | 33 +++++++++++ backend/db/info.sql | 16 +++++ backend/src/main.rs | 59 ++++++++++++++++++- frontend/.svelte-kit/ambient.d.ts | 2 - .../.svelte-kit/generated/server/internal.js | 2 +- 6 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 backend/README.md create mode 100644 backend/db/info.sql diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 64b5953..2c8b3c6 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -10,6 +10,7 @@ axum = { version = "0.6.20", features = ["form"] } 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", "sqlite"] } +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"] } +chrono = { version = "0.4", features = ["serde"] } \ No newline at end of file diff --git a/backend/README.md b/backend/README.md new file mode 100644 index 0000000..63f6d44 --- /dev/null +++ b/backend/README.md @@ -0,0 +1,33 @@ +# Creation of the Database + +`[name]` being the username you created in postgres\ +`[pwd]` being its password + +``` +sudo dnf install postgresql postgresql-client postgresql-server postgresql-contrib +sudo -u postgres createdb --owner=name cv +sudo service postgresql restart +``` + +## Importing `.sql` files + +Idk for now (xd) + +## ⚠️ Be sure to modify your config file to access the postgres database ⚠️ + +``` +sudo nano /var/lib/pgsql/data/pg_hba.conf +``` +Check if those lines are correct : +``` +# IPv4 local connections: +host all all 127.0.0.1/32 md5 +# IPv6 local connections: +host all all ::1/128 md5 +``` + +# Env file configuration + +``` +DATABASE_URL=postgres://[name]:[pwd]@localhost/cv +``` \ No newline at end of file diff --git a/backend/db/info.sql b/backend/db/info.sql new file mode 100644 index 0000000..f2ea0bc --- /dev/null +++ b/backend/db/info.sql @@ -0,0 +1,16 @@ +-- public.info definition + +-- Drop table + +-- DROP TABLE public.info; + +CREATE TABLE public.info ( + birth_year date NULL, + id serial4 NOT NULL, + full_name text NULL, + phone_number varchar NULL, + email varchar NULL, + softskills text NULL, + interests text NULL, + CONSTRAINT info_pkey PRIMARY KEY (id) +); \ No newline at end of file diff --git a/backend/src/main.rs b/backend/src/main.rs index e7a11a9..1b979bb 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,3 +1,58 @@ -fn main() { - println!("Hello, world!"); +use axum::{ + extract::{Path, State}, + routing::{get, post}, + Form, Json, Router, +}; +use axum_error::Result; +use dotenv::dotenv; +use serde::{Deserialize, Serialize}; +use sqlx::{ + postgres::{PgPool, PgPoolOptions}, + types::chrono::NaiveDate, +}; +use std::net::SocketAddr; +use tower_http::cors::CorsLayer; + +#[tokio::main] +async fn main() -> Result<()> { + // Environnement table + pool connection + dotenv().ok(); + let url = std::env::var("DATABASE_URL")?; + let pool = PgPoolOptions::new() + .max_connections(5) + .connect(&url) + .await?; + + // Creating a router + let router = Router::new() + .route("/info", get(info)) + .with_state(pool) + .layer(CorsLayer::very_permissive()); + + // Server + let address = SocketAddr::from(([0, 0, 0, 0], 8000)); + Ok(axum::Server::bind(&address) + .serve(router.into_make_service()) + .await?) +} + +#[derive(Deserialize, Serialize)] +struct Info { + id: i64, + full_name: Option, + phone_number: Option, + email: Option, + softskills: Option, + interests: Option, + birth_year: Option, +} + +async fn info(State(pool): State) -> Result>> { + let infos = sqlx::query_as!( + Info, + "SELECT id, full_name, phone_number, email, softskills, interests, birth_year FROM public.info" + ) + .fetch_all(&pool) + .await?; + Ok(Json(infos)) } diff --git a/frontend/.svelte-kit/ambient.d.ts b/frontend/.svelte-kit/ambient.d.ts index 66bf8b7..b81e570 100644 --- a/frontend/.svelte-kit/ambient.d.ts +++ b/frontend/.svelte-kit/ambient.d.ts @@ -109,7 +109,6 @@ declare module '$env/static/private' { export const npm_node_execpath: string; export const npm_config_engine_strict: string; export const GIO_LAUNCHED_DESKTOP_FILE: string; - export const OLDPWD: string; export const TERM_PROGRAM: string; export const NODE_ENV: string; } @@ -224,7 +223,6 @@ declare module '$env/dynamic/private' { npm_node_execpath: string; npm_config_engine_strict: string; GIO_LAUNCHED_DESKTOP_FILE: string; - OLDPWD: string; TERM_PROGRAM: string; NODE_ENV: string; [key: `PUBLIC_${string}`]: undefined; diff --git a/frontend/.svelte-kit/generated/server/internal.js b/frontend/.svelte-kit/generated/server/internal.js index 8401b74..3878ffe 100644 --- a/frontend/.svelte-kit/generated/server/internal.js +++ b/frontend/.svelte-kit/generated/server/internal.js @@ -20,7 +20,7 @@ export const options = { app: ({ head, body, assets, nonce, env }) => "\n\n\t\n\t\t\n\t\t\n\t\t\n\t\t" + head + "\n\t\n\t\n\t\t
" + body + "
\n\t\n\n", error: ({ status, message }) => "\n\n\t\n\t\t\n\t\t" + message + "\n\n\t\t\n\t\n\t\n\t\t
\n\t\t\t" + status + "\n\t\t\t
\n\t\t\t\t

" + message + "

\n\t\t\t
\n\t\t
\n\t\n\n" }, - version_hash: "1sncic5" + version_hash: "gzttwb" }; export function get_hooks() {