Skip to content

Commit 919535d

Browse files
committed
fix: improved logging
1 parent b533902 commit 919535d

File tree

6 files changed

+84
-71
lines changed

6 files changed

+84
-71
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async-graphql = { version = "6.0.11", features = ["uuid08", "chrono", "playgroun
4141
tracing = "0.1.40"
4242
tracing-log = "0.2.0"
4343
# async-graphql-actix-web = "6.0.11"
44-
tracing-subscriber = { version = "0.3.18", features = ["chrono", "env-filter", "fmt"] }
44+
tracing-subscriber = { version = "0.3.18", features = ["chrono", "env-filter", "fmt", "tracing-log"] }
4545
poem = { version = "1.3.59", features = ["test"] }
4646
async-graphql-poem = "6.0.11"
4747
tokio = { version = "1.35.0", features = ["macros", "rt-multi-thread"] }

src/cdn.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use forge_lib::structs::v1::{unpack_v1_forgemod, ForgeModTypes};
33
use poem::{handler, http::StatusCode, web::Path, IntoResponse, Response};
44
use serde::Deserialize;
55
use sqlx::PgPool;
6-
use tracing::error;
6+
use tracing::{warn, error};
77

88
use crate::{models, DB_POOL};
99

@@ -26,7 +26,7 @@ async fn cdn_handler(
2626
{
2727
Ok(db_mod) => db_mod,
2828
Err(e) => {
29-
error!("{}", e);
29+
warn!("{}", e);
3030

3131
return Response::builder()
3232
.status(StatusCode::INTERNAL_SERVER_ERROR)
@@ -71,7 +71,7 @@ async fn cdn_handler(
7171
let package = match unpack_v1_forgemod(&*file) {
7272
Ok(pkg) => pkg,
7373
Err(e) => {
74-
error!("{}", e);
74+
warn!("{}", e);
7575

7676
return Response::builder()
7777
.status(StatusCode::BAD_REQUEST)

src/debug.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use forge_lib::structs::{
44
};
55
use rand::{distributions::Alphanumeric, Rng};
66
use semver::{Version, VersionReq};
7-
use tracing::{info, error};
7+
use tracing::{info, error, warn};
88

99
use crate::{
1010
mods::_upload_mod,
@@ -129,7 +129,7 @@ pub async fn generate_mod(api_key: String, slug: Option<String>) -> anyhow::Resu
129129
if res.status().is_success() {
130130
Ok(())
131131
} else {
132-
error!("Failed to upload mod. Server returned {}", res.status());
132+
error!("{}", res.status());
133133
Err(anyhow::anyhow!("Failed to upload mod"))
134134
}
135135
}

src/main.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![deny(clippy::unwrap_used, clippy::print_stdout)]
22

3-
use std::{path::Path, sync::Arc};
3+
use std::{path::Path, sync::Arc, f64::consts::E};
44

55
use async_graphql::{
66
http::{playground_source, GraphQLPlaygroundConfig, GraphiQLSource},
@@ -9,12 +9,13 @@ use async_graphql::{
99
use async_graphql_poem::GraphQL;
1010
use cached::async_sync::OnceCell;
1111
use poem::{
12-
get, handler, http::StatusCode, listener::TcpListener, post, IntoResponse, Response, Route,
12+
get, handler, http::StatusCode, listener::TcpListener, post, IntoResponse, Response, Route, EndpointExt,
1313
};
1414
use rand::Rng;
1515
use search::MeiliMigrator;
16-
use sqlx::{migrate::Migrator, postgres::PgPoolOptions, PgPool};
16+
use sqlx::{migrate::Migrator, postgres::{PgPoolOptions, PgConnectOptions}, PgPool, ConnectOptions};
1717
use tracing::{error, info, warn};
18+
use tracing_subscriber::filter;
1819

1920
mod auth;
2021
mod cdn;
@@ -124,7 +125,7 @@ async fn index() -> impl IntoResponse {
124125
{
125126
Ok(record) => record,
126127
Err(e) => {
127-
error!("{}", e);
128+
warn!("{}", e);
128129

129130
return Response::builder()
130131
.status(StatusCode::INTERNAL_SERVER_ERROR)
@@ -140,7 +141,7 @@ async fn index() -> impl IntoResponse {
140141
{
141142
Ok(record) => record,
142143
Err(e) => {
143-
error!("{}", e);
144+
warn!("{}", e);
144145

145146
return Response::builder()
146147
.status(StatusCode::INTERNAL_SERVER_ERROR)
@@ -159,7 +160,7 @@ async fn index() -> impl IntoResponse {
159160

160161
res.push_str("<br>");
161162

162-
res.push_str(&format!("<p>Currently Serving <a style=\"color: #ff0000\">{}</a> Users and <a style=\"color: #0000ff\">{}</a> Mods.</p>", user_count, mod_count));
163+
res.push_str(&format!("<p>Currently Serving <a>{}</a> Users and <a>{}</a> Mods.</p>", user_count, mod_count));
163164

164165
res.push_str("<br>");
165166

@@ -174,13 +175,13 @@ async fn index() -> impl IntoResponse {
174175
));
175176

176177
res.push_str("</body></html>");
177-
res.into_response()
178+
res.into_response().with_content_type("text/html; charset=utf-8").into_response()
178179
}
179180

180181
#[tokio::main]
181182
async fn main() -> anyhow::Result<()> {
182183
dotenv::dotenv().ok();
183-
tracing_subscriber::fmt().init();
184+
tracing_subscriber::fmt().with_env_filter(filter::EnvFilter::from_default_env()).init();
184185

185186
info!("{}", text_to_ascii_art::convert("Beat-Forge-API".to_string()).expect("Should not fail as it is a constant, utf-8 string"));
186187

@@ -198,7 +199,16 @@ async fn main() -> anyhow::Result<()> {
198199
let pool = PgPoolOptions::new()
199200
.min_connections(5)
200201
.max_connections(20)
201-
.connect(&std::env::var("BF_DATABASE_URL")?)
202+
// .connect(&std::env::var("BF_DATABASE_URL")?)
203+
.connect_with(
204+
match std::env::var("BF_DATABASE_URL")?.parse::<PgConnectOptions>() {
205+
Ok(opt) => opt,
206+
Err(e) => {
207+
error!("{}", e);
208+
return Err(anyhow::anyhow!("Failed to parse database URL"));
209+
},
210+
}.log_statements(tracing_log::log::LevelFilter::Debug).log_slow_statements(tracing_log::log::LevelFilter::Warn, std::time::Duration::from_millis(500))
211+
)
202212
.await?;
203213

204214
DB_POOL.set(pool.clone())?;
@@ -238,7 +248,9 @@ async fn main() -> anyhow::Result<()> {
238248
.at("/cdn/:slug@:version", get(cdn::cdn_get_typeless))
239249
.at("/mods", post(mods::upload_mod))
240250
.at("/auth/github", post(users::user_auth))
241-
.at("/me", get(users::get_me));
251+
.at("/me", get(users::get_me))
252+
.at("/", get(index))
253+
.with(poem::middleware::Tracing);
242254

243255
poem::Server::new(TcpListener::bind("0.0.0.0:8080"))
244256
.run(app)

0 commit comments

Comments
 (0)