wing328 f0b7daeec6 [Rust] rename rust2 to rust-server (#6747)
* rename rust2 to rust-server

* update rust-server batch file to use petstore test spec
2017-10-19 20:17:08 +08:00

59 lines
2.0 KiB
Rust

#![allow(missing_docs)]
extern crate petstore_api;
extern crate iron;
extern crate futures;
extern crate hyper_openssl;
extern crate clap;
extern crate swagger;
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 iron::{Iron, Chain};
use swagger::auth::AllowAllMiddleware;
// Import the module that defines the Server struct.
mod server_lib;
/// 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
ssl.builder_mut().set_private_key_file("examples/server-key.pem", X509_FILETYPE_PEM)?;
ssl.builder_mut().set_certificate_chain_file("examples/server-chain.pem")?;
ssl.builder_mut().check_private_key()?;
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"))
.get_matches();
let server = server_lib::Server{};
let router = petstore_api::router(server);
let mut chain = Chain::new(router);
chain.link_before(petstore_api::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"));
if matches.is_present("https") {
// 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
Iron::new(chain).http("localhost:8080").expect("Failed to start HTTP server");
}
}