2023-08-02 01:10:53 +00:00
|
|
|
mod api_v1;
|
2024-03-31 03:10:48 +00:00
|
|
|
pub mod host_meta;
|
2023-09-23 11:53:55 +00:00
|
|
|
pub mod model;
|
2023-04-21 23:39:52 +00:00
|
|
|
pub mod nodeinfo;
|
2024-11-12 21:37:18 +00:00
|
|
|
mod rpc_v1;
|
2023-08-02 01:10:53 +00:00
|
|
|
pub mod service;
|
|
|
|
pub mod util;
|
|
|
|
pub mod web;
|
2023-02-18 14:28:09 +00:00
|
|
|
pub mod webfinger;
|
|
|
|
|
2023-08-02 01:10:53 +00:00
|
|
|
use crate::api_v1::create_api_router;
|
2024-03-31 03:10:48 +00:00
|
|
|
use crate::host_meta::handle_host_meta;
|
2023-04-21 23:39:52 +00:00
|
|
|
use crate::nodeinfo::{handle_nodeinfo, handle_nodeinfo_20, handle_nodeinfo_21};
|
2023-08-02 01:10:53 +00:00
|
|
|
use crate::service::MagnetarService;
|
2023-02-18 14:28:09 +00:00
|
|
|
use axum::routing::get;
|
|
|
|
use axum::Router;
|
|
|
|
use dotenvy::dotenv;
|
2024-11-12 21:37:18 +00:00
|
|
|
use futures::{select, FutureExt};
|
|
|
|
use magnetar_common::config::{MagnetarConfig, MagnetarRpcSocketKind};
|
2024-04-15 02:45:44 +00:00
|
|
|
use magnetar_model::{CacheConnectorConfig, CalckeyCache, CalckeyModel, ConnectorConfig};
|
2023-07-07 19:22:30 +00:00
|
|
|
use miette::{miette, IntoDiagnostic};
|
2024-11-12 21:37:18 +00:00
|
|
|
use rpc_v1::proto::{MagRpc, RpcMessage, RpcSockAddr};
|
|
|
|
use std::convert::Infallible;
|
|
|
|
use std::future::Future;
|
2023-02-18 14:28:09 +00:00
|
|
|
use std::net::SocketAddr;
|
2023-08-02 01:10:53 +00:00
|
|
|
use std::sync::Arc;
|
2023-12-22 18:19:07 +00:00
|
|
|
use tokio::net::TcpListener;
|
2023-08-02 01:18:16 +00:00
|
|
|
use tokio::signal;
|
2023-02-18 14:28:09 +00:00
|
|
|
use tower_http::cors::{Any, CorsLayer};
|
|
|
|
use tower_http::trace::TraceLayer;
|
2023-08-02 01:18:16 +00:00
|
|
|
use tracing::log::error;
|
2024-11-12 21:37:18 +00:00
|
|
|
use tracing::{debug, info};
|
2023-02-18 14:28:09 +00:00
|
|
|
use tracing_subscriber::EnvFilter;
|
|
|
|
|
|
|
|
#[tokio::main]
|
2023-07-07 19:22:30 +00:00
|
|
|
async fn main() -> miette::Result<()> {
|
2023-02-18 14:28:09 +00:00
|
|
|
dotenv().ok();
|
|
|
|
|
|
|
|
let filter_layer = EnvFilter::try_from_default_env()
|
|
|
|
.or_else(|_| EnvFilter::try_new("info"))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_env_filter(filter_layer)
|
|
|
|
.with_test_writer()
|
|
|
|
.init();
|
|
|
|
|
2023-07-07 19:22:30 +00:00
|
|
|
let config = &*Box::leak::<'static>(Box::new(
|
|
|
|
magnetar_common::config::load_config().into_diagnostic()?,
|
|
|
|
));
|
|
|
|
info!("Loaded configuration: {config:#?}");
|
2023-02-18 14:28:09 +00:00
|
|
|
|
2023-04-21 23:39:52 +00:00
|
|
|
let db = CalckeyModel::new(ConnectorConfig {
|
|
|
|
url: config.data.database_url.clone(),
|
|
|
|
})
|
2024-11-12 21:37:18 +00:00
|
|
|
.await
|
|
|
|
.into_diagnostic()?;
|
2023-07-29 03:20:00 +00:00
|
|
|
|
|
|
|
db.migrate().await.into_diagnostic()?;
|
2023-04-21 23:39:52 +00:00
|
|
|
|
2023-08-02 01:10:53 +00:00
|
|
|
let redis = CalckeyCache::new(CacheConnectorConfig {
|
|
|
|
url: config.data.redis_url.clone(),
|
|
|
|
})
|
2024-11-12 21:37:18 +00:00
|
|
|
.into_diagnostic()?;
|
2023-08-02 01:10:53 +00:00
|
|
|
|
|
|
|
let service = Arc::new(
|
|
|
|
MagnetarService::new(config, db.clone(), redis)
|
|
|
|
.await
|
|
|
|
.into_diagnostic()?,
|
|
|
|
);
|
|
|
|
|
2024-11-12 21:37:18 +00:00
|
|
|
let shutdown_signal = shutdown_signal().shared();
|
|
|
|
|
|
|
|
select! {
|
|
|
|
rpc_res = run_rpc(service.clone(), config, shutdown_signal.clone()).fuse() => rpc_res,
|
|
|
|
web_res = run_web(service, config, shutdown_signal).fuse() => web_res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_rpc(
|
|
|
|
service: Arc<MagnetarService>,
|
|
|
|
config: &'static MagnetarConfig,
|
|
|
|
shutdown_signal: impl Future<Output=()> + Send + 'static,
|
|
|
|
) -> miette::Result<()> {
|
|
|
|
let rpc_bind_addr = match &config.rpc.connection_settings {
|
|
|
|
MagnetarRpcSocketKind::None => {
|
|
|
|
std::future::pending::<Infallible>().await;
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
MagnetarRpcSocketKind::Unix(path) => RpcSockAddr::Unix(path.clone()),
|
|
|
|
MagnetarRpcSocketKind::Tcp(ip) => RpcSockAddr::Ip(*ip),
|
|
|
|
};
|
|
|
|
|
|
|
|
let rpc = MagRpc::new().handle(
|
|
|
|
"/ping",
|
|
|
|
|_, RpcMessage(message): RpcMessage<String>| async move {
|
|
|
|
debug!("Received RPC ping: {}", message);
|
|
|
|
RpcMessage("pong".to_owned())
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
rpc.run(service, rpc_bind_addr, Some(shutdown_signal)).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_web(
|
|
|
|
service: Arc<MagnetarService>,
|
|
|
|
config: &'static MagnetarConfig,
|
|
|
|
shutdown_signal: impl Future<Output=()> + Send + 'static,
|
|
|
|
) -> miette::Result<()> {
|
2023-04-21 23:39:52 +00:00
|
|
|
let well_known_router = Router::new()
|
|
|
|
.route(
|
|
|
|
"/webfinger",
|
2024-11-12 21:37:18 +00:00
|
|
|
get(webfinger::handle_webfinger).with_state((config, service.db.clone())),
|
2023-04-21 23:39:52 +00:00
|
|
|
)
|
2024-03-31 03:10:48 +00:00
|
|
|
.route("/host-meta", get(handle_host_meta))
|
2023-04-21 23:39:52 +00:00
|
|
|
.route("/nodeinfo", get(handle_nodeinfo));
|
|
|
|
|
|
|
|
let nodeinfo_router = Router::new()
|
|
|
|
.with_state(config)
|
|
|
|
.route("/2.0", get(handle_nodeinfo_20))
|
|
|
|
.route("/2.1", get(handle_nodeinfo_21));
|
2023-02-18 14:28:09 +00:00
|
|
|
|
|
|
|
let app = Router::new()
|
2023-08-02 01:10:53 +00:00
|
|
|
.nest("/.well-known", well_known_router.with_state(config))
|
|
|
|
.nest("/nodeinfo", nodeinfo_router.with_state(config))
|
|
|
|
.nest("/mag/v1", create_api_router(service))
|
2023-02-18 14:28:09 +00:00
|
|
|
.layer(
|
|
|
|
CorsLayer::new()
|
|
|
|
.allow_headers(Any)
|
|
|
|
.allow_methods(Any)
|
|
|
|
.allow_origin(Any),
|
|
|
|
)
|
|
|
|
.layer(TraceLayer::new_for_http());
|
|
|
|
|
2023-02-26 02:31:39 +00:00
|
|
|
let addr = SocketAddr::from((config.networking.bind_addr, config.networking.port));
|
2023-12-22 18:19:07 +00:00
|
|
|
info!("Binding to: {addr}");
|
|
|
|
let listener = TcpListener::bind(addr).await.into_diagnostic()?;
|
|
|
|
info!("Serving...");
|
|
|
|
axum::serve(listener, app.into_make_service())
|
2024-11-12 21:37:18 +00:00
|
|
|
.with_graceful_shutdown(shutdown_signal)
|
2023-02-18 14:28:09 +00:00
|
|
|
.await
|
2023-07-07 19:22:30 +00:00
|
|
|
.map_err(|e| miette!("Error running server: {}", e))
|
2023-02-14 00:59:15 +00:00
|
|
|
}
|
2023-08-02 01:18:16 +00:00
|
|
|
|
|
|
|
async fn shutdown_signal() {
|
|
|
|
let ctrl_c = async {
|
|
|
|
if let Err(e) = signal::ctrl_c().await {
|
|
|
|
error!("Ctrl+C signal handler error: {}", e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
let terminate = async {
|
|
|
|
signal::unix::signal(signal::unix::SignalKind::terminate())
|
|
|
|
.expect("SIGTERM handler error")
|
|
|
|
.recv()
|
|
|
|
.await;
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
let terminate = std::future::pending::<()>();
|
|
|
|
|
|
|
|
tokio::select! {
|
|
|
|
_ = ctrl_c => {},
|
|
|
|
_ = terminate => {},
|
|
|
|
}
|
|
|
|
|
2024-11-12 21:37:18 +00:00
|
|
|
info!("Received a signal to shut down...");
|
2023-08-02 01:18:16 +00:00
|
|
|
}
|