Benjamin Gill 1a831f73f3 [rust-server] Plaintext support; encode params (#7082)
* MMORCH-428: Export the Authorization struct

This is needed so that code can check that the version of Authorization
in the auto-generated code is the same as the version it is using. If
the versions are not exactly the same then the lookup into the TypeMap
will not work.

* Add Rust as a supported language for client and server.

Clarify that there are two Rust client implementations, and one Rust server implementation.

* Percent-encode path and query parameters in client URLs
Fixes #122

Also don't include a question mark at the end of the path when there are no query paramters.
Fixes #121

* Rust2 client: add --host and --port parameters to example client.

Allows the example command-line client to override the default host and port.

* Extract default host and port from Swagger file.

* Derive 'Eq' and 'Ord' on enums

* Rust2: improve server code structure.

    server.rs (main.rs) - main entry point for binary; starts the web server and points it at the server code within the library.
    server_lib/mod.rs (lib.rs) - root of library; creates the server.
    server_lib/server.rs (server.rs) - actual server code

The old server_lib/mod.rs is now server_lib/server.rs; server_lib/mod.rs is new.

This structure is easy to map onto a server implementation; unfortunately we can't get it exactly right here because of the limitations of cargo's examples/ folder.

* Rust2: Explain fully how to use the example code in your project.

* Added plaintext support

* Linting

* MGJ Markups
2017-12-08 18:42:14 +08:00

69 lines
2.2 KiB
Rust

//! Main binary entry point for petstore_api implementation.
#![allow(missing_docs)]
// Imports required by this file.
// extern crate <name of this crate>;
extern crate petstore_api;
extern crate swagger;
extern crate iron;
extern crate hyper_openssl;
extern crate clap;
// Imports required by server library.
// extern crate petstore_api;
// extern crate swagger;
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 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<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().unwrap();
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:80", ssl().expect("Failed to load SSL keys")).expect("Failed to start HTTPS server");
} else {
// Using HTTP
Iron::new(chain).http("localhost:80").expect("Failed to start HTTP server");
}
}