-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for wildcard routes:
/foo/bar/{rest:.*}
This will match paths such as `/foo/bar` and `/foo/bar/baz.css` We can use this to serve static assets such as css, html, and image files. Express uses this syntax: `/foo/bar/:rest(.*)` Dropwizard / Jersey / JAX-RS does: `/foo/bar/{rest:.*}` Actix does: `/foo/bar/{rest:.*}` This also adds support for a tag (unpublished = true) in the #[endpoint] macro to omit certain endpoints from the OpenAPI output. This is both useful for non-API endpoints and necessary in that OpenAPI doesn't support any type of multi-segment matching of routes.
- Loading branch information
Showing
5 changed files
with
541 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2021 Oxide Computer Company | ||
/*! | ||
* Example use of Dropshot for matching wildcard paths to serve static content. | ||
*/ | ||
|
||
use dropshot::ApiDescription; | ||
use dropshot::ConfigDropshot; | ||
use dropshot::ConfigLogging; | ||
use dropshot::ConfigLoggingLevel; | ||
use dropshot::HttpError; | ||
use dropshot::HttpServerStarter; | ||
use dropshot::RequestContext; | ||
use dropshot::{endpoint, Path}; | ||
use http::{Response, StatusCode}; | ||
use hyper::Body; | ||
use schemars::JsonSchema; | ||
use serde::Deserialize; | ||
use std::sync::Arc; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), String> { | ||
/* | ||
* We must specify a configuration with a bind address. We'll use 127.0.0.1 | ||
* since it's available and won't expose this server outside the host. We | ||
* request port 0, which allows the operating system to pick any available | ||
* port. | ||
*/ | ||
let config_dropshot: ConfigDropshot = Default::default(); | ||
|
||
/* | ||
* For simplicity, we'll configure an "info"-level logger that writes to | ||
* stderr assuming that it's a terminal. | ||
*/ | ||
let config_logging = ConfigLogging::StderrTerminal { | ||
level: ConfigLoggingLevel::Info, | ||
}; | ||
let log = config_logging | ||
.to_logger("example-basic") | ||
.map_err(|error| format!("failed to create logger: {}", error))?; | ||
|
||
/* | ||
* Build a description of the API. | ||
*/ | ||
let mut api = ApiDescription::new(); | ||
api.register(index).unwrap(); | ||
|
||
/* | ||
* Set up the server. | ||
*/ | ||
let server = HttpServerStarter::new(&config_dropshot, api, (), &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.await | ||
} | ||
|
||
#[derive(Deserialize, JsonSchema)] | ||
struct AllPath { | ||
path: String, | ||
} | ||
|
||
/** | ||
* Return static content.for all paths. | ||
*/ | ||
#[endpoint { | ||
method = GET, | ||
/* | ||
* Match literally every path including the empty path. | ||
*/ | ||
path = "/{path:.*}", | ||
/* | ||
* This isn't an API so we don't want this to appear in the OpenAPI | ||
* description if we were to generate it. | ||
*/ | ||
unpublished = true, | ||
}] | ||
async fn index( | ||
_rqctx: Arc<RequestContext<()>>, | ||
path: Path<AllPath>, | ||
) -> Result<Response<Body>, HttpError> { | ||
Ok(Response::builder() | ||
.status(StatusCode::OK) | ||
.header(http::header::CONTENT_TYPE, "text/html") | ||
.body( | ||
format!( | ||
"<HTML><HEAD>nothing at {}</HEAD></HTML>", | ||
path.into_inner().path | ||
) | ||
.into(), | ||
)?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.