generated from lucien/actix-react-template
Added json usage
This commit is contained in:
parent
c992014d53
commit
669c710f89
2 changed files with 25 additions and 2 deletions
|
@ -6,4 +6,6 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-files = "0.6.6"
|
actix-files = "0.6.6"
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
|
serde = "1.0.215"
|
||||||
|
serde_json = "1.0.133"
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,32 @@
|
||||||
use actix_web::{App, HttpServer};
|
use actix_web::{App, HttpServer, get, Responder, HttpResponse, http::header::ContentType};
|
||||||
use actix_files::Files;
|
use actix_files::Files;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
|
||||||
|
#[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]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.service(Files::new("/", "./public").index_file("index.html"))
|
.service(api)
|
||||||
|
.service(Files::new("/", "public").index_file("index.html"))
|
||||||
})
|
})
|
||||||
.bind(("0.0.0.0", 8080))?
|
.bind(("0.0.0.0", 8080))?
|
||||||
.run()
|
.run()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue