index and get

This commit is contained in:
Ruidy 2020-12-23 13:44:12 +01:00
commit 0cc35faa2d
5 changed files with 1851 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
/.idea

1781
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

11
Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "web"
version = "0.1.0"
authors = ["Ruidy <r.nemausat@empfohlen.de>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "3"
serde = "1.0.118"

1
rustfmt.toml Normal file
View file

@ -0,0 +1 @@
edition = "2018"

56
src/main.rs Normal file
View file

@ -0,0 +1,56 @@
use actix_web::{get, web, HttpResponse, Result};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Task {
id: u8,
title: String,
is_completed: bool,
}
#[derive(Serialize, Deserialize)]
struct TaskList {
tasks: Vec<Task>,
}
#[get("/")]
async fn index(data: web::Data<TaskList>) -> Result<HttpResponse> {
Ok(HttpResponse::Ok().json(&data.tasks))
}
#[get("/{id}")]
async fn get_task(
web::Path(id): web::Path<usize>,
data: web::Data<TaskList>,
) -> Result<HttpResponse> {
let task = &data.tasks[id];
Ok(HttpResponse::Ok().json(task))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
HttpServer::new(|| {
App::new()
.data(TaskList {
tasks: vec![
Task {
id: 0,
title: "Learn Rust".to_string(),
is_completed: false,
},
Task {
id: 1,
title: "Learn Actix".to_string(),
is_completed: false,
},
],
})
.service(index)
.service(get_task)
})
.bind("127.0.0.1:8000")?
.run()
.await
}