-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
63 lines (50 loc) · 1.6 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
mod config;
mod controllers;
mod middlewares;
mod router;
mod services;
mod structs;
mod utils;
use config::Config;
use dotenv::dotenv;
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
use std::sync::Arc;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use utils::graceful_shutdown::shutdown_signal;
use crate::router::router::create_router;
pub struct AppState {
db: Pool<Postgres>,
env: Config,
}
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
// axum logs rejections from built-in extractors with the `axum::rejection`
// target, at `TRACE` level. `axum::rejection=trace` enables showing those events
"example_tracing_aka_logging=debug,tower_http=debug,axum::rejection=trace".into()
}),
)
.with(tracing_subscriber::fmt::layer())
.init();
dotenv().ok();
let config = Config::init();
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&config.database_url)
.await?;
sqlx::migrate!().run(&pool).await?;
let app_state = Arc::new(AppState {
db: pool.clone(),
env: config.clone(),
});
let app = create_router(app_state);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
println!("🚀 Server started successfully");
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await
.unwrap();
Ok(())
}