diff --git a/.gitignore b/.gitignore index e63b1ee..f4214eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ back/target/ front/dist/ back/public/ +back/data/ +back/Cargo.lock -back/Cargo.lock \ No newline at end of file diff --git a/back/Cargo.toml b/back/Cargo.toml index 8b48126..6be9ee0 100644 --- a/back/Cargo.toml +++ b/back/Cargo.toml @@ -6,4 +6,7 @@ edition = "2021" [dependencies] actix-files = "0.6.6" actix-web = "4" +sqlite = "0.36.1" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0.133" diff --git a/back/src/create_db.rs b/back/src/create_db.rs new file mode 100644 index 0000000..67eb2af --- /dev/null +++ b/back/src/create_db.rs @@ -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(()) +} diff --git a/back/src/main.rs b/back/src/main.rs index d4d0ee1..7242d32 100644 --- a/back/src/main.rs +++ b/back/src/main.rs @@ -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 serde_json::json; +use sqlite::{Connection, State}; +use serde::{Serialize, Deserialize}; + +#[derive(serde::Serialize)] +struct Article { + id: i64, + title: String, + auteur: Option, + edited_at: Option, + published_at: Option, + 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::(0).unwrap(); + let title = stmt.read::(1).unwrap(); + let content = stmt.read::(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] -async fn main() -> std::io::Result<()> { +async fn main() -> Result<(), std::io::Error> { + let _ = init(); + HttpServer::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() .await } diff --git a/docker-compose.yml b/docker-compose.yml index 1e8480c..1e0df8d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,4 +7,6 @@ services: restart: always ports: - 8080:8080 + volumes: + - ./back/data:/app/data