Skip to content

Commit

Permalink
fix: improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sargon64 committed Jan 13, 2024
1 parent b533902 commit 919535d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async-graphql = { version = "6.0.11", features = ["uuid08", "chrono", "playgroun
tracing = "0.1.40"
tracing-log = "0.2.0"
# async-graphql-actix-web = "6.0.11"
tracing-subscriber = { version = "0.3.18", features = ["chrono", "env-filter", "fmt"] }
tracing-subscriber = { version = "0.3.18", features = ["chrono", "env-filter", "fmt", "tracing-log"] }
poem = { version = "1.3.59", features = ["test"] }
async-graphql-poem = "6.0.11"
tokio = { version = "1.35.0", features = ["macros", "rt-multi-thread"] }
Expand Down
6 changes: 3 additions & 3 deletions src/cdn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use forge_lib::structs::v1::{unpack_v1_forgemod, ForgeModTypes};
use poem::{handler, http::StatusCode, web::Path, IntoResponse, Response};
use serde::Deserialize;
use sqlx::PgPool;
use tracing::error;
use tracing::{warn, error};

use crate::{models, DB_POOL};

Expand All @@ -26,7 +26,7 @@ async fn cdn_handler(
{
Ok(db_mod) => db_mod,
Err(e) => {
error!("{}", e);
warn!("{}", e);

return Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
Expand Down Expand Up @@ -71,7 +71,7 @@ async fn cdn_handler(
let package = match unpack_v1_forgemod(&*file) {
Ok(pkg) => pkg,
Err(e) => {
error!("{}", e);
warn!("{}", e);

return Response::builder()
.status(StatusCode::BAD_REQUEST)
Expand Down
4 changes: 2 additions & 2 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use forge_lib::structs::{
};
use rand::{distributions::Alphanumeric, Rng};
use semver::{Version, VersionReq};
use tracing::{info, error};
use tracing::{info, error, warn};

use crate::{
mods::_upload_mod,
Expand Down Expand Up @@ -129,7 +129,7 @@ pub async fn generate_mod(api_key: String, slug: Option<String>) -> anyhow::Resu
if res.status().is_success() {
Ok(())
} else {
error!("Failed to upload mod. Server returned {}", res.status());
error!("{}", res.status());
Err(anyhow::anyhow!("Failed to upload mod"))
}
}
Expand Down
32 changes: 22 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![deny(clippy::unwrap_used, clippy::print_stdout)]

use std::{path::Path, sync::Arc};
use std::{path::Path, sync::Arc, f64::consts::E};

use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig, GraphiQLSource},
Expand All @@ -9,12 +9,13 @@ use async_graphql::{
use async_graphql_poem::GraphQL;
use cached::async_sync::OnceCell;
use poem::{
get, handler, http::StatusCode, listener::TcpListener, post, IntoResponse, Response, Route,
get, handler, http::StatusCode, listener::TcpListener, post, IntoResponse, Response, Route, EndpointExt,
};
use rand::Rng;
use search::MeiliMigrator;
use sqlx::{migrate::Migrator, postgres::PgPoolOptions, PgPool};
use sqlx::{migrate::Migrator, postgres::{PgPoolOptions, PgConnectOptions}, PgPool, ConnectOptions};
use tracing::{error, info, warn};
use tracing_subscriber::filter;

mod auth;
mod cdn;
Expand Down Expand Up @@ -124,7 +125,7 @@ async fn index() -> impl IntoResponse {
{
Ok(record) => record,
Err(e) => {
error!("{}", e);
warn!("{}", e);

return Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
Expand All @@ -140,7 +141,7 @@ async fn index() -> impl IntoResponse {
{
Ok(record) => record,
Err(e) => {
error!("{}", e);
warn!("{}", e);

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

res.push_str("<br>");

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));
res.push_str(&format!("<p>Currently Serving <a>{}</a> Users and <a>{}</a> Mods.</p>", user_count, mod_count));

res.push_str("<br>");

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

res.push_str("</body></html>");
res.into_response()
res.into_response().with_content_type("text/html; charset=utf-8").into_response()
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
tracing_subscriber::fmt().init();
tracing_subscriber::fmt().with_env_filter(filter::EnvFilter::from_default_env()).init();

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

Expand All @@ -198,7 +199,16 @@ async fn main() -> anyhow::Result<()> {
let pool = PgPoolOptions::new()
.min_connections(5)
.max_connections(20)
.connect(&std::env::var("BF_DATABASE_URL")?)
// .connect(&std::env::var("BF_DATABASE_URL")?)
.connect_with(
match std::env::var("BF_DATABASE_URL")?.parse::<PgConnectOptions>() {
Ok(opt) => opt,
Err(e) => {
error!("{}", e);
return Err(anyhow::anyhow!("Failed to parse database URL"));
},
}.log_statements(tracing_log::log::LevelFilter::Debug).log_slow_statements(tracing_log::log::LevelFilter::Warn, std::time::Duration::from_millis(500))
)
.await?;

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

poem::Server::new(TcpListener::bind("0.0.0.0:8080"))
.run(app)
Expand Down
Loading

0 comments on commit 919535d

Please sign in to comment.