generated from lucien/actix-react-template
Merged back_end to main branch #5
2 changed files with 40 additions and 16 deletions
|
@ -7,3 +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_json = "1.0.133"
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,8 @@
|
||||||
use actix_web::{get, http, post, web, App, HttpResponse, HttpServer, Responder};
|
use actix_web::{App, HttpServer, get, Responder, HttpResponse, http::header::ContentType};
|
||||||
use actix_files::Files;
|
use actix_files::Files;
|
||||||
|
use serde_json::json;
|
||||||
use sqlite::{Connection, State, Error};
|
use sqlite::{Connection, State, Error};
|
||||||
|
|
||||||
#[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
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/api/hello")]
|
#[get("/api/hello")]
|
||||||
async fn hello() -> impl Responder {
|
async fn hello() -> impl Responder {
|
||||||
HttpResponse::Ok().body("Hello world!")
|
HttpResponse::Ok().body("Hello world!")
|
||||||
|
@ -38,3 +24,38 @@ async fn get_articles() -> impl Responder {
|
||||||
|
|
||||||
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