-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from github/jorendorff/axum-encapsulate
Tidy up after hyper 1.x update
- Loading branch information
Showing
6 changed files
with
107 additions
and
60 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
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
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 |
---|---|---|
@@ -1,10 +1,58 @@ | ||
//! Undocumented features that are public for use in generated code (see `twirp-build`). | ||
#[doc(hidden)] | ||
pub use axum::extract::{Request, State}; | ||
use std::future::Future; | ||
|
||
#[doc(hidden)] | ||
pub use axum::routing::post; | ||
use axum::extract::{Request, State}; | ||
use axum::Router; | ||
|
||
#[doc(hidden)] | ||
pub use axum::response::Response; | ||
use crate::{server, TwirpErrorResponse}; | ||
|
||
/// Builder object used by generated code to build a Twirp service. | ||
/// | ||
/// The type `S` is something like `Arc<MyExampleAPIServer>`, which can be cheaply cloned for each | ||
/// incoming request, providing access to the Rust value that actually implements the RPCs. | ||
pub struct TwirpRouterBuilder<S> { | ||
service: S, | ||
router: Router<S>, | ||
} | ||
|
||
impl<S> TwirpRouterBuilder<S> | ||
where | ||
S: Clone + Send + Sync + 'static, | ||
{ | ||
pub fn new(service: S) -> Self { | ||
TwirpRouterBuilder { | ||
service, | ||
router: Router::new(), | ||
} | ||
} | ||
|
||
/// Add a handler for an `rpc` to the router. | ||
/// | ||
/// The generated code passes a closure that calls the method, like | ||
/// `|api: Arc<HaberdasherAPIServer>, req: MakeHatRequest| async move { api.make_hat(req) }`. | ||
pub fn route<F, Fut, Req, Res>(self, url: &str, f: F) -> Self | ||
where | ||
F: Fn(S, Req) -> Fut + Clone + Sync + Send + 'static, | ||
Fut: Future<Output = Result<Res, TwirpErrorResponse>> + Send, | ||
Req: prost::Message + Default + serde::de::DeserializeOwned, | ||
Res: prost::Message + serde::Serialize, | ||
{ | ||
TwirpRouterBuilder { | ||
service: self.service, | ||
router: self.router.route( | ||
url, | ||
axum::routing::post(move |State(api): State<S>, req: Request| async move { | ||
server::handle_request(api, req, f).await | ||
}), | ||
), | ||
} | ||
} | ||
|
||
/// Finish building the axum router. | ||
pub fn build(self) -> axum::Router { | ||
self.router | ||
.fallback(crate::server::not_found_handler) | ||
.with_state(self.service) | ||
} | ||
} |
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
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
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