diff --git a/hello-world/Cargo.toml b/hello-world/Cargo.toml index 7126813..cb47b1e 100644 --- a/hello-world/Cargo.toml +++ b/hello-world/Cargo.toml @@ -2,10 +2,9 @@ name = "hello-world" version = "0.1.0" authors = ["Nikolay Kim "] -workspace = "../" +workspace = ".." +edition = "2018" [dependencies] -env_logger = "0.5" - -actix = "0.7" -actix-web = "^0.7" +env_logger = "0.6" +actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } diff --git a/hello-world/src/main.rs b/hello-world/src/main.rs index 2d78783..3bb9064 100644 --- a/hello-world/src/main.rs +++ b/hello-world/src/main.rs @@ -1,29 +1,21 @@ -extern crate actix; -extern crate actix_web; -extern crate env_logger; +use actix_web::{middleware, web, App, HttpRequest, HttpServer}; -use actix_web::{middleware, server, App, HttpRequest}; - -fn index(_req: &HttpRequest) -> &'static str { +fn index(req: HttpRequest) -> &'static str { + println!("REQ: {:?}", req); "Hello world!" } -fn main() { - ::std::env::set_var("RUST_LOG", "actix_web=info"); +fn main() -> std::io::Result<()> { + std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); - let sys = actix::System::new("hello-world"); - server::new(|| { + HttpServer::new(|| { App::new() // enable logger .middleware(middleware::Logger::default()) - .resource("/index.html", |r| r.f(|_| "Hello world!")) - .resource("/", |r| r.f(index)) + .service(web::resource("/index.html").to(|| "Hello world!")) + .service(web::resource("/").to(index)) }) - .bind("127.0.0.1:8080") - .unwrap() - .start(); - - println!("Started http server: 127.0.0.1:8080"); - let _ = sys.run(); + .bind("127.0.0.1:8080")? + .run() } diff --git a/rustfmt.toml b/rustfmt.toml index 4fff285..94bd11d 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,5 +1,2 @@ max_width = 89 reorder_imports = true -#wrap_comments = true -fn_args_density = "Compressed" -#use_small_heuristics = false diff --git a/static_index/Cargo.toml b/static_index/Cargo.toml index 6f42732..ee3d537 100644 --- a/static_index/Cargo.toml +++ b/static_index/Cargo.toml @@ -2,11 +2,12 @@ name = "static_index" version = "0.1.0" authors = ["Jose Marinez "] -workspace = "../" +workspace = ".." +edition = "2018" [dependencies] futures = "0.1" env_logger = "0.5" -actix = "0.7" -actix-web = "0.7" +actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-files = { git="https://github.com/actix/actix-web.git", branch = "1.0" } diff --git a/static_index/src/main.rs b/static_index/src/main.rs index df6233d..e4c4fa8 100644 --- a/static_index/src/main.rs +++ b/static_index/src/main.rs @@ -1,31 +1,19 @@ -extern crate actix; -extern crate actix_web; -extern crate env_logger; +use actix_files as fs; +use actix_web::{middleware, App, HttpServer}; -use actix_web::{fs, middleware, server, App}; - -fn main() { - ::std::env::set_var("RUST_LOG", "actix_web=info"); - ::std::env::set_var("RUST_BACKTRACE", "1"); +fn main() -> std::io::Result<()> { + std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); - let sys = actix::System::new("static_index"); - - server::new(|| { + HttpServer::new(|| { App::new() // enable logger .middleware(middleware::Logger::default()) - .handler( - "/", - fs::StaticFiles::new("./static/") - .unwrap() - .index_file("index.html"), + .service( + // static files + fs::Files::new("/", "./static/").index_file("index.html"), ) }) - .bind("127.0.0.1:8080") - .expect("Can not start server on given IP/Port") - .start(); - - println!("Started http server: 127.0.0.1:8080"); - let _ = sys.run(); + .bind("127.0.0.1:8080")? + .run() } diff --git a/template_askama/Cargo.toml b/template_askama/Cargo.toml index 5eba342..d329742 100644 --- a/template_askama/Cargo.toml +++ b/template_askama/Cargo.toml @@ -2,14 +2,13 @@ name = "template-askama" version = "0.1.0" authors = ["Nikolay Kim "] -workspace = "../" +workspace = ".." +edition = "2018" [dependencies] -env_logger = "0.5" +env_logger = "0.6" askama = "0.6" - -actix = "0.7" -actix-web = "0.7" +actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } [build-dependencies] askama = "0.6" diff --git a/template_askama/src/main.rs b/template_askama/src/main.rs index 27dc761..b27e94a 100644 --- a/template_askama/src/main.rs +++ b/template_askama/src/main.rs @@ -1,11 +1,9 @@ -extern crate actix; -extern crate actix_web; #[macro_use] extern crate askama; use std::collections::HashMap; -use actix_web::{http, server, App, HttpResponse, Query, Result}; +use actix_web::{web, App, HttpResponse, HttpServer, Result}; use askama::Template; #[derive(Template)] @@ -19,7 +17,7 @@ struct UserTemplate<'a> { #[template(path = "index.html")] struct Index; -fn index(query: Query>) -> Result { +fn index(query: web::Query>) -> Result { let s = if let Some(name) = query.get("name") { UserTemplate { name: name, @@ -33,17 +31,11 @@ fn index(query: Query>) -> Result { Ok(HttpResponse::Ok().content_type("text/html").body(s)) } -fn main() { - let sys = actix::System::new("template-askama"); - +fn main() -> std::io::Result<()> { // start http server - server::new(move || { - App::new().resource("/", |r| r.method(http::Method::GET).with(index)) + HttpServer::new(move || { + App::new().service(web::resource("/").route(web::get()).to(index)) }) - .bind("127.0.0.1:8080") - .unwrap() - .start(); - - println!("Started http server: 127.0.0.1:8080"); - let _ = sys.run(); + .bind("127.0.0.1:8080")? + .run() } diff --git a/template_tera/Cargo.toml b/template_tera/Cargo.toml index 9e55bf4..2c01582 100644 --- a/template_tera/Cargo.toml +++ b/template_tera/Cargo.toml @@ -2,10 +2,10 @@ name = "template-tera" version = "0.1.0" authors = ["Nikolay Kim "] -workspace = "../" +workspace = ".." +edition = "2018" [dependencies] -env_logger = "0.5" +env_logger = "0.6" tera = "*" -actix = "0.7" -actix-web = "0.7" +actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } diff --git a/template_tera/src/main.rs b/template_tera/src/main.rs index 7cbd557..b442e82 100644 --- a/template_tera/src/main.rs +++ b/template_tera/src/main.rs @@ -1,58 +1,42 @@ -extern crate actix; -extern crate actix_web; -extern crate env_logger; #[macro_use] extern crate tera; use std::collections::HashMap; -use actix_web::{ - error, http, middleware, server, App, Error, HttpResponse, Query, State, -}; - -struct AppState { - template: tera::Tera, // <- store tera template in application state -} +use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer}; +// store tera template in application state fn index( - (state, query): (State, Query>), + tmpl: web::State, + query: web::Query>, ) -> Result { let s = if let Some(name) = query.get("name") { - // <- submitted form + // submitted form let mut ctx = tera::Context::new(); - ctx.add("name", &name.to_owned()); - ctx.add("text", &"Welcome!".to_owned()); - state - .template - .render("user.html", &ctx) + ctx.insert("name", &name.to_owned()); + ctx.insert("text", &"Welcome!".to_owned()); + tmpl.render("user.html", &ctx) .map_err(|_| error::ErrorInternalServerError("Template error"))? } else { - state - .template - .render("index.html", &tera::Context::new()) + tmpl.render("index.html", &tera::Context::new()) .map_err(|_| error::ErrorInternalServerError("Template error"))? }; Ok(HttpResponse::Ok().content_type("text/html").body(s)) } -fn main() { - ::std::env::set_var("RUST_LOG", "actix_web=info"); +fn main() -> std::io::Result<()> { + std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); - let sys = actix::System::new("tera-example"); - server::new(|| { + HttpServer::new(|| { let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")); - App::with_state(AppState { template: tera }) - // enable logger - .middleware(middleware::Logger::default()) - .resource("/", |r| r.method(http::Method::GET).with(index)) + App::new() + .state(tera) + .middleware(middleware::Logger::default()) // enable logger + .service(web::resource("/").route(web::get().to(index))) }) - .bind("127.0.0.1:8080") - .unwrap() - .start(); - - println!("Started http server: 127.0.0.1:8080"); - let _ = sys.run(); + .bind("127.0.0.1:8080")? + .run() }