aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/lib.rs
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-02-20 23:06:05 +0100
committerIlion Beyst <ilion.beyst@gmail.com>2022-02-20 23:06:05 +0100
commit9c90e79575afb3ab2574f1d7e0fc79d7fae90458 (patch)
tree6faf17c06481b52a612b45f9af35185a29c3fb90 /planetwars-server/src/lib.rs
parent8f29332048ce8fb1f06148be82542832a517ec84 (diff)
downloadplanetwars.dev-9c90e79575afb3ab2574f1d7e0fc79d7fae90458.tar.xz
planetwars.dev-9c90e79575afb3ab2574f1d7e0fc79d7fae90458.zip
implement basic configuration
Diffstat (limited to 'planetwars-server/src/lib.rs')
-rw-r--r--planetwars-server/src/lib.rs20
1 files changed, 16 insertions, 4 deletions
diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs
index 94f2965..60b68ed 100644
--- a/planetwars-server/src/lib.rs
+++ b/planetwars-server/src/lib.rs
@@ -14,6 +14,7 @@ use axum;
use bb8::PooledConnection;
use bb8_diesel::{self, DieselConnectionManager};
use diesel::PgConnection;
+use serde::Deserialize;
use axum::{
async_trait,
@@ -30,9 +31,8 @@ const MAPS_DIR: &str = "./data/maps";
type ConnectionPool = bb8::Pool<DieselConnectionManager<PgConnection>>;
-pub async fn api() -> Router {
- let database_url = "postgresql://planetwars:planetwars@localhost/planetwars";
- let manager = DieselConnectionManager::<PgConnection>::new(database_url);
+pub async fn api(configuration: Configuration) -> Router {
+ let manager = DieselConnectionManager::<PgConnection>::new(configuration.database_url);
let pool = bb8::Pool::builder().build(manager).await.unwrap();
let api = Router::new()
@@ -64,10 +64,22 @@ pub async fn api() -> Router {
}
pub async fn app() -> Router {
- let api = api().await;
+ let configuration = config::Config::builder()
+ .add_source(config::File::with_name("configuration.toml"))
+ .add_source(config::Environment::with_prefix("PLANETWARS"))
+ .build()
+ .unwrap()
+ .try_deserialize()
+ .unwrap();
+ let api = api(configuration).await;
Router::new().nest("/api", api)
}
+#[derive(Deserialize)]
+pub struct Configuration {
+ pub database_url: String,
+}
+
// we can also write a custom extractor that grabs a connection from the pool
// which setup is appropriate depends on your application
pub struct DatabaseConnection(PooledConnection<'static, DieselConnectionManager<PgConnection>>);