1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 01:52:57 +01:00
actix-web/src/main.rs

135 lines
3.1 KiB
Rust
Raw Normal View History

2017-10-08 06:48:00 +02:00
#![allow(dead_code, unused_variables)]
2017-10-07 06:48:14 +02:00
extern crate actix;
extern crate actix_http;
extern crate tokio_core;
extern crate env_logger;
use std::net;
use std::str::FromStr;
use actix::prelude::*;
use actix_http::*;
struct MyRoute {req: Option<HttpRequest>}
impl Actor for MyRoute {
type Context = HttpContext<Self>;
}
impl Route for MyRoute {
type State = ();
fn request(req: HttpRequest,
payload: Option<Payload>,
2017-10-08 23:56:51 +02:00
ctx: &mut HttpContext<Self>) -> Reply<Self>
2017-10-07 06:48:14 +02:00
{
if let Some(pl) = payload {
ctx.add_stream(pl);
2017-10-08 23:56:51 +02:00
Reply::stream(MyRoute{req: Some(req)})
2017-10-07 06:48:14 +02:00
} else {
2017-10-08 23:56:51 +02:00
Reply::with(req, httpcodes::HTTPOk)
2017-10-07 06:48:14 +02:00
}
}
}
impl ResponseType<PayloadItem> for MyRoute {
type Item = ();
type Error = ();
}
impl StreamHandler<PayloadItem, ()> for MyRoute {}
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() {
2017-10-08 06:48:00 +02:00
ctx.start(httpcodes::HTTPOk.response(req));
2017-10-07 06:48:14 +02:00
ctx.write_eof();
}
2017-10-07 09:22:09 +02:00
Self::empty()
2017-10-07 06:48:14 +02:00
}
}
2017-10-08 06:48:00 +02:00
struct MyWS {}
impl Actor for MyWS {
type Context = HttpContext<Self>;
}
impl Route for MyWS {
type State = ();
fn request(req: HttpRequest,
payload: Option<Payload>,
2017-10-08 23:56:51 +02:00
ctx: &mut HttpContext<Self>) -> Reply<Self>
2017-10-08 06:48:00 +02:00
{
if let Some(payload) = payload {
2017-10-08 07:41:02 +02:00
match ws::handshake(req) {
2017-10-08 06:48:00 +02:00
Ok(resp) => {
ctx.start(resp);
ctx.add_stream(ws::WsStream::new(payload));
2017-10-08 23:56:51 +02:00
Reply::stream(MyWS{})
2017-10-08 06:48:00 +02:00
},
Err(err) =>
2017-10-08 23:56:51 +02:00
Reply::reply(err)
2017-10-08 06:48:00 +02:00
}
} else {
2017-10-08 23:56:51 +02:00
Reply::with(req, httpcodes::HTTPBadRequest)
2017-10-08 06:48:00 +02: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),
_ => (),
}
Self::empty()
}
}
2017-10-07 06:48:14 +02:00
fn main() {
let _ = env_logger::init();
2017-10-08 09:14:52 +02:00
let sys = actix::System::new("http-example");
2017-10-07 06:48:14 +02:00
let mut routes = RoutingMap::default();
2017-10-08 23:56:51 +02:00
let mut app = Application::default();
2017-10-07 06:48:14 +02:00
app.add("/test")
.get::<MyRoute>()
.post::<MyRoute>();
routes.add("/blah", app);
routes.add_resource("/test")
.post::<MyRoute>();
2017-10-08 06:48:00 +02:00
routes.add_resource("/ws/")
.get::<MyWS>();
2017-10-07 06:48:14 +02:00
let http = HttpServer::new(routes);
http.serve::<()>(
&net::SocketAddr::from_str("127.0.0.1:9080").unwrap()).unwrap();
println!("starting");
let _ = sys.run();
}