generated from lucien/actix-react-template
Merge pull request 'i added db and articles end points' (#1) from back_end into main
Reviewed-on: #1
This commit is contained in:
commit
9fca283ab1
4 changed files with 43 additions and 3 deletions
|
@ -6,4 +6,4 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-files = "0.6.6"
|
actix-files = "0.6.6"
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
|
sqlite = "0.36.1"
|
||||||
|
|
0
back/data/data.db
Normal file
0
back/data/data.db
Normal file
15
back/src/creat_db.rs
Normal file
15
back/src/creat_db.rs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
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,
|
||||||
|
)",
|
||||||
|
[],
|
||||||
|
)?;
|
||||||
|
}
|
|
@ -1,10 +1,14 @@
|
||||||
use actix_web::{App, HttpServer};
|
use actix_web::{get, http, post, web, App, HttpResponse, HttpServer, Responder};
|
||||||
use actix_files::Files;
|
use actix_files::Files;
|
||||||
|
use sqlite::{Connection, State, Error};
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> Result<(), std::io::Error> {
|
||||||
|
let conn = Connection::open("./data/data.db");
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
|
.service(hello)
|
||||||
.service(Files::new("/", "./public").index_file("index.html"))
|
.service(Files::new("/", "./public").index_file("index.html"))
|
||||||
})
|
})
|
||||||
.bind(("0.0.0.0", 8080))?
|
.bind(("0.0.0.0", 8080))?
|
||||||
|
@ -12,3 +16,24 @@ async fn main() -> std::io::Result<()> {
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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 subTitle = stmt.read::<String, _>(2).unwrap();
|
||||||
|
let content = stmt.read::<String, _>(3).unwrap();
|
||||||
|
articles.push((id, title, subTitle, content));
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpResponse::Ok().json(articles)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue