From 669c710f895279dcc13691f491f69b1906ae718e Mon Sep 17 00:00:00 2001 From: Lukian Date: Thu, 5 Dec 2024 23:38:43 +0100 Subject: [PATCH] Added json usage --- back/Cargo.toml | 2 ++ back/src/main.rs | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/back/Cargo.toml b/back/Cargo.toml index 8b48126..e2495de 100644 --- a/back/Cargo.toml +++ b/back/Cargo.toml @@ -6,4 +6,6 @@ edition = "2021" [dependencies] actix-files = "0.6.6" actix-web = "4" +serde = "1.0.215" +serde_json = "1.0.133" diff --git a/back/src/main.rs b/back/src/main.rs index d4d0ee1..e63b176 100644 --- a/back/src/main.rs +++ b/back/src/main.rs @@ -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()