Add basic SQLx

This commit is contained in:
traxys 2023-08-14 18:26:38 +02:00
parent 886df477a8
commit c7e3468acc
6 changed files with 1036 additions and 11 deletions

View file

@ -1,7 +1,8 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::Arc};
use axum::Router;
use serde::Deserialize;
use sqlx::{postgres::PgPoolOptions, PgPool};
use tracing_subscriber::EnvFilter;
fn default_port() -> u16 {
@ -19,6 +20,7 @@ struct Settings {
port: u16,
#[serde(default = "default_address")]
address: String,
database_url: String,
}
impl Settings {
@ -31,6 +33,10 @@ impl Settings {
}
}
struct AppState {
db: PgPool,
}
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
@ -46,9 +52,16 @@ async fn main() -> color_eyre::Result<()> {
let addr: SocketAddr = format!("{}:{}", config.address, config.port).parse()?;
let db = PgPoolOptions::new()
.max_connections(5)
.connect(&config.database_url)
.await?;
sqlx::migrate!().run(&db).await?;
tracing::info!("Listening on {addr}");
let router = Router::new();
let router = Router::new().with_state(Arc::new(AppState { db }));
Ok(axum::Server::bind(&addr)
.serve(router.into_make_service())