init axum app
This commit is contained in:
parent
bbcc98edc7
commit
4d167f7160
3 changed files with 3049 additions and 2 deletions
2973
Cargo.lock
generated
Normal file
2973
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
37
Cargo.toml
37
Cargo.toml
|
@ -4,3 +4,40 @@ version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
thiserror="2"
|
||||||
|
|
||||||
|
tracing="0.1"
|
||||||
|
tracing-subscriber={version="0.3", features=["env-filter"]}
|
||||||
|
tracing-appender="0.2"
|
||||||
|
axum = "0.8"
|
||||||
|
|
||||||
|
serde_json="1"
|
||||||
|
|
||||||
|
|
||||||
|
[dependencies.reqwest]
|
||||||
|
version="0.12"
|
||||||
|
features=["json"]
|
||||||
|
|
||||||
|
[dependencies.tower-http]
|
||||||
|
version="0.6"
|
||||||
|
features=["trace"]
|
||||||
|
|
||||||
|
[dependencies.serde]
|
||||||
|
version="1"
|
||||||
|
features=["derive"]
|
||||||
|
|
||||||
|
# Async runtime
|
||||||
|
[dependencies.tokio]
|
||||||
|
version="1"
|
||||||
|
features = ["full"]
|
||||||
|
|
||||||
|
[dependencies.sqlx]
|
||||||
|
version = "0.8"
|
||||||
|
features = [
|
||||||
|
"postgres",
|
||||||
|
"runtime-tokio",
|
||||||
|
"uuid",
|
||||||
|
"chrono",
|
||||||
|
"migrate",
|
||||||
|
"derive"
|
||||||
|
]
|
||||||
|
|
41
src/main.rs
41
src/main.rs
|
@ -1,3 +1,40 @@
|
||||||
fn main() {
|
use std::time::Duration;
|
||||||
println!("Hello, world!");
|
|
||||||
|
use axum::{routing::get, Router};
|
||||||
|
use sqlx::postgres::PgPoolOptions;
|
||||||
|
use tokio::net::TcpListener;
|
||||||
|
use tracing::info;
|
||||||
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
tracing_subscriber::registry()
|
||||||
|
.with(tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "goathacks-backend=debug".into()))
|
||||||
|
.with(tracing_subscriber::fmt::layer())
|
||||||
|
.init();
|
||||||
|
|
||||||
|
let db_connection_str = std::env::var("DATABASE_URL")
|
||||||
|
.unwrap_or_else(|_| "postgres://localhost/goathacks".into());
|
||||||
|
|
||||||
|
let pool = PgPoolOptions::new()
|
||||||
|
.max_connections(5)
|
||||||
|
.acquire_timeout(Duration::from_secs(3))
|
||||||
|
.connect(&db_connection_str)
|
||||||
|
.await
|
||||||
|
.expect("Can't connect to database");
|
||||||
|
|
||||||
|
let app = Router::new()
|
||||||
|
.route("/health", get(health_check))
|
||||||
|
.with_state(pool);
|
||||||
|
|
||||||
|
let bind_addr = std::env::var("BIND_ADDR").unwrap_or_else(|_| "127.0.0.1:1998".into());
|
||||||
|
let listener = TcpListener::bind(bind_addr.clone()).await.unwrap();
|
||||||
|
info!("Listening on {}", bind_addr);
|
||||||
|
axum::serve(listener, app).await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn health_check() -> &'static str {
|
||||||
|
"OK"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue