generated from lucien/actix-react-template
fais que ça marche je t'ensuplie
This commit is contained in:
parent
241a783092
commit
e4927d4cae
2 changed files with 37 additions and 40 deletions
|
@ -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,7 +1,33 @@
|
||||||
use actix_web::{App, HttpServer, get, Responder, HttpResponse, http::header::ContentType};
|
|
||||||
use actix_files::Files;
|
use actix_files::Files;
|
||||||
use serde_json::json;
|
use actix_web::{get, http, post, web, App, HttpResponse, HttpServer, Responder};
|
||||||
use sqlite::{Connection, State, Error};
|
use sqlite::{Connection, State, Error};
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
#[actix_web::main]
|
||||||
|
async fn main() -> Result<(), std::io::Error> {
|
||||||
|
let conn = Connection::open("./data/data.db");
|
||||||
|
|
||||||
|
HttpServer::new(|| {
|
||||||
|
App::new()
|
||||||
|
.service(hello)
|
||||||
|
.service(get_articles)
|
||||||
|
.service(Files::new("/", "./public").index_file("index.html"))
|
||||||
|
})
|
||||||
|
.bind(("0.0.0.0", 8080))?
|
||||||
|
.run()
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[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,45 +43,16 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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() -> Result<(), std::io::Error> {
|
|
||||||
let conn = Connection::open("./data/data.db");
|
|
||||||
|
|
||||||
HttpServer::new(|| {
|
|
||||||
App::new()
|
|
||||||
.service(hello)
|
|
||||||
.service(get_articles)
|
|
||||||
.service(api)
|
|
||||||
.service(Files::new("/", "public").index_file("index.html"))
|
|
||||||
})
|
|
||||||
.bind(("0.0.0.0", 8080))?
|
|
||||||
.run()
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue