mirror of
https://github.com/actix/examples
synced 2025-02-24 18:13:21 +01:00
update basic example
This commit is contained in:
parent
5c8aaba5f9
commit
71e83fb87c
@ -11,7 +11,7 @@ dotenv = "0.10"
|
|||||||
env_logger = "0.5"
|
env_logger = "0.5"
|
||||||
failure = "0.1.1"
|
failure = "0.1.1"
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
num_cpus = "1.8.0"
|
num_cpus = "1.10.0"
|
||||||
r2d2 = "0.8.2"
|
r2d2 = "0.8.2"
|
||||||
r2d2_sqlite = "0.5.0"
|
r2d2_sqlite = "0.5.0"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "basics"
|
name = "basics"
|
||||||
version = "0.1.0"
|
version = "1.0.0"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
workspace = "../"
|
workspace = "../"
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix = "0.7"
|
actix-rt = "0.1"
|
||||||
actix-web = "0.7"
|
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||||
|
actix-session = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||||
|
actix-staticfiles = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||||
|
|
||||||
futures = "0.1"
|
futures = "0.1.25"
|
||||||
env_logger = "0.5"
|
env_logger = "0.5"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
|
@ -1,43 +1,34 @@
|
|||||||
#![allow(unused_variables)]
|
|
||||||
#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
|
|
||||||
|
|
||||||
extern crate actix;
|
|
||||||
extern crate actix_web;
|
|
||||||
extern crate bytes;
|
|
||||||
extern crate env_logger;
|
|
||||||
extern crate futures;
|
|
||||||
|
|
||||||
use bytes::Bytes;
|
|
||||||
use futures::sync::mpsc;
|
|
||||||
use futures::Stream;
|
|
||||||
|
|
||||||
use actix_web::http::{header, Method, StatusCode};
|
|
||||||
use actix_web::middleware::session::{self, RequestSession};
|
|
||||||
use actix_web::{
|
|
||||||
error, fs, middleware, pred, server, App, Error, HttpRequest, HttpResponse, Path,
|
|
||||||
Result,
|
|
||||||
};
|
|
||||||
use futures::future::{result, FutureResult};
|
|
||||||
use std::{env, io};
|
use std::{env, io};
|
||||||
|
|
||||||
|
use actix_session::{CookieSession, Session};
|
||||||
|
use actix_staticfiles as fs;
|
||||||
|
use actix_web::extract::Path;
|
||||||
|
use actix_web::http::{header, Method, StatusCode};
|
||||||
|
use actix_web::{
|
||||||
|
error, guard, web, App, Error, HttpRequest, HttpResponse, HttpServer, Result,
|
||||||
|
};
|
||||||
|
use bytes::Bytes;
|
||||||
|
use futures::unsync::mpsc;
|
||||||
|
use futures::{future::ok, Future, Stream};
|
||||||
|
|
||||||
/// favicon handler
|
/// favicon handler
|
||||||
fn favicon(req: &HttpRequest) -> Result<fs::NamedFile> {
|
fn favicon() -> Result<fs::NamedFile> {
|
||||||
Ok(fs::NamedFile::open("static/favicon.ico")?)
|
Ok(fs::NamedFile::open("static/favicon.ico")?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// simple index handler
|
/// simple index handler
|
||||||
fn welcome(req: &HttpRequest) -> Result<HttpResponse> {
|
fn welcome(session: Session, req: HttpRequest) -> Result<HttpResponse> {
|
||||||
println!("{:?}", req);
|
println!("{:?}", req);
|
||||||
|
|
||||||
// session
|
// session
|
||||||
let mut counter = 1;
|
let mut counter = 1;
|
||||||
if let Some(count) = req.session().get::<i32>("counter")? {
|
if let Some(count) = session.get::<i32>("counter")? {
|
||||||
println!("SESSION value: {}", count);
|
println!("SESSION value: {}", count);
|
||||||
counter = count + 1;
|
counter = count + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// set counter to session
|
// set counter to session
|
||||||
req.session().set("counter", counter)?;
|
session.set("counter", counter)?;
|
||||||
|
|
||||||
// response
|
// response
|
||||||
Ok(HttpResponse::build(StatusCode::OK)
|
Ok(HttpResponse::build(StatusCode::OK)
|
||||||
@ -46,17 +37,17 @@ fn welcome(req: &HttpRequest) -> Result<HttpResponse> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 404 handler
|
/// 404 handler
|
||||||
fn p404(req: &HttpRequest) -> Result<fs::NamedFile> {
|
fn p404() -> Result<fs::NamedFile> {
|
||||||
Ok(fs::NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND))
|
Ok(fs::NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// async handler
|
/// async handler
|
||||||
fn index_async(req: &HttpRequest) -> FutureResult<HttpResponse, Error> {
|
fn index_async(req: HttpRequest) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
println!("{:?}", req);
|
println!("{:?}", req);
|
||||||
|
|
||||||
result(Ok(HttpResponse::Ok().content_type("text/html").body(
|
ok(HttpResponse::Ok()
|
||||||
format!("Hello {}!", req.match_info().get("name").unwrap()),
|
.content_type("text/html")
|
||||||
)))
|
.body(format!("Hello {}!", req.match_info().get("name").unwrap())))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// async body
|
/// async body
|
||||||
@ -67,76 +58,86 @@ fn index_async_body(path: Path<String>) -> HttpResponse {
|
|||||||
let _ = tx.unbounded_send(Bytes::from(text.as_bytes()));
|
let _ = tx.unbounded_send(Bytes::from(text.as_bytes()));
|
||||||
|
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.streaming(rx_body.map_err(|e| error::ErrorBadRequest("bad request")))
|
.streaming(rx_body.map_err(|_| error::ErrorBadRequest("bad request")))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// handler with path parameters like `/user/{name}/`
|
/// handler with path parameters like `/user/{name}/`
|
||||||
fn with_param(req: &HttpRequest) -> HttpResponse {
|
fn with_param(req: HttpRequest, path: Path<(String,)>) -> HttpResponse {
|
||||||
println!("{:?}", req);
|
println!("{:?}", req);
|
||||||
|
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.content_type("text/plain")
|
.content_type("text/plain")
|
||||||
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))
|
.body(format!("Hello {}!", path.0))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> io::Result<()> {
|
||||||
env::set_var("RUST_LOG", "actix_web=debug");
|
env::set_var("RUST_LOG", "actix_web=debug");
|
||||||
env::set_var("RUST_BACKTRACE", "1");
|
env::set_var("RUST_BACKTRACE", "1");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let sys = actix::System::new("basic-example");
|
let sys = actix_rt::System::new("basic-example");
|
||||||
|
|
||||||
let addr = server::new(
|
HttpServer::new(|| {
|
||||||
|| App::new()
|
App::new()
|
||||||
// enable logger
|
// enable logger
|
||||||
.middleware(middleware::Logger::default())
|
// .middleware(middleware::Logger::default())
|
||||||
// cookie session middleware
|
// cookie session middleware
|
||||||
.middleware(session::SessionStorage::new(
|
.middleware(CookieSession::signed(&[0; 32]).secure(false))
|
||||||
session::CookieSessionBackend::signed(&[0; 32]).secure(false)
|
|
||||||
))
|
|
||||||
// register favicon
|
// register favicon
|
||||||
.resource("/favicon", |r| r.f(favicon))
|
.resource("/favicon", |r| r.to(favicon))
|
||||||
// register simple route, handle all methods
|
// register simple route, handle all methods
|
||||||
.resource("/welcome", |r| r.f(welcome))
|
.resource("/welcome", |r| r.to(welcome))
|
||||||
// with path parameters
|
// with path parameters
|
||||||
.resource("/user/{name}", |r| r.method(Method::GET).f(with_param))
|
.resource("/user/{name}", |r| r.route(web::get().to(with_param)))
|
||||||
// async handler
|
// async handler
|
||||||
.resource("/async/{name}", |r| r.method(Method::GET).a(index_async))
|
.resource("/async/{name}", |r| {
|
||||||
|
r.route(web::get().to_async(index_async))
|
||||||
|
})
|
||||||
// async handler
|
// async handler
|
||||||
.resource("/async-body/{name}", |r| r.method(Method::GET).with(index_async_body))
|
.resource("/async-body/{name}", |r| {
|
||||||
.resource("/test", |r| r.f(|req| {
|
r.route(web::get().to(index_async_body))
|
||||||
match *req.method() {
|
})
|
||||||
|
.resource("/test", |r| {
|
||||||
|
r.to(|req: HttpRequest| match *req.method() {
|
||||||
Method::GET => HttpResponse::Ok(),
|
Method::GET => HttpResponse::Ok(),
|
||||||
Method::POST => HttpResponse::MethodNotAllowed(),
|
Method::POST => HttpResponse::MethodNotAllowed(),
|
||||||
_ => HttpResponse::NotFound(),
|
_ => HttpResponse::NotFound(),
|
||||||
}
|
})
|
||||||
}))
|
})
|
||||||
.resource("/error", |r| r.f(|req| {
|
.resource("/error", |r| {
|
||||||
|
r.to(|| {
|
||||||
error::InternalError::new(
|
error::InternalError::new(
|
||||||
io::Error::new(io::ErrorKind::Other, "test"), StatusCode::INTERNAL_SERVER_ERROR)
|
io::Error::new(io::ErrorKind::Other, "test"),
|
||||||
}))
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
// static files
|
// static files
|
||||||
.handler("/static", fs::StaticFiles::new("static").unwrap())
|
.service(fs::StaticFiles::new("/static", "static").unwrap())
|
||||||
// redirect
|
// redirect
|
||||||
.resource("/", |r| r.method(Method::GET).f(|req| {
|
.resource("/", |r| {
|
||||||
|
r.route(web::get().to(|req: HttpRequest| {
|
||||||
println!("{:?}", req);
|
println!("{:?}", req);
|
||||||
HttpResponse::Found()
|
HttpResponse::Found()
|
||||||
.header(header::LOCATION, "static/welcome.html")
|
.header(header::LOCATION, "static/welcome.html")
|
||||||
.finish()
|
.finish()
|
||||||
}))
|
}))
|
||||||
|
})
|
||||||
// default
|
// default
|
||||||
.default_resource(|r| {
|
.default_resource(|r| {
|
||||||
// 404 for GET request
|
// 404 for GET request
|
||||||
r.method(Method::GET).f(p404);
|
r.route(web::get().to(p404))
|
||||||
|
|
||||||
// all requests that are not `GET`
|
// all requests that are not `GET`
|
||||||
r.route().filter(pred::Not(pred::Get())).f(
|
.route(
|
||||||
|req| HttpResponse::MethodNotAllowed());
|
web::route()
|
||||||
}))
|
.guard(guard::Not(guard::Get()))
|
||||||
|
.to(|| HttpResponse::MethodNotAllowed()),
|
||||||
.bind("127.0.0.1:8080").expect("Can not bind to 127.0.0.1:8080")
|
)
|
||||||
.shutdown_timeout(0) // <- Set shutdown timeout to 0 seconds (default 60s)
|
})
|
||||||
|
})
|
||||||
|
.bind("127.0.0.1:8080")?
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
println!("Starting http server: 127.0.0.1:8080");
|
println!("Starting http server: 127.0.0.1:8080");
|
||||||
let _ = sys.run();
|
let _ = sys.run();
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user