web3-proxy/web3_proxy/src/frontend/mod.rs

72 lines
2.4 KiB
Rust
Raw Normal View History

2022-08-06 08:49:52 +03:00
/// this should move into web3_proxy once the basics are working
2022-06-05 22:58:47 +03:00
mod errors;
mod http;
mod http_proxy;
mod rate_limit;
2022-07-14 00:49:57 +03:00
mod users;
2022-06-05 22:58:47 +03:00
mod ws_proxy;
2022-07-07 06:22:09 +03:00
2022-06-05 22:58:47 +03:00
use axum::{
handler::Handler,
routing::{get, post},
Extension, Router,
};
use std::net::SocketAddr;
2022-06-05 22:58:47 +03:00
use std::sync::Arc;
use tracing::info;
2022-06-15 04:02:26 +03:00
2022-06-05 22:58:47 +03:00
use crate::app::Web3ProxyApp;
///
pub async fn serve(port: u16, proxy_app: Arc<Web3ProxyApp>) -> anyhow::Result<()> {
2022-06-05 22:58:47 +03:00
// build our application with a route
2022-07-26 08:01:08 +03:00
// order most to least common
2022-06-05 22:58:47 +03:00
let app = Router::new()
2022-08-05 22:22:23 +03:00
.route("/", post(http_proxy::public_proxy_web3_rpc))
.route("/", get(ws_proxy::public_websocket_handler))
2022-08-06 04:17:25 +03:00
.route("/u/:user_key", post(http_proxy::user_proxy_web3_rpc))
.route("/u/:user_key", get(ws_proxy::user_websocket_handler))
2022-06-29 21:22:53 +03:00
.route("/health", get(http::health))
2022-06-05 22:58:47 +03:00
.route("/status", get(http::status))
2022-07-14 00:49:57 +03:00
.route("/users", post(users::create_user))
2022-08-07 22:35:24 +03:00
.layer(Extension(proxy_app))
// 404 for any unknown routes
.fallback(errors::handler_404.into_service());
2022-06-05 22:58:47 +03:00
// run our app with hyper
2022-08-05 22:22:23 +03:00
// TODO: allow only listening on localhost?
2022-06-05 22:58:47 +03:00
let addr = SocketAddr::from(([0, 0, 0, 0], port));
2022-08-06 04:17:25 +03:00
info!("listening on port {}", port);
2022-07-07 06:22:09 +03:00
// TODO: into_make_service is enough if we always run behind a proxy. make into_make_service_with_connect_info optional?
2022-08-11 05:57:01 +03:00
/*
It sequentially looks for an IP in:
- x-forwarded-for header (de-facto standard)
- x-real-ip header
- forwarded header (new standard)
- axum::extract::ConnectInfo (if not behind proxy)
So we probably won't need into_make_service_with_connect_info, but it shouldn't hurt
*/
let service = app.into_make_service_with_connect_info::<SocketAddr>();
// let service = app.into_make_service();
// `axum::Server` is a re-export of `hyper::Server`
2022-06-05 22:58:47 +03:00
axum::Server::bind(&addr)
2022-08-07 09:48:57 +03:00
// TODO: option to use with_connect_info. we want it in dev, but not when running behind a proxy, but not
2022-08-11 05:57:01 +03:00
.serve(service)
.with_graceful_shutdown(signal_shutdown())
2022-06-05 22:58:47 +03:00
.await
.map_err(Into::into)
}
/// Tokio signal handler that will wait for a user to press CTRL+C.
/// We use this in our hyper `Server` method `with_graceful_shutdown`.
async fn signal_shutdown() {
info!("ctrl-c to quit");
tokio::signal::ctrl_c()
.await
.expect("expect tokio signal ctrl-c");
info!("signal shutdown");
}