diff options
| author | Teddy Wing | 2020-06-28 18:22:18 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2020-06-28 18:22:18 +0200 | 
| commit | a613320c5f69089feba14ef36de58bdc21dd7e0b (patch) | |
| tree | debae65c668eefe26a9aa9dcd4bff133dfaea97a | |
| parent | 0529b3763ca263f6c45fb632f24cf5d3c5ea0271 (diff) | |
| download | fastcgi-conduit-a613320c5f69089feba14ef36de58bdc21dd7e0b.tar.bz2 | |
Server: Working `start()` implementation
Got a draft version of the server working.
Change the example to use the new Conduit server. Got a working HTML
fragment response.
Need to handle `conduit::Body::File` as well as unwrapped errors.
| -rw-r--r-- | examples/server.rs | 31 | ||||
| -rw-r--r-- | src/lib.rs | 25 | 
2 files changed, 39 insertions, 17 deletions
| diff --git a/examples/server.rs b/examples/server.rs index 7460f56..864c105 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -1,27 +1,26 @@ +extern crate conduit;  extern crate http; +extern crate fastcgi_conduit;  use std::fs::OpenOptions;  use std::io::prelude::*; +use std::io; -use http::{Response, StatusCode}; +use conduit::{Body, RequestExt, Response}; +use conduit::header; -use fcgi; +use fastcgi_conduit::Server;  fn main() { -    fcgi::run(move |req| { -        let mut file = OpenOptions::new() -            .write(true) -            .append(true) -            .open("/tmp/fcgi-log.txt") -            .unwrap(); -        write!(file, "ยป {:?}\n", req).unwrap(); - -        let resp = Response::builder() -            .status(StatusCode::OK) -            .body(()) -            .unwrap(); +    Server::start(handler); +} -        return resp; -    }); +fn handler(req: &mut dyn RequestExt) -> io::Result<Response<Body>> { +    Ok( +        Response::builder() +            .header(header::CONTENT_TYPE, "text/html") +            .body(Body::from_static(b"<h1>Test</h1>")) +            .unwrap() +    )  } @@ -241,10 +241,33 @@ impl<'a> conduit::RequestExt for FastCgiRequest<'a> {  } -struct Server; +pub struct Server;  impl Server {      pub fn start<H: Handler + 'static + Sync>(handler: H) -> io::Result<Server> { +        fastcgi::run(move |mut raw_request| { +            let mut request = FastCgiRequest::new(&mut raw_request).unwrap(); +            let response = handler.call(&mut request); + +            let mut stdout = raw_request.stdout(); + +            let (head, body) = response.unwrap().into_parts(); + +            for (name, value) in head.headers.iter() { +                write!(&mut stdout, "{}: ", name).unwrap(); +                stdout.write(value.as_bytes()).unwrap(); +                stdout.write(b"\r\n").unwrap(); +            } + +            stdout.write(b"\r\n").unwrap(); + +            match body { +                conduit::Body::Static(slice) => stdout.write(slice).map(|_| ()).unwrap(), +                conduit::Body::Owned(vec) => stdout.write(&vec).map(|_| ()).unwrap(), +                conduit::Body::File(file) => (), +            }; +        }); +          Ok(Server{})      }  } | 
