mirror of
https://github.com/rjNemo/rust-web
synced 2026-06-06 02:46:41 +00:00
index and get
This commit is contained in:
commit
0cc35faa2d
5 changed files with 1851 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
/.idea
|
||||
1781
Cargo.lock
generated
Normal file
1781
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
11
Cargo.toml
Normal file
11
Cargo.toml
Normal 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
1
rustfmt.toml
Normal file
|
|
@ -0,0 +1 @@
|
|||
edition = "2018"
|
||||
56
src/main.rs
Normal file
56
src/main.rs
Normal 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
|
||||
}
|
||||
Loading…
Reference in a new issue