Skip to content
Snippets Groups Projects
Commit cd4407ec authored by nutzer05's avatar nutzer05
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
/target
This diff is collapsed.
[package]
name = "webrechner"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4.4.1"
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
use serde::{Deserialize, Serialize};
use serde_json;
#[derive(Deserialize)]
enum CalcOp {
Add,
Sub,
Mul,
Div,
}
#[derive(Deserialize)]
struct CalcReq {
a: f32,
b: f32,
op: CalcOp,
}
#[derive(Serialize)]
struct CalcRes {
res: f32,
}
impl CalcReq {
fn calc(&self) -> CalcRes {
let res = match self.op {
CalcOp::Add => self.a + self.b,
CalcOp::Sub => self.a - self.b,
CalcOp::Mul => self.a * self.b,
CalcOp::Div => self.a / self.b,
};
CalcRes { res }
}
}
#[post("/calc")]
async fn calc(req_body: String) -> impl Responder {
let req: CalcReq = serde_json::from_str(&req_body).unwrap();
let res = req.calc();
HttpResponse::Ok().body(serde_json::to_string(&res).unwrap())
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(calc)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment