openbook-candles/src/server/main.rs

66 lines
1.7 KiB
Rust
Raw Normal View History

use actix_web::{
middleware::Logger,
web::{self, Data},
App, HttpServer,
};
2023-03-13 09:51:30 -07:00
use candles::get_candles;
use dotenv;
2023-03-13 22:45:29 -07:00
use markets::get_markets;
2023-03-13 09:51:30 -07:00
use openbook_candles::{
database::initialize::connect_to_database,
2023-03-13 23:21:15 -07:00
structs::markets::{fetch_market_infos, load_markets},
2023-03-13 09:51:30 -07:00
utils::{Config, WebContext},
};
2023-03-14 18:46:49 -07:00
use std::env;
use traders::{get_top_traders_by_base_volume, get_top_traders_by_quote_volume};
2023-03-12 22:03:37 -07:00
mod candles;
mod coingecko;
2023-03-13 22:45:29 -07:00
mod markets;
2023-03-12 22:03:37 -07:00
mod server_error;
2023-03-13 09:51:30 -07:00
mod traders;
2023-03-05 23:11:15 -08:00
#[actix_web::main]
2023-03-12 22:03:37 -07:00
async fn main() -> std::io::Result<()> {
dotenv::dotenv().ok();
env_logger::init();
2023-03-14 18:46:49 -07:00
let args: Vec<String> = env::args().collect();
assert!(args.len() == 2);
let path_to_markets_json = &args[1];
2023-03-12 22:03:37 -07:00
let rpc_url: String = dotenv::var("RPC_URL").unwrap();
let bind_addr: String = dotenv::var("SERVER_BIND_ADDR").expect("reading bind addr from env");
2023-03-12 22:03:37 -07:00
let config = Config {
rpc_url: rpc_url.clone(),
};
2023-03-05 23:11:15 -08:00
2023-03-14 18:46:49 -07:00
let markets = load_markets(path_to_markets_json);
2023-03-12 22:03:37 -07:00
let market_infos = fetch_market_infos(&config, markets).await.unwrap();
let pool = connect_to_database().await.unwrap();
2023-03-12 22:03:37 -07:00
let context = Data::new(WebContext {
rpc_url,
2023-03-12 22:03:37 -07:00
pool,
markets: market_infos,
});
2023-03-05 23:11:15 -08:00
2023-03-13 09:51:30 -07:00
println!("Starting server");
HttpServer::new(move || {
2023-03-05 23:11:15 -08:00
App::new()
.wrap(Logger::default())
2023-03-13 09:51:30 -07:00
.app_data(context.clone())
.service(
web::scope("/api")
.service(get_candles)
.service(get_top_traders_by_base_volume)
2023-03-13 22:45:29 -07:00
.service(get_top_traders_by_quote_volume)
.service(get_markets)
.service(coingecko::service()),
2023-03-13 09:51:30 -07:00
)
2023-03-05 23:11:15 -08:00
})
.bind(&bind_addr)?
2023-03-05 23:11:15 -08:00
.run()
.await
2023-03-05 22:52:42 -08:00
}