forked from loafle/openapi-generator-original
* End use of deprecated openssl method * Enhance rust-server to use hyper 0.11 to support handling operations asynchronously The changes are complete and working (at least for microservices tested within Metaswitch). This isn't completely compatible with the (previous/current) synchronous swagger-codegen. Specifically, * `Client` is no longer `Send + Sync` * Api implementations used by Server are no longer expected to be `Send + Sync` (which is good, because it's quite hard if `Client` isn't) * the code to create `Client`s and `Server`s, and hook them into `hyper` or `tokio` is different. Importantly, though, the business logic itself should be unchanged. * Re-adds the `basePath` element to all server endpoints. This mean clients and servers can talk to each other again. * Fix multipart formdata codegen * Fix up handling of multipart messages * Fix server -> client multipart message response * Correct handling of optional file types * Add authorization header to requests with basic auth * Add client support for `application/x-www-form-urlencoded` * Import uuid library if headers use UUID type * Add BASE_PATH to the server module. * Wrap client connector * Support both query and body parameters on the same operation
75 lines
2.3 KiB
Plaintext
75 lines
2.3 KiB
Plaintext
//! Main binary entry point for {{externCrateName}} implementation.
|
|
|
|
#![allow(missing_docs)]
|
|
|
|
// Imports required by this file.
|
|
// extern crate <name of this crate>;
|
|
extern crate {{externCrateName}};
|
|
extern crate swagger;
|
|
extern crate hyper;
|
|
extern crate openssl;
|
|
extern crate native_tls;
|
|
extern crate tokio_proto;
|
|
extern crate tokio_tls;
|
|
extern crate clap;
|
|
|
|
// Imports required by server library.
|
|
// extern crate {{externCrateName}};
|
|
// extern crate swagger;
|
|
extern crate futures;
|
|
extern crate chrono;
|
|
#[macro_use]
|
|
extern crate error_chain;
|
|
{{#apiUsesUuid}}extern crate uuid;{{/apiUsesUuid}}
|
|
|
|
use openssl::x509::X509_FILETYPE_PEM;
|
|
use openssl::ssl::{SslAcceptorBuilder, SslMethod};
|
|
use openssl::error::ErrorStack;
|
|
use hyper::server::Http;
|
|
use tokio_proto::TcpServer;
|
|
use clap::{App, Arg};
|
|
use swagger::auth::AllowAllAuthenticator;
|
|
|
|
mod server_lib;
|
|
|
|
// Builds an SSL implementation for Simple HTTPS from some hard-coded file names
|
|
fn ssl() -> Result<SslAcceptorBuilder, ErrorStack> {
|
|
let mut ssl = SslAcceptorBuilder::mozilla_intermediate_raw(SslMethod::tls())?;
|
|
|
|
// Server authentication
|
|
ssl.set_private_key_file("examples/server-key.pem", X509_FILETYPE_PEM)?;
|
|
ssl.set_certificate_chain_file("examples/server-chain.pem")?;
|
|
ssl.check_private_key()?;
|
|
|
|
Ok(ssl)
|
|
}
|
|
|
|
/// 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 service_fn =
|
|
{{externCrateName}}::server::auth::NewService::new(
|
|
AllowAllAuthenticator::new(
|
|
server_lib::NewService,
|
|
"cosmo"
|
|
)
|
|
);
|
|
|
|
let addr = "127.0.0.1:{{serverPort}}".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);
|
|
} else {
|
|
// Using HTTP
|
|
TcpServer::new(Http::new(), addr).serve(service_fn);
|
|
}
|
|
}
|