[Add] Dynamic routes through path parameters
All checks were successful
CI / checks (push) Successful in 5m22s
All checks were successful
CI / checks (push) Successful in 5m22s
This commit is contained in:
parent
3919eb46fd
commit
cf3cccd3ca
6 changed files with 379 additions and 13 deletions
|
|
@ -141,7 +141,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
.await?;
|
||||
let mut host = mtp::webserver::MTPWebServer::new(config, web_server::config()?).await?;
|
||||
println!(
|
||||
"Server listening on https://{} (TCP HTTPS + UDP WebTransport)",
|
||||
"Server listening on https://{} (HTTPS + UDP WebTransport)",
|
||||
host.local_addr()
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use mtp::webserver::{Http3Request, Http3Response, WebServerConfig};
|
||||
use mtp::webserver::{Http3Request, Http3Response, RouteParams, WebServerConfig};
|
||||
use rustls::pki_types::{PrivateKeyDer, pem::PemObject};
|
||||
use std::{
|
||||
io,
|
||||
|
|
@ -19,8 +19,32 @@ async fn ok(_request: Http3Request, response: Http3Response) -> Http3Response {
|
|||
.body("OK")
|
||||
}
|
||||
|
||||
async fn profile(
|
||||
_request: Http3Request,
|
||||
response: Http3Response,
|
||||
params: RouteParams,
|
||||
) -> Http3Response {
|
||||
let Some(user) = params.get("user") else {
|
||||
return response.body("missing user");
|
||||
};
|
||||
|
||||
let body = serde_json::json!({
|
||||
"user": user,
|
||||
"profile": {
|
||||
"display_name": format!("Example user {user}"),
|
||||
"status": "active"
|
||||
}
|
||||
});
|
||||
|
||||
response
|
||||
.header("content-type", "application/json; charset=utf-8")
|
||||
.body(body.to_string())
|
||||
}
|
||||
|
||||
pub fn config() -> Result<WebServerConfig, mtp::webserver::RouterError> {
|
||||
WebServerConfig::new().route("/", ok)
|
||||
WebServerConfig::new()
|
||||
.route("/", ok)?
|
||||
.route_pattern("/api/get/{user}/profile", profile)
|
||||
}
|
||||
|
||||
/// Starts the conventional HTTPS side of the example host. WebTransport uses
|
||||
|
|
|
|||
Loading…
Reference in a new issue