diff --git a/Cargo.lock b/Cargo.lock index 14977a4..d59fff6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -296,6 +296,17 @@ dependencies = [ "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]] name = "autocfg" version = "1.0.1" @@ -523,6 +534,19 @@ dependencies = [ "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]] name = "flate2" version = "1.0.19" @@ -758,6 +782,12 @@ version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" +[[package]] +name = "humantime" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a" + [[package]] name = "idna" version = "0.2.0" @@ -1403,6 +1433,15 @@ dependencies = [ "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]] name = "thiserror" version = "1.0.22" @@ -1718,6 +1757,7 @@ name = "web" version = "0.1.0" dependencies = [ "actix-web", + "env_logger", "serde", ] @@ -1755,6 +1795,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 9752bad..70cf5ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2018" [dependencies] actix-web = "3" -serde = "1.0.118" \ No newline at end of file +serde = "1.0.118" +env_logger = "0.8.2" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9154bf6..5ca4cd9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,54 +1,29 @@ -use actix_web::{get, web, HttpResponse, Result}; -use serde::{Deserialize, Serialize}; +use actix_web::{middleware, App, HttpServer}; +use env_logger::Env; -#[derive(Serialize, Deserialize)] -struct Task { - id: u8, - title: String, - is_completed: bool, -} - -#[derive(Serialize, Deserialize)] -struct TaskList { - tasks: Vec, -} - -#[get("/")] -async fn index(data: web::Data) -> Result { - Ok(HttpResponse::Ok().json(&data.tasks)) -} - -#[get("/{id}")] -async fn get_task( - web::Path(id): web::Path, - data: web::Data, -) -> Result { - let task = &data.tasks[id]; - Ok(HttpResponse::Ok().json(task)) -} +mod task; #[actix_web::main] 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(|| { App::new() - .data(TaskList { + .wrap(middleware::Logger::default()) + .data(task::TaskList { tasks: vec![ - Task { + task::Task { id: 0, title: "Learn Rust".to_string(), is_completed: false, }, - Task { + task::Task { id: 1, title: "Learn Actix".to_string(), is_completed: false, }, ], }) - .service(index) - .service(get_task) + .configure(task::init) }) .bind("127.0.0.1:8000")? .run() diff --git a/src/task/mod.rs b/src/task/mod.rs new file mode 100644 index 0000000..6a76e36 --- /dev/null +++ b/src/task/mod.rs @@ -0,0 +1,4 @@ +pub mod types; +pub use types::*; +pub mod routes; +pub use routes::init; diff --git a/src/task/routes.rs b/src/task/routes.rs new file mode 100644 index 0000000..798c719 --- /dev/null +++ b/src/task/routes.rs @@ -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) -> Result { + Ok(HttpResponse::Ok().json(&data.tasks)) +} + +#[get("/{id}")] +async fn get_task( + web::Path(id): web::Path, + data: web::Data, +) -> Result { + let task = &data.tasks[id]; + Ok(HttpResponse::Ok().json(task)) +} + +#[post("/")] +async fn add_task(form: web::Form, data: web::Data) -> Result { + 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)) +} diff --git a/src/task/types.rs b/src/task/types.rs new file mode 100644 index 0000000..320fb8e --- /dev/null +++ b/src/task/types.rs @@ -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, +}