1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

pass request by ref; added middleware support

This commit is contained in:
Nikolay Kim
2017-10-21 22:59:09 -07:00
parent 7364e088be
commit afe9459ce1
17 changed files with 470 additions and 110 deletions

View File

@ -6,13 +6,13 @@ extern crate env_logger;
use actix_web::*;
/// somple handle
fn index(req: HttpRequest, payload: Payload, state: &()) -> HttpResponse {
fn index(req: &mut HttpRequest, payload: Payload, state: &()) -> HttpResponse {
println!("{:?}", req);
httpcodes::HTTPOk.into()
}
/// handle with path parameters like `/name/{name}/`
fn with_param(req: HttpRequest, payload: Payload, state: &()) -> HttpResponse {
fn with_param(req: &mut HttpRequest, payload: Payload, state: &()) -> HttpResponse {
println!("{:?}", req);
HttpResponse::builder(StatusCode::OK)
@ -22,10 +22,14 @@ fn with_param(req: HttpRequest, payload: Payload, state: &()) -> HttpResponse {
}
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
let _ = env_logger::init();
let sys = actix::System::new("ws-example");
HttpServer::new(
Application::default("/")
// enable logger
.middleware(Logger::new(None))
// register simple handler, handle all methods
.handler("/index.html", index)
// with path parameters

View File

@ -1,6 +1,7 @@
#![allow(unused_variables)]
extern crate actix;
extern crate actix_web;
extern crate env_logger;
use actix::*;
use actix_web::*;
@ -15,7 +16,8 @@ impl Actor for MyWebSocket {
impl Route for MyWebSocket {
type State = ();
fn request(req: HttpRequest, payload: Payload, ctx: &mut HttpContext<Self>) -> Reply<Self>
fn request(req: &mut HttpRequest,
payload: Payload, ctx: &mut HttpContext<Self>) -> Reply<Self>
{
match ws::handshake(&req) {
Ok(resp) => {
@ -59,12 +61,17 @@ impl Handler<ws::Message> for MyWebSocket {
}
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
let _ = env_logger::init();
let sys = actix::System::new("ws-example");
HttpServer::new(
Application::default("/")
// enable logger
.middleware(Logger::new(None))
// websocket route
.resource("/ws/", |r| r.get::<MyWebSocket>())
.route_handler("/", StaticFiles::new("static/", true)))
.route_handler("/", StaticFiles::new("examples/static/", true)))
.serve::<_, ()>("127.0.0.1:8080").unwrap();
println!("Started http server: 127.0.0.1:8080");

View File

@ -1,12 +0,0 @@
[package]
name = "websocket-example"
version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
[[bin]]
name = "websocket"
path = "src/main.rs"
[dependencies]
actix = { git = "https://github.com/fafhrd91/actix.git" }
actix-web = { path = "../../" }