Merge branch 'main' into game

This commit is contained in:
iMaxLaPince 2024-12-06 02:40:26 +00:00
commit 9ee9ae8cb7
5 changed files with 99 additions and 5 deletions

3
.gitignore vendored
View file

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

View file

@ -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"

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 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]
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
}

View file

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