Router, add xrpc and router abstraction to router
This commit is contained in:
parent
57c396afe7
commit
07f4720244
5 changed files with 1514 additions and 37 deletions
53
rust/src/router/xrpc.rs
Normal file
53
rust/src/router/xrpc.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
use axum::{
|
||||
Json,
|
||||
extract::Path,
|
||||
routing::{
|
||||
get,
|
||||
method_routing::MethodRouter,
|
||||
},
|
||||
http::StatusCode,
|
||||
Router as axumRouter,
|
||||
};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
enum Nsid {
|
||||
Nsid(String),
|
||||
NotImplemented,
|
||||
}
|
||||
|
||||
pub struct XrpcEndpoint {
|
||||
nsid: Nsid,
|
||||
resolver: MethodRouter,
|
||||
}
|
||||
|
||||
impl XrpcEndpoint {
|
||||
|
||||
pub fn add_to_router(self, router: axumRouter) -> axumRouter {
|
||||
let path = match self.nsid {
|
||||
Nsid::Nsid(s) => &("/xrpc/".to_owned() + &s),
|
||||
Nsid::NotImplemented => "/xrpc/{*nsid}",
|
||||
};
|
||||
|
||||
router.route(path, self.resolver)
|
||||
}
|
||||
|
||||
pub fn not_implemented() -> XrpcEndpoint {
|
||||
XrpcEndpoint {
|
||||
nsid: Nsid::NotImplemented,
|
||||
resolver: get(Self::not_implemented_resolver)
|
||||
.post(Self::not_implemented_resolver),
|
||||
}
|
||||
}
|
||||
|
||||
async fn not_implemented_resolver(Path(_nsid): Path<String>) -> (StatusCode, Json<Value>) {
|
||||
(
|
||||
StatusCode::NOT_IMPLEMENTED,
|
||||
Json(json!({
|
||||
"error": "MethodNotImplemented",
|
||||
"message": "Method Not Implemented"
|
||||
}))
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue