Skip to content
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

Restructure HttpServer's API #81

Merged
merged 20 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from 14 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
36 changes: 36 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,42 @@ https://github.com/oxidecomputer/dropshot/compare/v0.4.0\...HEAD[Full list of co
// configured in release.toml. DO NOT change the format of the headers or the
// list of raw commits.

`HttpServer` has been split into two objects, `HttpServerStarter`, and
davepacheco marked this conversation as resolved.
Show resolved Hide resolved
`HttpServer`.
- In the old implementation, `HttpServer` represented both a pending and
running server. Callers were expected to invoke `run()` to begin execution of
the old server.
- In the new implementation, `HttpServerStarter` may be used to construct a
server, and `HttpServer` represents the running server. Invoking
`HttpServerStarter::start()` creates and `HttpServer` object, which represents
the new server.

`HttpServer` now implements `Future`, and does not directly expose a
`tokio::JoinHandle`.
- In the old implementation, `HttpServer` returned a `tokio::JoinHandle`, and
callers were expected to invoke `wait_for_shutdown` to await the completion
of a server.
- In the new implementation, `HttpServer` implements `Future`, and may be
`await`-ed directly.

As an example:

```rust
// Old Version:
let mut server = HttpServer::new( /* Arguments are the same between versions */ )
.map_err(|error| format!("failed to start server: {}", error))?;

let server_task = server.run();
server.wait_for_shutdown(server_task).await;

// New Version
let server = HttpServerStarter::new( /* Arguments are the same between versions */ )
.map_err(|error| format!("failed to start server: {}", error))?
.start();

server.await;
```

== 0.4.0 (released 2021-02-01)

https://github.com/oxidecomputer/dropshot/compare/v0.3.0\...v0.4.0[Full list of commits]
Expand Down
11 changes: 6 additions & 5 deletions dropshot/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use dropshot::ConfigLoggingLevel;
use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::HttpResponseUpdatedNoContent;
use dropshot::HttpServer;
use dropshot::HttpServerStarter;
use dropshot::RequestContext;
use dropshot::TypedBody;
use schemars::JsonSchema;
Expand Down Expand Up @@ -58,15 +58,16 @@ async fn main() -> Result<(), String> {
/*
* Set up the server.
*/
let mut server = HttpServer::new(&config_dropshot, api, api_context, &log)
.map_err(|error| format!("failed to create server: {}", error))?;
let server_task = server.run();
let server =
HttpServerStarter::new(&config_dropshot, api, api_context, &log)
.map_err(|error| format!("failed to create server: {}", error))?
.start();

/*
* Wait for the server to stop. Note that there's not any code to shut down
* this server, so we should never get past this point.
*/
server.wait_for_shutdown(server_task).await
server.await
}

/**
Expand Down
11 changes: 6 additions & 5 deletions dropshot/examples/module-basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use dropshot::ApiDescription;
use dropshot::ConfigLogging;
use dropshot::ConfigLoggingLevel;
use dropshot::HttpServer;
use dropshot::HttpServerStarter;
use dropshot::RequestContext;
use schemars::JsonSchema;
use serde::Deserialize;
Expand Down Expand Up @@ -51,15 +51,16 @@ async fn main() -> Result<(), String> {
/*
* Set up the server.
*/
let mut server = HttpServer::new(&config_dropshot, api, api_context, &log)
.map_err(|error| format!("failed to create server: {}", error))?;
let server_task = server.run();
let server =
HttpServerStarter::new(&config_dropshot, api, api_context, &log)
.map_err(|error| format!("failed to create server: {}", error))?
.start();

/*
* Wait for the server to stop. Note that there's not any code to shut down
* this server, so we should never get past this point.
*/
server.wait_for_shutdown(server_task).await
server.await
}

