feat: add global component tag to rust json logs, add sentry, some cleanup

This commit is contained in:
alyssa 2024-11-21 10:45:03 +09:00
parent 0600ae00ff
commit 701bafdf97
11 changed files with 321 additions and 49 deletions

View file

@ -54,29 +54,9 @@ async fn rproxy(
// this function is manually formatted for easier legibility of route_services
#[rustfmt::skip]
#[tokio::main]
async fn main() -> anyhow::Result<()> {
libpk::init_logging("api")?;
libpk::init_metrics()?;
info!("hello world");
let db = libpk::db::init_data_db().await?;
let redis = libpk::db::init_redis().await?;
let rproxy_uri = Uri::from_static(&libpk::config.api.as_ref().expect("missing api config").remote_url).to_string();
let rproxy_client = hyper_util::client::legacy::Client::<(), ()>::builder(TokioExecutor::new())
.build(HttpConnector::new());
let ctx = ApiContext {
db,
redis,
rproxy_uri: rproxy_uri[..rproxy_uri.len() - 1].to_string(),
rproxy_client,
};
fn router(ctx: ApiContext) -> Router {
// processed upside down (???) so we have to put middleware at the end
let app = Router::new()
Router::new()
.route("/v2/systems/:system_id", get(rproxy))
.route("/v2/systems/:system_id", patch(rproxy))
.route("/v2/systems/:system_id/settings", get(rproxy))
@ -143,9 +123,42 @@ async fn main() -> anyhow::Result<()> {
.with_state(ctx)
.route("/", get(|| async { axum::response::Redirect::to("https://pluralkit.me/api") }));
.route("/", get(|| async { axum::response::Redirect::to("https://pluralkit.me/api") }))
}
libpk::main!("api");
async fn real_main() -> anyhow::Result<()> {
let db = libpk::db::init_data_db().await?;
let redis = libpk::db::init_redis().await?;
let rproxy_uri = Uri::from_static(
&libpk::config
.api
.as_ref()
.expect("missing api config")
.remote_url,
)
.to_string();
let rproxy_client = hyper_util::client::legacy::Client::<(), ()>::builder(TokioExecutor::new())
.build(HttpConnector::new());
let ctx = ApiContext {
db,
redis,
rproxy_uri: rproxy_uri[..rproxy_uri.len() - 1].to_string(),
rproxy_client,
};
let app = router(ctx);
let addr: &str = libpk::config
.api
.as_ref()
.expect("missing api config")
.addr
.as_ref();
let addr: &str = libpk::config.api.as_ref().expect("missing api config").addr.as_ref();
let listener = tokio::net::TcpListener::bind(addr).await?;
info!("listening on {}", addr);
axum::serve(listener, app).await?;

View file

@ -4,12 +4,8 @@ use sqlx::prelude::FromRow;
use std::{sync::Arc, time::Duration};
use tracing::{error, info};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
libpk::init_logging("avatar_cleanup")?;
libpk::init_metrics()?;
info!("hello world");
libpk::main!("avatar_cleanup");
async fn real_main() -> anyhow::Result<()> {
let config = libpk::config
.avatars
.as_ref()
@ -129,7 +125,7 @@ async fn cleanup_job(pool: sqlx::PgPool, bucket: Arc<s3::Bucket>) -> anyhow::Res
}
_ => {
let status = cf_resp.status();
println!("{:#?}", cf_resp.text().await?);
tracing::info!("raw response from cloudflare: {:#?}", cf_resp.text().await?);
anyhow::bail!("cloudflare returned bad error code {}", status);
}
}

View file

@ -142,12 +142,8 @@ pub struct AppState {
config: Arc<AvatarsConfig>,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
libpk::init_logging("avatars")?;
libpk::init_metrics()?;
info!("hello world");
libpk::main!("avatars");
async fn real_main() -> anyhow::Result<()> {
let config = libpk::config
.avatars
.as_ref()

View file

@ -19,6 +19,8 @@ use axum::{extract::State, http::Uri, routing::post, Json, Router};
mod logger;
// this package does not currently use libpk
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt()

View file

@ -47,7 +47,6 @@ pub async fn run_server(cache: Arc<DiscordCache>) -> anyhow::Result<()> {
get(|State(cache): State<Arc<DiscordCache>>, Path(guild_id): Path<u64>| async move {
match cache.guild_permissions(Id::new(guild_id), libpk::config.discord.as_ref().expect("missing discord config").client_id).await {
Ok(val) => {
println!("hh {}", Permissions::all().bits());
status_code(StatusCode::FOUND, to_string(&val.bits()).unwrap())
},
Err(err) => {

View file

@ -18,12 +18,8 @@ mod cache_api;
mod discord;
mod logger;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
libpk::init_logging("gateway")?;
libpk::init_metrics()?;
info!("hello world");
libpk::main!("gateway");
async fn real_main() -> anyhow::Result<()> {
let (shutdown_tx, shutdown_rx) = channel::<()>();
let shutdown_tx = Arc::new(shutdown_tx);