Compare commits

...

18 commits

Author SHA1 Message Date
=
5b812000b8 Merge branch 'main' into main_page 2024-12-06 02:51:57 +01:00
ee692fec27 Actualiser back/src/main.rs 2024-12-06 01:50:00 +00:00
ac20ecc932 Merge pull request 'back_end' (#6) from back_end into main
Reviewed-on: #6
2024-12-06 00:54:17 +00:00
e2575b55b6 Actualiser back/Cargo.toml 2024-12-06 00:43:11 +00:00
3688d031b4 Actualiser back/src/main.rs
finito ?
2024-12-06 00:42:10 +00:00
9b91a5acf3 Updated docker-compose.yml 2024-12-06 01:33:19 +01:00
85ab4aba4f Fixed databse init function again 2024-12-06 01:31:27 +01:00
8f8fc0c46e Fixed databse init function 2024-12-06 01:30:34 +01:00
902e96b205 Added database init function 2024-12-06 01:00:02 +01:00
241a783092 Merge pull request 'Merged back_end to main branch' (#5) from back_end into main
Reviewed-on: #5
2024-12-05 23:29:13 +00:00
6461ebc629 Merged back_end and back_end_json 2024-12-06 00:26:12 +01:00
8316a35e9e Added json usage 2024-12-06 00:22:01 +01:00
c3a7d0ee7b Merge pull request 'Actualiser back/src/main.rs' (#4) from back_end into main
Reviewed-on: #4
2024-12-05 23:03:41 +00:00
c3888c61f9 Actualiser back/src/main.rs
added the service bc i forgot oupsie

Signed-off-by: linlkin <ilyass.ajdira@hotmail.com>
2024-12-05 23:02:43 +00:00
9fca283ab1 Merge pull request 'i added db and articles end points' (#1) from back_end into main
Reviewed-on: #1
2024-12-05 22:42:28 +00:00
efe0b6b410 Merge branch 'main' into back_end 2024-12-05 22:42:15 +00:00
669c710f89 Added json usage 2024-12-05 23:38:43 +01:00
57d574d143 i added db and articles end points 2024-12-05 23:19:15 +01:00
5 changed files with 99 additions and 5 deletions

3
.gitignore vendored
View file

@ -1,5 +1,6 @@
back/target/ back/target/
front/dist/ front/dist/
back/public/ back/public/
back/data/
back/Cargo.lock
back/Cargo.lock

View file

@ -6,4 +6,7 @@ edition = "2021"
[dependencies] [dependencies]
actix-files = "0.6.6" actix-files = "0.6.6"
actix-web = "4" actix-web = "4"
sqlite = "0.36.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.133"

21
back/src/create_db.rs Normal file
View file

@ -0,0 +1,21 @@
use sqlite::{Connection, OpenFlags};
pub fn init() -> sqlite::Result<()> {
let conn = Connection::open_with_flags(
"./data/data.db",
OpenFlags::new()
.with_create()
.with_read_write()
)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
subTitle TEXT,
content TEXT NOT NULL
)",
)?;
Ok(())
}

View file

@ -1,13 +1,80 @@
use actix_web::{App, HttpServer}; mod create_db;
use create_db::init;
use actix_web::{App, HttpServer, get, Responder, HttpResponse, http::header::ContentType};
use actix_files::Files; use actix_files::Files;
use serde_json::json;
use sqlite::{Connection, State};
use serde::{Serialize, Deserialize};
#[derive(serde::Serialize)]
struct Article {
id: i64,
title: String,
auteur: Option<String>,
edited_at: Option<String>,
published_at: Option<String>,
content: String,
}
#[get("/api/hello")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}
#[get("/api/articles")]
async fn get_articles() -> impl Responder {
let conn = Connection::open("./data/data.db").unwrap();
let mut stmt = conn.prepare("SELECT * FROM articles").unwrap();
let mut articles = Vec::new();
while let State::Row = stmt.next().unwrap() {
let id = stmt.read::<i64, _>(0).unwrap();
let title = stmt.read::<String, _>(1).unwrap();
let content = stmt.read::<String, _>(3).unwrap();
articles.push(Article {
id,
title,
auteur: None,
edited_at: None,
published_at: None,
content
});
}
HttpResponse::Ok().json(articles)
}
#[get("/api")]
async fn api() -> impl Responder {
let value = json!({
"code": 200,
"success": true,
"payload": {
"features": [
"serde",
"json"
],
"homepage": null
}
});
HttpResponse::Ok()
.content_type(ContentType::json())
.body(value.to_string())
}
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> Result<(), std::io::Error> {
let _ = init();
HttpServer::new(|| { HttpServer::new(|| {
App::new() App::new()
.service(Files::new("/", "./public").index_file("index.html")) .service(hello)
.service(get_articles)
.service(api)
.service(Files::new("/", "public").index_file("index.html"))
}) })
.bind(("0.0.0.0", 8080))? .bind(("0.0.0.0", 2486))?
.run() .run()
.await .await
} }

View file

@ -7,4 +7,6 @@ services:
restart: always restart: always
ports: ports:
- 8080:8080 - 8080:8080
volumes:
- ./back/data:/app/data