1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

port more examples

This commit is contained in:
Nikolay Kim 2019-03-09 22:38:15 -08:00
parent b6929b47b1
commit 52c12f264a
9 changed files with 61 additions and 109 deletions

View File

@ -2,10 +2,9 @@
name = "hello-world" name = "hello-world"
version = "0.1.0" version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
workspace = "../" workspace = ".."
edition = "2018"
[dependencies] [dependencies]
env_logger = "0.5" env_logger = "0.6"
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
actix = "0.7"
actix-web = "^0.7"

View File

@ -1,29 +1,21 @@
extern crate actix; use actix_web::{middleware, web, App, HttpRequest, HttpServer};
extern crate actix_web;
extern crate env_logger;
use actix_web::{middleware, server, App, HttpRequest}; fn index(req: HttpRequest) -> &'static str {
println!("REQ: {:?}", req);
fn index(_req: &HttpRequest) -> &'static str {
"Hello world!" "Hello world!"
} }
fn main() { fn main() -> std::io::Result<()> {
::std::env::set_var("RUST_LOG", "actix_web=info"); std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init(); env_logger::init();
let sys = actix::System::new("hello-world");
server::new(|| { HttpServer::new(|| {
App::new() App::new()
// enable logger // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.resource("/index.html", |r| r.f(|_| "Hello world!")) .service(web::resource("/index.html").to(|| "Hello world!"))
.resource("/", |r| r.f(index)) .service(web::resource("/").to(index))
}) })
.bind("127.0.0.1:8080") .bind("127.0.0.1:8080")?
.unwrap() .run()
.start();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
} }

View File

@ -1,5 +1,2 @@
max_width = 89 max_width = 89
reorder_imports = true reorder_imports = true
#wrap_comments = true
fn_args_density = "Compressed"
#use_small_heuristics = false

View File

@ -2,11 +2,12 @@
name = "static_index" name = "static_index"
version = "0.1.0" version = "0.1.0"
authors = ["Jose Marinez <digeratus@gmail.com>"] authors = ["Jose Marinez <digeratus@gmail.com>"]
workspace = "../" workspace = ".."
edition = "2018"
[dependencies] [dependencies]
futures = "0.1" futures = "0.1"
env_logger = "0.5" env_logger = "0.5"
actix = "0.7" actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
actix-web = "0.7" actix-files = { git="https://github.com/actix/actix-web.git", branch = "1.0" }

View File

@ -1,31 +1,19 @@
extern crate actix; use actix_files as fs;
extern crate actix_web; use actix_web::{middleware, App, HttpServer};
extern crate env_logger;
use actix_web::{fs, middleware, server, App}; fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
::std::env::set_var("RUST_BACKTRACE", "1");
env_logger::init(); env_logger::init();
let sys = actix::System::new("static_index"); HttpServer::new(|| {
server::new(|| {
App::new() App::new()
// enable logger // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.handler( .service(
"/", // static files
fs::StaticFiles::new("./static/") fs::Files::new("/", "./static/").index_file("index.html"),
.unwrap()
.index_file("index.html"),
) )
}) })
.bind("127.0.0.1:8080") .bind("127.0.0.1:8080")?
.expect("Can not start server on given IP/Port") .run()
.start();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
} }

View File

@ -2,14 +2,13 @@
name = "template-askama" name = "template-askama"
version = "0.1.0" version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
workspace = "../" workspace = ".."
edition = "2018"
[dependencies] [dependencies]
env_logger = "0.5" env_logger = "0.6"
askama = "0.6" askama = "0.6"
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
actix = "0.7"
actix-web = "0.7"
[build-dependencies] [build-dependencies]
askama = "0.6" askama = "0.6"

View File

