generated from lucien/actix-react-template
back_end #6
7 changed files with 50 additions and 21 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,5 +1,6 @@
|
||||||
back/target/
|
back/target/
|
||||||
front/dist/
|
front/dist/
|
||||||
back/public/
|
back/public/
|
||||||
|
back/data/
|
||||||
back/Cargo.lock
|
back/Cargo.lock
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,6 @@ edition = "2021"
|
||||||
actix-files = "0.6.6"
|
actix-files = "0.6.6"
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
sqlite = "0.36.1"
|
sqlite = "0.36.1"
|
||||||
serde = "1.0.215"
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0.133"
|
serde_json = "1.0.133"
|
||||||
|
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
use sqlite::{Connection, State};
|
|
||||||
|
|
||||||
fn main() -> sqlite::Result<()> {
|
|
||||||
let conn = Connection::open("./data/data.db")?;
|
|
||||||
|
|
||||||
conn.execute(
|
|
||||||
"CREATE TABLE articles (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
title TEXT NOT NULL,
|
|
||||||
subTitle TEXT,
|
|
||||||
content TEXT NOT NULL,
|
|
||||||
)",
|
|
||||||
[],
|
|
||||||
)?;
|
|
||||||
}
|
|
21
back/src/create_db.rs
Normal file
21
back/src/create_db.rs
Normal 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(())
|
||||||
|
}
|
|
@ -1,7 +1,21 @@
|
||||||
|
mod create_db;
|
||||||
|
use create_db::init;
|
||||||
|
|
||||||
use actix_web::{App, HttpServer, get, Responder, HttpResponse, http::header::ContentType};
|
use actix_web::{App, HttpServer, get, Responder, HttpResponse, http::header::ContentType};
|
||||||
use actix_files::Files;
|
use actix_files::Files;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use sqlite::{Connection, State, Error};
|
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")]
|
#[get("/api/hello")]
|
||||||
async fn hello() -> impl Responder {
|
async fn hello() -> impl Responder {
|
||||||
|
@ -17,9 +31,15 @@ async fn get_articles() -> impl Responder {
|
||||||
while let State::Row = stmt.next().unwrap() {
|
while let State::Row = stmt.next().unwrap() {
|
||||||
let id = stmt.read::<i64, _>(0).unwrap();
|
let id = stmt.read::<i64, _>(0).unwrap();
|
||||||
let title = stmt.read::<String, _>(1).unwrap();
|
let title = stmt.read::<String, _>(1).unwrap();
|
||||||
let subTitle = stmt.read::<String, _>(2).unwrap();
|
|
||||||
let content = stmt.read::<String, _>(3).unwrap();
|
let content = stmt.read::<String, _>(3).unwrap();
|
||||||
articles.push((id, title, subTitle, content));
|
articles.push(Article {
|
||||||
|
id,
|
||||||
|
title,
|
||||||
|
auteur: None,
|
||||||
|
edited_at: None,
|
||||||
|
published_at: None,
|
||||||
|
content
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpResponse::Ok().json(articles)
|
HttpResponse::Ok().json(articles)
|
||||||
|
@ -45,7 +65,7 @@ async fn api() -> impl Responder {
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> Result<(), std::io::Error> {
|
async fn main() -> Result<(), std::io::Error> {
|
||||||
let conn = Connection::open("./data/data.db");
|
let _ = init();
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
|
|
|
@ -7,4 +7,6 @@ services:
|
||||||
restart: always
|
restart: always
|
||||||
ports:
|
ports:
|
||||||
- 8080:8080
|
- 8080:8080
|
||||||
|
volumes:
|
||||||
|
- ./back/data:/app/data
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue