basic db setup

This commit is contained in:
Hamcha 2023-06-26 10:08:49 +02:00
commit 8eb70cdc40
Signed by: hamcha
GPG Key ID: 1669C533B8CF6D89
5 changed files with 1918 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
/docker-data

1835
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

15
Cargo.toml Normal file
View File

@ -0,0 +1,15 @@
[package]
name = "mabel"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = "0.6"
tracing = "0.1"
tracing-subscriber = "0.3"
tokio = { version = "1.28", features = ["full"] }
sqlx = { version = "0.6", features = [ "runtime-tokio-rustls", "postgres", "uuid", "chrono", "macros", "migrate" ] }
uuid = { version = "1.3", features = ["v4", "fast-rng"] }
serde = { version = "1" }
figment = { version = "0.10", features = ["toml", "env"] }
anyhow = "1.0"

11
docker-compose.yaml Normal file
View File

@ -0,0 +1,11 @@
services:
database:
image: postgres
environment:
POSTGRES_USER: artificiale
POSTGRES_PASSWORD: changeme
POSTGRES_DB: artificiale
ports:
- 5432:5432
volumes:
- ./docker-data/database:/var/lib/postgresql

55
src/main.rs Normal file
View File

@ -0,0 +1,55 @@
use anyhow::{Ok, Result};
use axum::{routing::get, Router, Server};
use figment::{
providers::{Env, Format, Serialized, Toml},
Figment,
};
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgPoolOptions;
use std::net::SocketAddr;
#[derive(Deserialize, Serialize)]
struct Config {
bind: String,
database_url: String,
}
impl Default for Config {
fn default() -> Self {
Config {
bind: "127.0.0.1:3000".to_owned(),
database_url: "postgres://artificiale:changeme@localhost/artificiale".to_owned(),
}
}
}
async fn root() -> &'static str {
"Hello, World!"
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let config: Config = Figment::from(Serialized::defaults(Config::default()))
.merge(Toml::file("mabel.toml"))
.merge(Env::prefixed("MABEL_"))
.extract()?;
let app = Router::new().route("/", get(root));
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(config.database_url.as_str())
.await?;
sqlx::migrate!().run(&pool).await?;
let addr: SocketAddr = config.bind.parse()?;
tracing::debug!("listening on {}", addr);
Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
Ok(())
}