2024-05-22 18:34:47 -07:00
|
|
|
use anyhow::Context;
|
2024-05-09 11:01:53 -07:00
|
|
|
use axum::routing::get;
|
|
|
|
|
use axum::routing::Router;
|
2024-05-25 19:16:36 -07:00
|
|
|
use neptune_explorer::alert_email;
|
2024-05-09 11:01:53 -07:00
|
|
|
use neptune_explorer::html::page::block::block_page;
|
2024-05-14 11:54:42 -07:00
|
|
|
use neptune_explorer::html::page::not_found::not_found_html_fallback;
|
2024-05-16 10:16:48 -07:00
|
|
|
use neptune_explorer::html::page::redirect_qs_to_path::redirect_query_string_to_path;
|
2024-05-09 11:01:53 -07:00
|
|
|
use neptune_explorer::html::page::root::root;
|
|
|
|
|
use neptune_explorer::html::page::utxo::utxo_page;
|
|
|
|
|
use neptune_explorer::model::app_state::AppState;
|
2024-05-25 19:16:36 -07:00
|
|
|
use neptune_explorer::neptune_rpc;
|
2024-05-09 11:01:53 -07:00
|
|
|
use neptune_explorer::rpc::block_digest::block_digest;
|
|
|
|
|
use neptune_explorer::rpc::block_info::block_info;
|
|
|
|
|
use neptune_explorer::rpc::utxo_digest::utxo_digest;
|
2024-05-14 11:54:42 -07:00
|
|
|
use tower_http::services::ServeDir;
|
2024-05-28 15:39:46 -07:00
|
|
|
use tracing::info;
|
2024-05-25 19:16:36 -07:00
|
|
|
use tracing_subscriber::EnvFilter;
|
2024-04-24 18:44:52 -07:00
|
|
|
|
|
|
|
|
#[tokio::main]
|
2024-05-22 18:34:47 -07:00
|
|
|
async fn main() -> Result<(), anyhow::Error> {
|
2024-05-25 19:16:36 -07:00
|
|
|
let filter = EnvFilter::from_default_env()
|
|
|
|
|
// Set the base level when not matched by other directives to INFO.
|
|
|
|
|
.add_directive("neptune_explorer=info".parse()?);
|
|
|
|
|
|
|
|
|
|
tracing_subscriber::fmt().with_env_filter(filter).init();
|
|
|
|
|
|
|
|
|
|
let app_state = AppState::init().await?;
|
|
|
|
|
|
|
|
|
|
let routes = setup_routes(app_state.clone());
|
|
|
|
|
|
2024-05-27 17:24:01 -07:00
|
|
|
let port = app_state.load().config.listen_port;
|
2024-05-25 19:16:36 -07:00
|
|
|
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{port}"))
|
2024-05-22 18:34:47 -07:00
|
|
|
.await
|
2024-05-25 19:16:36 -07:00
|
|
|
.with_context(|| format!("Failed to bind to port {port}"))?;
|
|
|
|
|
|
2024-05-28 15:39:46 -07:00
|
|
|
// this will log warnings if smtp not configured or mis-configured.
|
|
|
|
|
alert_email::check_alert_params();
|
2024-04-24 18:44:52 -07:00
|
|
|
|
2024-12-31 09:58:40 -08:00
|
|
|
tokio::task::spawn(neptune_rpc::watchdog(app_state.clone()));
|
|
|
|
|
tokio::task::spawn(neptune_rpc::blockchain_watchdog(app_state));
|
2024-04-24 18:44:52 -07:00
|
|
|
|
2024-05-25 19:16:36 -07:00
|
|
|
info!("Running on http://localhost:{port}");
|
|
|
|
|
|
|
|
|
|
axum::serve(listener, routes)
|
|
|
|
|
.await
|
|
|
|
|
.with_context(|| "Axum server encountered an error")?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn setup_routes(app_state: AppState) -> Router {
|
|
|
|
|
Router::new()
|
2024-05-09 08:56:59 -07:00
|
|
|
// -- RPC calls --
|
2024-05-21 12:41:34 -07:00
|
|
|
.route("/rpc/block_info/*selector", get(block_info))
|
|
|
|
|
.route("/rpc/block_digest/*selector", get(block_digest))
|
2024-04-24 18:44:52 -07:00
|
|
|
.route("/rpc/utxo_digest/:index", get(utxo_digest))
|
2024-05-09 08:56:59 -07:00
|
|
|
// -- Dynamic HTML pages --
|
2024-04-24 18:44:52 -07:00
|
|
|
.route("/", get(root))
|
2024-05-21 12:41:34 -07:00
|
|
|
.route("/block/*selector", get(block_page))
|
2024-05-09 08:56:59 -07:00
|
|
|
.route("/utxo/:value", get(utxo_page))
|
2024-05-16 10:16:48 -07:00
|
|
|
// -- Rewrite query-strings to path --
|
|
|
|
|
.route("/rqs", get(redirect_query_string_to_path))
|
2024-05-09 08:56:59 -07:00
|
|
|
// -- Static files --
|
2024-05-14 11:54:42 -07:00
|
|
|
.nest_service(
|
|
|
|
|
"/css",
|
|
|
|
|
ServeDir::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/web/css")),
|
2024-05-10 14:20:43 -07:00
|
|
|
)
|
2024-05-14 11:54:42 -07:00
|
|
|
.nest_service(
|
|
|
|
|
"/image",
|
|
|
|
|
ServeDir::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/web/image")),
|
2024-05-09 11:01:53 -07:00
|
|
|
)
|
2024-05-14 11:54:42 -07:00
|
|
|
// handle route not-found
|
2024-05-25 19:16:36 -07:00
|
|
|
.fallback(not_found_html_fallback)
|
2024-05-09 08:56:59 -07:00
|
|
|
// add state
|
2024-05-25 19:16:36 -07:00
|
|
|
.with_state(app_state.into())
|
2024-04-24 18:44:52 -07:00
|
|
|
}
|