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
55use async_graphql:: {
66 http:: { playground_source, GraphQLPlaygroundConfig , GraphiQLSource } ,
@@ -9,12 +9,13 @@ use async_graphql::{
99use async_graphql_poem:: GraphQL ;
1010use cached:: async_sync:: OnceCell ;
1111use 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} ;
1414use rand:: Rng ;
1515use search:: MeiliMigrator ;
16- use sqlx:: { migrate:: Migrator , postgres:: PgPoolOptions , PgPool } ;
16+ use sqlx:: { migrate:: Migrator , postgres:: { PgPoolOptions , PgConnectOptions } , PgPool , ConnectOptions } ;
1717use tracing:: { error, info, warn} ;
18+ use tracing_subscriber:: filter;
1819
1920mod auth;
2021mod 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]
181182async 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