This commit is contained in:
Nathan Sobo 2022-04-25 20:21:43 -06:00
parent 3938f7c364
commit be040b60b7
3 changed files with 13 additions and 20 deletions

View file

@ -28,6 +28,7 @@ pub fn routes(state: Arc<AppState>) -> Router<Body> {
.layer(Extension(state))
.layer(middleware::from_fn(validate_api_token)),
)
// TODO: Compression on API routes?
}
pub async fn validate_api_token<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {

View file

@ -51,28 +51,13 @@ async fn main() -> Result<()> {
let config = envy::from_env::<Config>().expect("error loading config");
let state = AppState::new(&config).await?;
let rpc = Peer::new();
run_server(
state.clone(),
rpc,
TcpListener::bind(&format!("0.0.0.0:{}", config.http_port))
.expect("failed to bind TCP listener"),
)
.await?;
Ok(())
}
pub async fn run_server(
state: Arc<AppState>,
peer: Arc<Peer>,
listener: TcpListener,
) -> Result<()> {
// TODO: Compression on API routes?
// TODO: Authenticate API routes.
let listener = TcpListener::bind(&format!("0.0.0.0:{}", config.http_port))
.expect("failed to bind TCP listener");
let app = Router::<Body>::new().merge(api::routes(state.clone()));
// TODO: Add rpc routes
let app = Router::<Body>::new()
.merge(api::routes(state))
.merge(rpc::routes(Peer::new()));
axum::Server::from_tcp(listener)?
.serve(app.into_make_service())

View file

@ -1099,6 +1099,10 @@
// }
// }
pub fn routes(peer: Arc<Peer>) -> Router<Body> {
Router::new()
}
// pub fn add_routes(app: &mut tide::Server<Arc<AppState>>, rpc: &Arc<Peer>) {
// let server = Server::new(app.state().clone(), rpc.clone(), None);
// app.at("/rpc").get(move |request: Request<Arc<AppState>>| {
@ -6360,3 +6364,6 @@
// }
// }
// }
use axum::{body::Body, Router};
use client::Peer;