add json route

This commit is contained in:
José Henrique 2023-10-29 19:01:13 -03:00
parent d5d2ab4956
commit 77aa085b0c
7 changed files with 94 additions and 16 deletions

View File

@ -22,5 +22,11 @@ namespace TCC.Controllers
return Ok(sum); return Ok(sum);
} }
[HttpGet("json")]
public async Task<IActionResult> GetHarmonicProgression([FromQuery] int n)
{
return Ok(new { message = "Hello World!" });
}
} }
} }

View File

@ -11,7 +11,7 @@ namespace TCC.Controllers
[HttpGet("ok")] [HttpGet("ok")]
public async Task<IActionResult> ReturnOk() public async Task<IActionResult> ReturnOk()
{ {
return Ok(new { status = 200 }); return Ok();
} }
} }
} }

62
ActixAPI/Cargo.lock generated
View File

@ -8,8 +8,11 @@ version = "0.1.0"
dependencies = [ dependencies = [
"actix-files", "actix-files",
"actix-web", "actix-web",
"futures",
"magick_rust", "magick_rust",
"qstring", "qstring",
"serde",
"serde_json",
] ]
[[package]] [[package]]
@ -549,12 +552,65 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "futures"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]] [[package]]
name = "futures-core" name = "futures-core"
version = "0.3.28" version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c"
[[package]]
name = "futures-executor"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa"
[[package]]
name = "futures-macro"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.32",
]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.28" version = "0.3.28"
@ -573,10 +629,16 @@ version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533"
dependencies = [ dependencies = [
"futures-channel",
"futures-core", "futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task", "futures-task",
"memchr",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
"slab",
] ]
[[package]] [[package]]

View File

@ -10,3 +10,6 @@ actix-web = "4"
actix-files = "0.6.2" actix-files = "0.6.2"
magick_rust = "0.19.1" magick_rust = "0.19.1"
qstring = "0.7.2" qstring = "0.7.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
futures = "0.3"

View File

@ -5,7 +5,7 @@ use magick_rust::MagickWand;
#[get("/status/ok")] #[get("/status/ok")]
async fn hello() -> impl Responder { async fn hello() -> impl Responder {
HttpResponse::Ok().body("{\"status\": 200}") HttpResponse::Ok()
} }
async fn static_serve(req: HttpRequest) -> Result<NamedFile> { async fn static_serve(req: HttpRequest) -> Result<NamedFile> {
@ -29,6 +29,17 @@ async fn simulation_harmonic_progression(req: HttpRequest) -> impl Responder {
HttpResponse::Ok().body(format!("{}", sum)) HttpResponse::Ok().body(format!("{}", sum))
} }
#[get("/simulation/json")]
async fn simulation_json() -> impl Responder {
// body = {"message": "Hello World"}
let body = serde_json::json!({
"message": "Hello World!"
});
HttpResponse::Ok()
.json(body)
}
#[get("/image/load-small-image")] #[get("/image/load-small-image")]
async fn load_small_image() -> Result<NamedFile> { async fn load_small_image() -> Result<NamedFile> {
let real_path = "static/small-image.png"; let real_path = "static/small-image.png";
@ -89,6 +100,7 @@ async fn main() -> std::io::Result<()> {
.service(save_big_image) .service(save_big_image)
.service(blur_image) .service(blur_image)
.service(simulation_harmonic_progression) .service(simulation_harmonic_progression)
.service(simulation_json)
.app_data(web::PayloadConfig::new(1024 * 1024 * 1024)) .app_data(web::PayloadConfig::new(1024 * 1024 * 1024))
}) })
.bind(("0.0.0.0", 5000))? .bind(("0.0.0.0", 5000))?

View File

@ -1,4 +1,4 @@
from flask import request, Blueprint from flask import request, Blueprint, jsonify
simulation_blueprint = Blueprint('simulation_blueprint', __name__) simulation_blueprint = Blueprint('simulation_blueprint', __name__)
@ -13,6 +13,9 @@ class SimulationController:
return sum return sum
def return_helloworld(self):
return jsonify(message="Hello World!")
simulation_controller = SimulationController() simulation_controller = SimulationController()
@simulation_blueprint.route('/simulation/harmonic-progression', methods=['GET']) @simulation_blueprint.route('/simulation/harmonic-progression', methods=['GET'])
@ -21,3 +24,7 @@ def return_ok():
sum = simulation_controller.harmonic_progression(n) sum = simulation_controller.harmonic_progression(n)
return str(sum), 200 return str(sum), 200
@simulation_blueprint.route('/simulation/json', methods=['GET'])
def return_ok():
return simulation_controller.return_helloworld(), 200

View File

@ -2,18 +2,6 @@ from flask import jsonify, Blueprint
status_blueprint = Blueprint('status_blueprint', __name__) status_blueprint = Blueprint('status_blueprint', __name__)
class StatusController:
def __init__(self):
pass
def return_ok(self):
return jsonify(status=200)
status_controller = StatusController()
@status_blueprint.route('/status/ok', methods=['GET']) @status_blueprint.route('/status/ok', methods=['GET'])
def return_ok(): def return_ok():
return jsonify(status=200) return 200