mirror of
https://github.com/rjNemo/rust-web
synced 2026-06-06 02:46:41 +00:00
refactor to multiple files
This commit is contained in:
parent
0cc35faa2d
commit
7b4d311f48
6 changed files with 122 additions and 35 deletions
49
Cargo.lock
generated
49
Cargo.lock
generated
|
|
@ -296,6 +296,17 @@ dependencies = [
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "atty"
|
||||||
|
version = "0.2.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
"winapi 0.3.9",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
|
|
@ -523,6 +534,19 @@ dependencies = [
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "env_logger"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f26ecb66b4bdca6c1409b40fb255eefc2bd4f6d135dab3c3124f80ffa2a9661e"
|
||||||
|
dependencies = [
|
||||||
|
"atty",
|
||||||
|
"humantime",
|
||||||
|
"log",
|
||||||
|
"regex",
|
||||||
|
"termcolor",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "flate2"
|
name = "flate2"
|
||||||
version = "1.0.19"
|
version = "1.0.19"
|
||||||
|
|
@ -758,6 +782,12 @@ version = "1.3.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"
|
checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "humantime"
|
||||||
|
version = "2.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "idna"
|
name = "idna"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
|
@ -1403,6 +1433,15 @@ dependencies = [
|
||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "termcolor"
|
||||||
|
version = "1.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "1.0.22"
|
version = "1.0.22"
|
||||||
|
|
@ -1718,6 +1757,7 @@ name = "web"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-web",
|
"actix-web",
|
||||||
|
"env_logger",
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -1755,6 +1795,15 @@ version = "0.4.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
||||||
|
dependencies = [
|
||||||
|
"winapi 0.3.9",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
|
|
||||||
|
|
@ -8,4 +8,5 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "3"
|
actix-web = "3"
|
||||||
serde = "1.0.118"
|
serde = "1.0.118"
|
||||||
|
env_logger = "0.8.2"
|
||||||
43
src/main.rs
43
src/main.rs
|
|
@ -1,54 +1,29 @@
|
||||||
use actix_web::{get, web, HttpResponse, Result};
|
use actix_web::{middleware, App, HttpServer};
|
||||||
use serde::{Deserialize, Serialize};
|
use env_logger::Env;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
mod task;
|
||||||
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]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
use actix_web::{App, HttpServer};
|
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.data(TaskList {
|
.wrap(middleware::Logger::default())
|
||||||
|
.data(task::TaskList {
|
||||||
tasks: vec![
|
tasks: vec![
|
||||||
Task {
|
task::Task {
|
||||||
id: 0,
|
id: 0,
|
||||||
title: "Learn Rust".to_string(),
|
title: "Learn Rust".to_string(),
|
||||||
is_completed: false,
|
is_completed: false,
|
||||||
},
|
},
|
||||||
Task {
|
task::Task {
|
||||||
id: 1,
|
id: 1,
|
||||||
title: "Learn Actix".to_string(),
|
title: "Learn Actix".to_string(),
|
||||||
is_completed: false,
|
is_completed: false,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
.service(index)
|
.configure(task::init)
|
||||||
.service(get_task)
|
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8000")?
|
.bind("127.0.0.1:8000")?
|
||||||
.run()
|
.run()
|
||||||
|
|
|
||||||
4
src/task/mod.rs
Normal file
4
src/task/mod.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
pub mod types;
|
||||||
|
pub use types::*;
|
||||||
|
pub mod routes;
|
||||||
|
pub use routes::init;
|
||||||
30
src/task/routes.rs
Normal file
30
src/task/routes.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
use crate::task::types::*;
|
||||||
|
use actix_web::{get, post, web, HttpResponse, Result};
|
||||||
|
|
||||||
|
pub fn init(config: &mut web::ServiceConfig) {
|
||||||
|
config.service(index);
|
||||||
|
config.service(get_task);
|
||||||
|
config.service(add_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))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/")]
|
||||||
|
async fn add_task(form: web::Form<NewTask>, data: web::Data<TaskList>) -> Result<HttpResponse> {
|
||||||
|
let id = data.tasks.len();
|
||||||
|
let new_task = Task::new(id, form.title.clone());
|
||||||
|
// data.tasks.push(new_task);
|
||||||
|
Ok(HttpResponse::Ok().json(new_task))
|
||||||
|
}
|
||||||
28
src/task/types.rs
Normal file
28
src/task/types.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct Task {
|
||||||
|
pub id: usize,
|
||||||
|
pub title: String,
|
||||||
|
pub is_completed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Task {
|
||||||
|
pub fn new(id: usize, title: String) -> Task {
|
||||||
|
Task {
|
||||||
|
id,
|
||||||
|
title,
|
||||||
|
is_completed: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct NewTask {
|
||||||
|
pub title: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct TaskList {
|
||||||
|
pub tasks: Vec<Task>,
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue