Skip to content

Return HttpResult<Listening> from serve() #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Possible log types:
### UNRELEASED

- [changed] Removed datastore module, use Redis directly (#10)
- [changed] SpaceapiServer.serve() now returns a HttpResult<Listening> (#16)
- [added] Support status modifiers (#8)

### v0.1.1 (2015-11-16)
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ include = [
redis = "^0.5"
rustc-serialize = "^0.3"
iron = "^0.2"
hyper = "^0.7"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be the possibility to just "use whatever hyper version iron uses"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not :( If you find a way to do this, let me know.

Btw, I think iron should pub use the necessary hyper types anyways. Unfortunately they don't right now. I created an issue.

log = "^0.3"
urlencoded = "^0.2"
router = "^0.0.15"
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
#[macro_use] extern crate error_type;
extern crate rustc_serialize;
extern crate iron;
extern crate hyper;
#[macro_use] extern crate router;
extern crate urlencoded;
extern crate redis;
extern crate spaceapi;

pub use spaceapi as api;
pub use iron::error::HttpResult;
pub use hyper::server::Listening;

mod server;
mod errors;
Expand Down
8 changes: 5 additions & 3 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,17 @@ impl SpaceapiServer {

/// Start a HTTP server listening on ``self.host:self.port``.
///
/// This call is blocking. It can be interrupted with SIGINT (Ctrl+C).
pub fn serve(self) {
/// The call returns an `HttpResult<Listening>` object, see
/// http://ironframework.io/doc/hyper/server/struct.Listening.html
/// for more information.
pub fn serve(self) -> ::HttpResult<::Listening> {
let host = self.host;
let port = self.port;

let router = self.route();

println!("Starting HTTP server on http://{}:{}...", host, port);
Iron::new(router).http((host, port)).unwrap();
Iron::new(router).http((host, port))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay one unwrap() less ✨

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

/// Register a new sensor.
Expand Down
70 changes: 70 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
extern crate spaceapi_server;

use std::net::Ipv4Addr;
use std::net::TcpStream;
use std::io::ErrorKind;

use spaceapi_server::SpaceapiServer;
use spaceapi_server::api;
use spaceapi_server::api::optional::Optional;


/// Create a new status object containing test data.
fn get_status() -> api::Status {
api::Status::new(
"ourspace",
"https://example.com/logo.png",
"https://example.com/",
api::Location {
address: Optional::Value("Street 1, Zürich, Switzerland".into()),
lat: 47.123,
lon: 8.88,
},
api::Contact {
irc: Optional::Absent,
twitter: Optional::Absent,
foursquare: Optional::Absent,
email: Optional::Value("[email protected]".into()),
},
vec![
"email".into(),
"twitter".into(),
],
)
}


/// Create a new SpaceapiServer instance listening on the specified port.
fn get_server(ip: Ipv4Addr, port: u16, status: api::Status) -> SpaceapiServer {
// Start and return a server instance
SpaceapiServer::new(ip, port, status, "redis://127.0.0.1/", vec![]).unwrap()
}


#[test]
fn server_starts() {
//! Test that the spaceapi server starts at all.

// Ip / port for test server
let ip = Ipv4Addr::new(127, 0, 0, 1);
let port = 3344;

// Test data
let status = get_status();

// Connection to port should fail right now
let connect_result = TcpStream::connect((ip, port));
assert!(connect_result.is_err());
assert_eq!(connect_result.unwrap_err().kind(), ErrorKind::ConnectionRefused);

// Instantiate and start server
let server = get_server(ip, port, status);
let mut listening = server.serve().unwrap();

// Connecting to server should work now
let connect_result = TcpStream::connect((ip, port));
assert!(connect_result.is_ok());

// Close server
listening.close().unwrap();
}