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;
|
|
|
|
|
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-21 14:57:49 +00:00
|
|
|
use serde::Deserialize;
|
2022-04-26 17:15:41 +00:00
|
|
|
use std::{
|
|
|
|
net::{SocketAddr, TcpListener},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2022-05-12 15:58:17 +00:00
|
|
|
use tracing_log::LogTracer;
|
2022-05-17 15:11:43 +00:00
|
|
|
use tracing_subscriber::{filter::EnvFilter, fmt::format::JsonFields, Layer};
|
2022-05-12 15:58:17 +00:00
|
|
|
use util::ResultExt;
|
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,
|
2022-04-22 17:55:10 +00:00
|
|
|
pub honeycomb_api_key: Option<String>,
|
|
|
|
pub honeycomb_dataset: Option<String>,
|
2022-05-12 15:58:17 +00:00
|
|
|
pub rust_log: Option<String>,
|
2022-05-17 15:11:43 +00:00
|
|
|
pub log_json: Option<bool>,
|
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-26 02:12:32 +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<()> {
|
2021-07-12 20:14:39 +00:00
|
|
|
if let Err(error) = env::load_dotenv() {
|
2022-05-12 16:05:49 +00:00
|
|
|
eprintln!(
|
2021-07-12 20:14:39 +00:00
|
|
|
"error loading .env.toml (this is expected in production): {}",
|
|
|
|
error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let config = envy::from_env::<Config>().expect("error loading config");
|
2022-04-29 14:45:29 +00:00
|
|
|
init_tracing(&config);
|
2022-04-26 02:12:32 +00:00
|
|
|
let state = AppState::new(&config).await?;
|
2021-09-29 17:15:19 +00:00
|
|
|
|
2022-04-26 02:21:43 +00:00
|
|
|
let listener = TcpListener::bind(&format!("0.0.0.0:{}", config.http_port))
|
|
|
|
.expect("failed to bind TCP listener");
|
2022-04-26 02:05:09 +00:00
|
|
|
|
2022-04-26 02:21:43 +00:00
|
|
|
let app = Router::<Body>::new()
|
2022-04-26 17:15:41 +00:00
|
|
|
.merge(api::routes(state.clone()))
|
|
|
|
.merge(rpc::routes(state));
|
2021-07-12 20:14:39 +00:00
|
|
|
|
2022-04-24 17:08:25 +00:00
|
|
|
axum::Server::from_tcp(listener)?
|
2022-04-26 17:15:41 +00:00
|
|
|
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
|
2022-04-24 17:08:25 +00:00
|
|
|
.await?;
|
2021-07-12 20:14:39 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-25 00:02:14 +00:00
|
|
|
|
2022-04-26 17:15:41 +00:00
|
|
|
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
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
|
|
|
|
2022-05-19 17:09:44 +00:00
|
|
|
impl From<anyhow::Error> for Error {
|
|
|
|
fn from(error: anyhow::Error) -> Self {
|
|
|
|
Self::Internal(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<sqlx::Error> for Error {
|
|
|
|
fn from(error: sqlx::Error) -> 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
|
|
|
}
|
|
|
|
}
|
2022-04-22 17:55:10 +00:00
|
|
|
|
2022-05-19 17:09:44 +00:00
|
|
|
impl std::error::Error for Error {}
|
|
|
|
|
2022-04-29 14:45:29 +00:00
|
|
|
pub fn init_tracing(config: &Config) -> Option<()> {
|
2022-04-27 17:48:43 +00:00
|
|
|
use opentelemetry::KeyValue;
|
2022-04-22 17:55:10 +00:00
|
|
|
use opentelemetry_otlp::WithExportConfig;
|
2022-04-27 21:26:54 +00:00
|
|
|
use std::str::FromStr;
|
2022-04-27 17:48:43 +00:00
|
|
|
use tracing_opentelemetry::OpenTelemetryLayer;
|
|
|
|
use tracing_subscriber::layer::SubscriberExt;
|
2022-05-12 16:16:50 +00:00
|
|
|
let rust_log = config.rust_log.clone()?;
|
2022-05-12 15:58:17 +00:00
|
|
|
|
|
|
|
LogTracer::init().log_err()?;
|
2022-04-27 17:48:43 +00:00
|
|
|
|
2022-05-12 15:58:17 +00:00
|
|
|
let open_telemetry_layer = config
|
2022-04-22 17:55:10 +00:00
|
|
|
.honeycomb_api_key
|
|
|
|
.clone()
|
2022-05-12 15:58:17 +00:00
|
|
|
.zip(config.honeycomb_dataset.clone())
|
|
|
|
.map(|(honeycomb_api_key, honeycomb_dataset)| {
|
|
|
|
let mut metadata = tonic::metadata::MetadataMap::new();
|
|
|
|
metadata.insert("x-honeycomb-team", honeycomb_api_key.parse().unwrap());
|
|
|
|
let tracer = opentelemetry_otlp::new_pipeline()
|
|
|
|
.tracing()
|
|
|
|
.with_exporter(
|
|
|
|
opentelemetry_otlp::new_exporter()
|
|
|
|
.tonic()
|
|
|
|
.with_endpoint("https://api.honeycomb.io")
|
|
|
|
.with_metadata(metadata),
|
|
|
|
)
|
|
|
|
.with_trace_config(opentelemetry::sdk::trace::config().with_resource(
|
|
|
|
opentelemetry::sdk::Resource::new(vec![KeyValue::new(
|
|
|
|
"service.name",
|
|
|
|
honeycomb_dataset,
|
|
|
|
)]),
|
|
|
|
))
|
|
|
|
.install_batch(opentelemetry::runtime::Tokio)
|
|
|
|
.expect("failed to initialize tracing");
|
|
|
|
|
|
|
|
OpenTelemetryLayer::new(tracer)
|
|
|
|
});
|
2022-04-22 17:55:10 +00:00
|
|
|
|
2022-04-27 17:48:43 +00:00
|
|
|
let subscriber = tracing_subscriber::Registry::default()
|
2022-05-12 15:58:17 +00:00
|
|
|
.with(open_telemetry_layer)
|
2022-05-17 15:11:43 +00:00
|
|
|
.with(if config.log_json.unwrap_or(false) {
|
|
|
|
Box::new(
|
|
|
|
tracing_subscriber::fmt::layer()
|
|
|
|
.fmt_fields(JsonFields::default())
|
|
|
|
.event_format(
|
|
|
|
tracing_subscriber::fmt::format()
|
|
|
|
.json()
|
|
|
|
.flatten_event(true)
|
|
|
|
.with_span_list(true),
|
|
|
|
),
|
|
|
|
) as Box<dyn Layer<_> + Send + Sync>
|
|
|
|
} else {
|
|
|
|
Box::new(
|
|
|
|
tracing_subscriber::fmt::layer()
|
|
|
|
.event_format(tracing_subscriber::fmt::format().pretty()),
|
|
|
|
)
|
|
|
|
})
|
2022-05-12 16:16:50 +00:00
|
|
|
.with(EnvFilter::from_str(rust_log.as_str()).log_err()?);
|
2022-04-27 17:48:43 +00:00
|
|
|
|
|
|
|
tracing::subscriber::set_global_default(subscriber).unwrap();
|
|
|
|
|
2022-04-29 14:45:29 +00:00
|
|
|
None
|
2022-04-22 17:55:10 +00:00
|
|
|
}
|