/**
Expand Down
10 changes: 5 additions & 5 deletions dropshot/examples/pagination-basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use dropshot::ConfigLoggingLevel;
use dropshot::EmptyScanParams;
use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::HttpServer;
use dropshot::HttpServerStarter;
use dropshot::PaginationParams;
use dropshot::Query;
use dropshot::RequestContext;
Expand Down Expand Up @@ -153,8 +153,8 @@ async fn main() -> Result<(), String> {
.map_err(|error| format!("failed to create logger: {}", error))?;
let mut api = ApiDescription::new();
api.register(example_list_projects).unwrap();
let mut server = HttpServer::new(&config_dropshot, api, ctx, &log)
.map_err(|error| format!("failed to create server: {}", error))?;
let server_task = server.run();
server.wait_for_shutdown(server_task).await
let server = HttpServerStarter::new(&config_dropshot, api, ctx, &log)
.map_err(|error| format!("failed to create server: {}", error))?
.start();
server.await
}
11 changes: 6 additions & 5 deletions dropshot/examples/pagination-multiple-resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use dropshot::ConfigLogging;
use dropshot::ConfigLoggingLevel;
use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::HttpServer;
use dropshot::HttpServerStarter;
use dropshot::PaginationOrder;
use dropshot::PaginationOrder::Ascending;
use dropshot::PaginationOrder::Descending;
Expand Down Expand Up @@ -307,10 +307,11 @@ async fn main() -> Result<(), String> {
api.register(example_list_projects).unwrap();
api.register(example_list_disks).unwrap();
api.register(example_list_instances).unwrap();
let mut server = HttpServer::new(&config_dropshot, api, ctx, &log)
.map_err(|error| format!("failed to create server: {}", error))?;
let server_task = server.run();
server.wait_for_shutdown(server_task).await
let server = HttpServerStarter::new(&config_dropshot, api, ctx, &log)
.map_err(|error| format!("failed to create server: {}", error))?
.start();

server.await
}

fn rqctx_to_data(rqctx: Arc<RequestContext>) -> Arc<DataCollection> {
Expand Down
10 changes: 5 additions & 5 deletions dropshot/examples/pagination-multiple-sorts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ use dropshot::ConfigLogging;
use dropshot::ConfigLoggingLevel;
use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::HttpServer;
use dropshot::HttpServerStarter;
use dropshot::PaginationOrder;
use dropshot::PaginationOrder::Ascending;
use dropshot::PaginationOrder::Descending;
Expand Down Expand Up @@ -326,16 +326,16 @@ async fn main() -> Result<(), String> {
.map_err(|error| format!("failed to create logger: {}", error))?;
let mut api = ApiDescription::new();
api.register(example_list_projects).unwrap();
let mut server = HttpServer::new(&config_dropshot, api, ctx, &log)
.map_err(|error| format!("failed to create server: {}", error))?;
let server_task = server.run();
let server = HttpServerStarter::new(&config_dropshot, api, ctx, &log)
.map_err(|error| format!("failed to create server: {}", error))?
.start();

/*
* Print out some example requests to start with.
*/
print_example_requests(log, &server.local_addr());

server.wait_for_shutdown(server_task).await
server.await
}

fn print_example_requests(log: slog::Logger, addr: &SocketAddr) {
Expand Down
16 changes: 8 additions & 8 deletions dropshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* use dropshot::ConfigDropshot;
* use dropshot::ConfigLogging;
* use dropshot::ConfigLoggingLevel;
* use dropshot::HttpServer;
* use dropshot::HttpServerStarter;
* use std::sync::Arc;
*
* #[tokio::main]
Expand All @@ -61,12 +61,12 @@
* .map_err(|e| e.to_string())?;
*
* // Describe the API.
* let mut api = ApiDescription::new();
* let api = ApiDescription::new();
* // Register API functions -- see detailed example or ApiDescription docs.
*
* // Start the server.
* let mut server =
* HttpServer::new(
* let server =
* HttpServerStarter::new(
* &ConfigDropshot {
* bind_address: "127.0.0.1:0".parse().unwrap(),
* request_body_max_bytes: 1024,
Expand All @@ -75,10 +75,10 @@
* Arc::new(()),
* &log,
* )
* .map_err(|error| format!("failed to start server: {}", error))?;
* .map_err(|error| format!("failed to start server: {}", error))?
* .start();
*
* let server_task = server.run();
* server.wait_for_shutdown(server_task).await
* server.await
* }
* ```
*
Expand Down Expand Up @@ -542,7 +542,7 @@ pub use pagination::PaginationOrder;
pub use pagination::PaginationParams;
pub use pagination::ResultsPage;
pub use pagination::WhichPage;
pub use server::HttpServer;
pub use server::{HttpServer, HttpServerStarter};

/*
* Users of the `endpoint` macro need `http::Method` available.
Expand Down
Loading