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

81 lines
1.7 KiB
Rust
Raw Normal View History

2017-10-07 06:48:14 +02:00
#![allow(dead_code)]
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-07 08:14:13 +02:00
ctx: &mut HttpContext<Self>) -> HttpMessage<Self>
2017-10-07 06:48:14 +02:00
{
if let Some(pl) = payload {
ctx.add_stream(pl);
2017-10-07 09:22:09 +02:00
Self::http_stream(MyRoute{req: Some(req)})
2017-10-07 06:48:14 +02:00
} else {
2017-10-07 09:22:09 +02:00
Self::http_reply(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() {
ctx.start(httpcodes::HTTPOk.into_response(req));
ctx.write_eof();
}
2017-10-07 09:22:09 +02:00
Self::empty()
2017-10-07 06:48:14 +02:00
}
}
fn main() {
let _ = env_logger::init();
let sys = actix::System::new("http-example".to_owned());
let mut routes = RoutingMap::default();
let mut app = HttpApplication::no_state();
app.add("/test")
.get::<MyRoute>()
.post::<MyRoute>();
routes.add("/blah", app);
routes.add_resource("/test")
.post::<MyRoute>();
let http = HttpServer::new(routes);
http.serve::<()>(
&net::SocketAddr::from_str("127.0.0.1:9080").unwrap()).unwrap();
println!("starting");
let _ = sys.run();
}