54 lines
1 KiB
Rust
54 lines
1 KiB
Rust
|
|
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"
|
||
|
|
}))
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|