1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-18 22:01:50 +01:00
actix-web/src/main.rs

130 lines
3.2 KiB
Rust
Raw Normal View History

2017-10-13 18:58:22 -07:00
// #![feature(try_trait)]
2017-10-07 21:48:00 -07:00
#![allow(dead_code, unused_variables)]
2017-10-06 21:48:14 -07:00
extern crate actix;
2017-10-14 07:59:35 -07:00
extern crate actix_web;
2017-10-06 21:48:14 -07:00
extern crate tokio_core;
extern crate env_logger;
use std::net;
use std::str::FromStr;
use actix::prelude::*;
2017-10-14 07:59:35 -07:00
use actix_web::*;
2017-10-06 21:48:14 -07:00
struct MyRoute {req: Option<HttpRequest>}
impl Actor for MyRoute {
type Context = HttpContext<Self>;
}
impl Route for MyRoute {
type State = ();
2017-10-08 20:16:48 -07:00
fn request(req: HttpRequest, payload: Payload, ctx: &mut HttpContext<Self>) -> Reply<Self> {
if !payload.eof() {
ctx.add_stream(payload);
2017-10-08 14:56:51 -07:00
Reply::stream(MyRoute{req: Some(req)})
2017-10-06 21:48:14 -07:00
} else {
Reply::reply(httpcodes::HTTPOk)
2017-10-06 21:48:14 -07:00
}
}
}
impl ResponseType<PayloadItem> for MyRoute {
type Item = ();
type Error = ();
}
2017-10-08 20:16:48 -07:00
impl StreamHandler<PayloadItem> for MyRoute {}
2017-10-06 21:48:14 -07:00
impl Handler<PayloadItem> for MyRoute {
fn handle(&mut self, msg: PayloadItem, ctx: &mut HttpContext<Self>)
-> Response<Self, PayloadItem>
{
println!("CHUNK: {:?}", msg);
if let Some(req) = self.req.take() {
ctx.start(httpcodes::HTTPOk);
2017-10-06 21:48:14 -07:00
ctx.write_eof();
}
2017-10-07 00:22:09 -07:00
Self::empty()
2017-10-06 21:48:14 -07:00
}
}
2017-10-07 21:48:00 -07:00
struct MyWS {}
impl Actor for MyWS {
type Context = HttpContext<Self>;
}
impl Route for MyWS {
type State = ();
fn request(req: HttpRequest, payload: Payload, ctx: &mut HttpContext<Self>) -> Reply<Self>
{
2017-10-13 18:54:02 -07:00
match ws::handshake(&req) {
Ok(resp) => {
ctx.start(resp);
ctx.add_stream(ws::WsStream::new(payload));
Reply::stream(MyWS{})
}
Err(err) => {
Reply::reply(err)
}
}
2017-10-07 21:48:00 -07:00
}
}
impl ResponseType<ws::Message> for MyWS {
type Item = ();
type Error = ();
}
impl StreamHandler<ws::Message> for MyWS {}
impl Handler<ws::Message> for MyWS {
fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>)
-> Response<Self, ws::Message>
{
println!("WS: {:?}", msg);
match msg {
ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, msg),
ws::Message::Text(text) => ws::WsWriter::text(ctx, text),
ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin),
2017-10-13 16:33:23 -07:00
ws::Message::Closed | ws::Message::Error => {
ctx.stop();
}
2017-10-07 21:48:00 -07:00
_ => (),
}
Self::empty()
}
}
2017-10-06 21:48:14 -07:00
fn main() {
let _ = env_logger::init();
2017-10-08 00:14:52 -07:00
let sys = actix::System::new("http-example");
2017-10-06 21:48:14 -07:00
2017-10-15 14:17:41 -07:00
HttpServer::new(
RoutingMap::default()
.app(
"/blah", Application::default()
.resource("/test", |r| {
r.get::<MyRoute>();
r.post::<MyRoute>();
})
.finish())
.resource("/test", |r| r.post::<MyRoute>())
.resource("/ws/", |r| r.get::<MyWS>())
.resource("/simple/", |r|
r.handler(Method::GET, |req, payload, state| {
httpcodes::HTTPOk
}))
.finish())
.serve::<()>(
&net::SocketAddr::from_str("127.0.0.1:9080").unwrap()).unwrap();
2017-10-06 21:48:14 -07:00
println!("starting");
let _ = sys.run();
}