mirror of
https://github.com/actix/examples
synced 2024-11-23 14:31:07 +01:00
port more examples
This commit is contained in:
parent
b6929b47b1
commit
52c12f264a
@ -2,10 +2,9 @@
|
||||
name = "hello-world"
|
||||
version = "0.1.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
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" }
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -1,5 +1,2 @@
|
||||
max_width = 89
|
||||
reorder_imports = true
|
||||
#wrap_comments = true
|
||||
fn_args_density = "Compressed"
|
||||
#use_small_heuristics = false
|
||||
|
@ -2,11 +2,12 @@
|
||||
name = "static_index"
|
||||
version = "0.1.0"
|
||||
authors = ["Jose Marinez <digeratus@gmail.com>"]
|
||||
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" }
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -2,14 +2,13 @@
|
||||
name = "template-askama"
|
||||
version = "0.1.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
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"
|
||||
|
@ -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<HashMap<String, String>>) -> Result<HttpResponse> {
|
||||
fn index(query: web::Query<HashMap<String, String>>) -> Result<HttpResponse> {
|
||||
let s = if let Some(name) = query.get("name") {
|
||||
UserTemplate {
|
||||
name: name,
|
||||
@ -33,17 +31,11 @@ fn index(query: Query<HashMap<String, String>>) -> Result<HttpResponse> {
|
||||
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()
|
||||
}
|
||||
|
@ -2,10 +2,10 @@
|
||||
name = "template-tera"
|
||||
version = "0.1.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
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" }
|
||||
|
@ -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<AppState>, Query<HashMap<String, String>>),
|
||||
tmpl: web::State<tera::Tera>,
|
||||
query: web::Query<HashMap<String, String>>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user