diff options
Diffstat (limited to 'rust/fatcat-api/examples/server.rs')
-rw-r--r-- | rust/fatcat-api/examples/server.rs | 64 |
1 files changed, 27 insertions, 37 deletions
diff --git a/rust/fatcat-api/examples/server.rs b/rust/fatcat-api/examples/server.rs index f9b8617c..79d7367e 100644 --- a/rust/fatcat-api/examples/server.rs +++ b/rust/fatcat-api/examples/server.rs @@ -4,35 +4,32 @@ // Imports required by this file. // extern crate <name of this crate>; -extern crate clap; extern crate fatcat; -extern crate hyper; -extern crate native_tls; -extern crate openssl; extern crate swagger; -extern crate tokio_proto; -extern crate tokio_tls; +extern crate iron; +extern crate hyper_openssl; +extern crate clap; // Imports required by server library. // extern crate fatcat; // extern crate swagger; -extern crate chrono; extern crate futures; +extern crate chrono; #[macro_use] extern crate error_chain; +use hyper_openssl::OpensslServer; +use hyper_openssl::openssl::x509::X509_FILETYPE_PEM; +use hyper_openssl::openssl::ssl::{SslAcceptorBuilder, SslMethod}; +use hyper_openssl::openssl::error::ErrorStack; use clap::{App, Arg}; -use hyper::server::Http; -use openssl::error::ErrorStack; -use openssl::ssl::{SslAcceptorBuilder, SslMethod}; -use openssl::x509::X509_FILETYPE_PEM; -use swagger::auth::AllowAllAuthenticator; -use tokio_proto::TcpServer; +use iron::{Iron, Chain}; +use swagger::auth::AllowAllMiddleware; mod server_lib; -// Builds an SSL implementation for Simple HTTPS from some hard-coded file names -fn ssl() -> Result<SslAcceptorBuilder, ErrorStack> { +/// Builds an SSL implementation for Simple HTTPS from some hard-coded file names +fn ssl() -> Result<OpensslServer, ErrorStack> { let mut ssl = SslAcceptorBuilder::mozilla_intermediate_raw(SslMethod::tls())?; // Server authentication @@ -40,39 +37,32 @@ fn ssl() -> Result<SslAcceptorBuilder, ErrorStack> { ssl.set_certificate_chain_file("examples/server-chain.pem")?; ssl.check_private_key()?; - Ok(ssl) + Ok(OpensslServer::from(ssl.build())) } /// Create custom server, wire it to the autogenerated router, /// and pass it to the web server. fn main() { let matches = App::new("server") - .arg( - Arg::with_name("https") - .long("https") - .help("Whether to use HTTPS or not"), - ) + .arg(Arg::with_name("https") + .long("https") + .help("Whether to use HTTPS or not")) .get_matches(); - let service_fn = fatcat::server::auth::NewService::new(AllowAllAuthenticator::new( - server_lib::NewService, - "cosmo", - )); + let server = server_lib::server().unwrap(); + let router = fatcat::router(server); + + let mut chain = Chain::new(router); + chain.link_before(fatcat::server::ExtractAuthData); + // add authentication middlewares into the chain here + // for the purpose of this example, pretend we have authenticated a user + chain.link_before(AllowAllMiddleware::new("cosmo")); - let addr = "127.0.0.1:8080" - .parse() - .expect("Failed to parse bind address"); if matches.is_present("https") { - let ssl = ssl().expect("Failed to load SSL keys"); - let builder: native_tls::TlsAcceptorBuilder = - native_tls::backend::openssl::TlsAcceptorBuilderExt::from_openssl(ssl); - let tls_acceptor = builder.build().expect("Failed to build TLS acceptor"); - TcpServer::new( - tokio_tls::proto::Server::new(Http::new(), tls_acceptor), - addr, - ).serve(service_fn); + // Using Simple HTTPS + Iron::new(chain).https("localhost:8080", ssl().expect("Failed to load SSL keys")).expect("Failed to start HTTPS server"); } else { // Using HTTP - TcpServer::new(Http::new(), addr).serve(service_fn); + Iron::new(chain).http("localhost:8080").expect("Failed to start HTTP server"); } } |