@ -1,11 +1,9 @@
extern crate actix;
extern crate actix_web;
#[macro_use] #[macro_use]
extern crate askama; extern crate askama;
use std::collections::HashMap; use std::collections::HashMap;
use actix_web::{http, server, App, HttpResponse, Query, Result}; use actix_web::{web, App, HttpResponse, HttpServer, Result};
use askama::Template; use askama::Template;
#[derive(Template)] #[derive(Template)]
@ -19,7 +17,7 @@ struct UserTemplate<'a> {
#[template(path = "index.html")] #[template(path = "index.html")]
struct Index; struct Index;
fn index(query: Query<HashMap<String, String>>) -> Result<HttpResponse> { fn index(query: web::Query<HashMap<String, String>>) -> Result<HttpResponse> {
let s = if let Some(name) = query.get("name") { let s = if let Some(name) = query.get("name") {
UserTemplate { UserTemplate {
name: name, name: name,
@ -33,17 +31,11 @@ fn index(query: Query<HashMap<String, String>>) -> Result<HttpResponse> {
Ok(HttpResponse::Ok().content_type("text/html").body(s)) Ok(HttpResponse::Ok().content_type("text/html").body(s))
} }
fn main() { fn main() -> std::io::Result<()> {
let sys = actix::System::new("template-askama");
// start http server // start http server
server::new(move || { HttpServer::new(move || {
App::new().resource("/", |r| r.method(http::Method::GET).with(index)) App::new().service(web::resource("/").route(web::get()).to(index))
}) })
.bind("127.0.0.1:8080") .bind("127.0.0.1:8080")?
.unwrap() .run()
.start();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
} }

View File

@ -2,10 +2,10 @@
name = "template-tera" name = "template-tera"
version = "0.1.0" version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
workspace = "../" workspace = ".."
edition = "2018"
[dependencies] [dependencies]
env_logger = "0.5" env_logger = "0.6"
tera = "*" tera = "*"
actix = "0.7" actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
actix-web = "0.7"

View File

@ -1,58 +1,42 @@
extern crate actix;
extern crate actix_web;
extern crate env_logger;
#[macro_use] #[macro_use]
extern crate tera; extern crate tera;
use std::collections::HashMap; use std::collections::HashMap;
use actix_web::{ use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer};
error, http, middleware, server, App, Error, HttpResponse, Query, State,
};
struct AppState {
template: tera::Tera, // <- store tera template in application state
}
// store tera template in application state
fn index( fn index(
(state, query): (State<AppState>, Query<HashMap<String, String>>), tmpl: web::State<tera::Tera>,
query: web::Query<HashMap<String, String>>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let s = if let Some(name) = query.get("name") { let s = if let Some(name) = query.get("name") {
// <- submitted form // submitted form
let mut ctx = tera::Context::new(); let mut ctx = tera::Context::new();
ctx.add("name", &name.to_owned()); ctx.insert("name", &name.to_owned());
ctx.add("text", &"Welcome!".to_owned()); ctx.insert("text", &"Welcome!".to_owned());
state tmpl.render("user.html", &ctx)
.template
.render("user.html", &ctx)
.map_err(|_| error::ErrorInternalServerError("Template error"))? .map_err(|_| error::ErrorInternalServerError("Template error"))?
} else { } else {
state tmpl.render("index.html", &tera::Context::new())
.template
.render("index.html", &tera::Context::new())
.map_err(|_| error::ErrorInternalServerError("Template error"))? .map_err(|_| error::ErrorInternalServerError("Template error"))?
}; };
Ok(HttpResponse::Ok().content_type("text/html").body(s)) Ok(HttpResponse::Ok().content_type("text/html").body(s))
} }
fn main() { fn main() -> std::io::Result<()> {
::std::env::set_var("RUST_LOG", "actix_web=info"); std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init(); env_logger::init();
let sys = actix::System::new("tera-example");
server::new(|| { HttpServer::new(|| {
let tera = let tera =
compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")); compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"));
App::with_state(AppState { template: tera }) App::new()
// enable logger .state(tera)
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default()) // enable logger
.resource("/", |r| r.method(http::Method::GET).with(index)) .service(web::resource("/").route(web::get().to(index)))
}) })
.bind("127.0.0.1:8080") .bind("127.0.0.1:8080")?
.unwrap() .run()
.start();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
} }