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
|
|
@ -1,27 +1,31 @@
|
|||
use axum::{
|
||||
Json,
|
||||
routing::get,
|
||||
extract::Path,
|
||||
http::StatusCode,
|
||||
Router,
|
||||
};
|
||||
use serde_json::{Value, json};
|
||||
use crate::router::xrpc::XrpcEndpoint;
|
||||
use axum::Router as axumRouter;
|
||||
use core::net::SocketAddr;
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
pub async fn setup() {
|
||||
let app = Router::new()
|
||||
.route("/xrpc/{*nsid}", get(not_implemented).post(not_implemented));
|
||||
|
||||
let listener = TcpListener::bind("0.0.0.0:6702").await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
pub struct Router {
|
||||
addr: SocketAddr,
|
||||
xrpc: Vec<XrpcEndpoint>,
|
||||
}
|
||||
|
||||
async fn not_implemented(Path(_nsid): Path<String>) -> (StatusCode, Json<Value>) {
|
||||
(
|
||||
StatusCode::NOT_IMPLEMENTED,
|
||||
Json(json!({
|
||||
"error": "MethodNotImplemented",
|
||||
"message": "Method Not Implemented"
|
||||
}))
|
||||
)
|
||||
mod xrpc;
|
||||
|
||||
impl Router {
|
||||
pub fn new() -> Self {
|
||||
let xrpc = vec![XrpcEndpoint::not_implemented()];
|
||||
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127,0,0,1)), 6702);
|
||||
Router { xrpc, addr }
|
||||
}
|
||||
|
||||
pub async fn serve(self) {
|
||||
let listener = TcpListener::bind(self.addr).await.unwrap();
|
||||
let mut router = axumRouter::new();
|
||||
|
||||
for endpoint in self.xrpc {
|
||||
router = endpoint.add_to_router(router);
|
||||
}
|
||||
|
||||
axum::serve(listener, router).await.unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue