4ac95ad8cf
* explorer-api: move mix node client operations into a package * explorer-api: add port test for mixnodes with cache for results * explorer-api: add `humantime-serde` dependency * explorer-api: mix node API proxy This fixes mixed-content responses when using the mix node API from the network explorer. An in-memory cache protects the explorer API from over-querying the http API on the mix node. * explorer-api: adjust naming * explorer-api: fix up self refs * explorer-api: add method to state to get a mix node by identity key * explorer-api: add cached http resource to proxy the `/description` and `/stats` http api resources to allow the network explorer do https requests for the mix node api resource avoid mixed content requests * explorer-api: set default mix node cache time to 30 minutes * explorer-api: make clippy happy * explorer-api: add CORS with wide open configuration * explorer-api: fixes from review feedback * explorer-api: move port check test into separate function * explorer-api: use `rocket-cors` that is pinned in the `validator-api` and remove custom CORS handler
51 lines
1.6 KiB
Rust
51 lines
1.6 KiB
Rust
use log::info;
|
|
use rocket::http::Method;
|
|
use rocket::Request;
|
|
use rocket_cors::{AllowedHeaders, AllowedOrigins};
|
|
use rocket_okapi::swagger_ui::make_swagger_ui;
|
|
|
|
use crate::country_statistics::http::country_statistics_make_default_routes;
|
|
use crate::http::swagger::get_docs;
|
|
use crate::mix_node::http::mix_node_make_default_routes;
|
|
use crate::ping::http::ping_make_default_routes;
|
|
use crate::state::ExplorerApiStateContext;
|
|
|
|
mod swagger;
|
|
|
|
pub(crate) fn start(state: ExplorerApiStateContext) {
|
|
tokio::spawn(async move {
|
|
info!("Starting up...");
|
|
|
|
let allowed_origins = AllowedOrigins::all();
|
|
|
|
// You can also deserialize this
|
|
let cors = rocket_cors::CorsOptions {
|
|
allowed_origins,
|
|
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
|
allowed_headers: AllowedHeaders::some(&["*"]),
|
|
allow_credentials: true,
|
|
..Default::default()
|
|
}
|
|
.to_cors()
|
|
.unwrap();
|
|
|
|
let config = rocket::config::Config::release_default();
|
|
rocket::build()
|
|
.configure(config)
|
|
.mount("/countries", country_statistics_make_default_routes())
|
|
.mount("/ping", ping_make_default_routes())
|
|
.mount("/mix-node", mix_node_make_default_routes())
|
|
.mount("/swagger", make_swagger_ui(&get_docs()))
|
|
.register("/", catchers![not_found])
|
|
.manage(state)
|
|
.attach(cors)
|
|
.launch()
|
|
.await
|
|
});
|
|
}
|
|
|
|
#[catch(404)]
|
|
pub(crate) fn not_found(req: &Request) -> String {
|
|
format!("I couldn't find '{}'. Try something else?", req.uri())
|
|
}
|