Added json usage

This commit is contained in:
Lukian 2024-12-05 23:38:43 +01:00
parent c992014d53
commit 669c710f89
2 changed files with 25 additions and 2 deletions

View file

@ -6,4 +6,6 @@ edition = "2021"
[dependencies]
actix-files = "0.6.6"
actix-web = "4"
serde = "1.0.215"
serde_json = "1.0.133"

View file

@ -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 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]
async fn main() -> std::io::Result<()> {
HttpServer::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))?
.run()