mabel/src/main.rs
2023-06-26 10:08:49 +02:00

55 lines
1.3 KiB
Rust

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(())
}