2021-12-19 16:43:13 +00:00
|
|
|
mod api;
|
2021-07-12 20:14:39 +00:00
|
|
|
mod auth;
|
2021-08-06 02:06:50 +00:00
|
|
|
mod db;
|
2021-07-12 20:14:39 +00:00
|
|
|
mod env;
|
|
|
|
mod rpc;
|
|
|
|
|
2021-10-04 22:06:12 +00:00
|
|
|
use ::rpc::Peer;
|
2022-04-25 00:02:14 +00:00
|
|
|
use axum::{body::Body, http::StatusCode, response::IntoResponse, Router};
|
2022-02-17 16:04:04 +00:00
|
|
|
use db::{Db, PostgresDb};
|
2022-04-24 17:08:25 +00:00
|
|
|
|
2022-04-21 14:57:49 +00:00
|
|
|
use serde::Deserialize;
|
2022-04-24 16:45:20 +00:00
|
|
|
use std::{net::TcpListener, sync::Arc};
|
2021-07-12 20:14:39 +00:00
|
|
|
|
|
|
|
#[derive(Default, Deserialize)]
|
|
|
|
pub struct Config {
|
|
|
|
pub http_port: u16,
|
|
|
|
pub database_url: String,
|
2021-12-19 16:43:13 +00:00
|
|
|
pub api_token: String,
|
2021-07-12 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AppState {
|
2022-02-17 16:04:04 +00:00
|
|
|
db: Arc<dyn Db>,
|
2022-04-26 02:05:09 +00:00
|
|
|
api_token: String,
|
2021-07-12 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppState {
|
2022-04-22 19:33:19 +00:00
|
|
|
async fn new(config: Config) -> Result<Arc<Self>> {
|
2022-02-17 16:04:04 +00:00
|
|
|
let db = PostgresDb::new(&config.database_url, 5).await?;
|
2021-07-12 20:14:39 +00:00
|
|
|
let this = Self {
|
2022-02-17 16:04:04 +00:00
|
|
|
db: Arc::new(db),
|
2022-04-26 02:05:09 +00:00
|
|
|
api_token: config.api_token.clone(),
|
2021-07-12 20:14:39 +00:00
|
|
|
};
|
|
|
|
Ok(Arc::new(this))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 19:33:19 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2022-04-08 16:06:51 +00:00
|
|
|
if std::env::var("LOG_JSON").is_ok() {
|
|
|
|
json_env_logger::init();
|
|
|
|
} else {
|
2022-04-22 19:33:19 +00:00
|
|
|
env_logger::init();
|
2022-04-08 16:06:51 +00:00
|
|
|
}
|
2021-07-12 20:14:39 +00:00
|
|
|
|
|
|
|
if let Err(error) = env::load_dotenv() {
|
|
|
|
log::error!(
|
|
|
|
"error loading .env.toml (this is expected in production): {}",
|
|
|
|
error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let config = envy::from_env::<Config>().expect("error loading config");
|
|
|
|
let state = AppState::new(config).await?;
|
|
|
|
let rpc = Peer::new();
|
|
|
|
run_server(
|
|
|
|
state.clone(),
|
|
|
|
rpc,
|
2022-04-26 02:05:09 +00:00
|
|
|
TcpListener::bind(&format!("0.0.0.0:{}", config.http_port))
|
2022-04-23 00:46:31 +00:00
|
|
|
.expect("failed to bind TCP listener"),
|
2021-07-12 20:14:39 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:45:20 +00:00
|
|
|
pub async fn run_server(
|
|
|
|
state: Arc<AppState>,
|
|
|
|
peer: Arc<Peer>,
|
|
|
|
listener: TcpListener,
|
|
|
|
) -> Result<()> {
|
2022-04-24 17:08:25 +00:00
|
|
|
// TODO: Compression on API routes?
|
|
|
|
// TODO: Authenticate API routes.
|
2021-09-29 17:15:19 +00:00
|
|
|
|
2022-04-26 02:05:09 +00:00
|
|
|
let app = Router::<Body>::new().merge(api::routes(state.clone()));
|
|
|
|
|
2022-04-24 17:08:25 +00:00
|
|
|
// TODO: Add rpc routes
|
2021-07-12 20:14:39 +00:00
|
|
|
|
2022-04-24 17:08:25 +00:00
|
|
|
axum::Server::from_tcp(listener)?
|
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await?;
|
2021-07-12 20:14:39 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-25 00:02:14 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
2022-04-25 00:02:14 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
pub enum Error {
|
|
|
|
Http(StatusCode, String),
|
|
|
|
Internal(anyhow::Error),
|
|
|
|
}
|
2022-04-25 00:02:14 +00:00
|
|
|
|
|
|
|
impl<E> From<E> for Error
|
|
|
|
where
|
|
|
|
E: Into<anyhow::Error>,
|
|
|
|
{
|
|
|
|
fn from(error: E) -> Self {
|
2022-04-25 23:51:13 +00:00
|
|
|
Self::Internal(error.into())
|
2022-04-25 00:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoResponse for Error {
|
|
|
|
fn into_response(self) -> axum::response::Response {
|
2022-04-25 23:51:13 +00:00
|
|
|
match self {
|
|
|
|
Error::Http(code, message) => (code, message).into_response(),
|
|
|
|
Error::Internal(error) => {
|
|
|
|
(StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response()
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 00:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2022-04-25 23:51:13 +00:00
|
|
|
match self {
|
|
|
|
Error::Http(code, message) => (code, message).fmt(f),
|
|
|
|
Error::Internal(error) => error.fmt(f),
|
|
|
|
}
|
2022-04-25 00:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2022-04-25 23:51:13 +00:00
|
|
|
match self {
|
|
|
|
Error::Http(code, message) => write!(f, "{code}: {message}"),
|
|
|
|
Error::Internal(error) => error.fmt(f),
|
|
|
|
}
|
2022-04-25 00:02:14 +00:00
|
|
|
}
|
|
|
|
}
|