mod api; mod auth; mod db; mod env; mod rpc; use ::rpc::Peer; use axum::{body::Body, http::StatusCode, response::IntoResponse, Router}; use db::{Db, PostgresDb}; use serde::Deserialize; use std::{net::TcpListener, sync::Arc}; // type Request = tide::Request>; #[derive(Default, Deserialize)] pub struct Config { pub http_port: u16, pub database_url: String, pub api_token: String, } pub struct AppState { db: Arc, config: Config, } impl AppState { async fn new(config: Config) -> Result> { let db = PostgresDb::new(&config.database_url, 5).await?; let this = Self { db: Arc::new(db), config, }; Ok(Arc::new(this)) } } // trait RequestExt { // fn db(&self) -> &Arc; // } // impl RequestExt for Request { // fn db(&self) -> &Arc { // &self.data::>().unwrap().db // } // } #[tokio::main] async fn main() -> Result<()> { if std::env::var("LOG_JSON").is_ok() { json_env_logger::init(); } else { env_logger::init(); } if let Err(error) = env::load_dotenv() { log::error!( "error loading .env.toml (this is expected in production): {}", error ); } let config = envy::from_env::().expect("error loading config"); let state = AppState::new(config).await?; let rpc = Peer::new(); run_server( state.clone(), rpc, TcpListener::bind(&format!("0.0.0.0:{}", state.config.http_port)) .expect("failed to bind TCP listener"), ) .await?; Ok(()) } pub async fn run_server( state: Arc, peer: Arc, listener: TcpListener, ) -> Result<()> { let app = Router::::new(); // TODO: Compression on API routes? // TODO: Authenticate API routes. let app = api::add_routes(app, state); // TODO: Add rpc routes axum::Server::from_tcp(listener)? .serve(app.into_make_service()) .await?; Ok(()) } type Result = std::result::Result; struct Error(anyhow::Error); impl From for Error where E: Into, { fn from(error: E) -> Self { Self(error.into()) } } impl IntoResponse for Error { fn into_response(self) -> axum::response::Response { (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &self.0)).into_response() } } impl std::fmt::Debug for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } } impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } }