implement basic configuration
This commit is contained in:
parent
8f29332048
commit
9c90e79575
3 changed files with 18 additions and 4 deletions
|
@ -24,6 +24,7 @@ base64 = "0.13.0"
|
||||||
zip = "0.5"
|
zip = "0.5"
|
||||||
toml = "0.5"
|
toml = "0.5"
|
||||||
planetwars-matchrunner = { path = "../planetwars-matchrunner" }
|
planetwars-matchrunner = { path = "../planetwars-matchrunner" }
|
||||||
|
config = { version = "0.12", features = ["toml"] }
|
||||||
|
|
||||||
# TODO: remove me
|
# TODO: remove me
|
||||||
shlex = "1.1"
|
shlex = "1.1"
|
||||||
|
|
1
planetwars-server/configuration.toml
Normal file
1
planetwars-server/configuration.toml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
database_url = "postgresql://planetwars:planetwars@localhost/planetwars"
|
|
@ -14,6 +14,7 @@ use axum;
|
||||||
use bb8::PooledConnection;
|
use bb8::PooledConnection;
|
||||||
use bb8_diesel::{self, DieselConnectionManager};
|
use bb8_diesel::{self, DieselConnectionManager};
|
||||||
use diesel::PgConnection;
|
use diesel::PgConnection;
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
async_trait,
|
async_trait,
|
||||||
|
@ -30,9 +31,8 @@ const MAPS_DIR: &str = "./data/maps";
|
||||||
|
|
||||||
type ConnectionPool = bb8::Pool<DieselConnectionManager<PgConnection>>;
|
type ConnectionPool = bb8::Pool<DieselConnectionManager<PgConnection>>;
|
||||||
|
|
||||||
pub async fn api() -> Router {
|
pub async fn api(configuration: Configuration) -> Router {
|
||||||
let database_url = "postgresql://planetwars:planetwars@localhost/planetwars";
|
let manager = DieselConnectionManager::<PgConnection>::new(configuration.database_url);
|
||||||
let manager = DieselConnectionManager::<PgConnection>::new(database_url);
|
|
||||||
let pool = bb8::Pool::builder().build(manager).await.unwrap();
|
let pool = bb8::Pool::builder().build(manager).await.unwrap();
|
||||||
|
|
||||||
let api = Router::new()
|
let api = Router::new()
|
||||||
|
@ -64,10 +64,22 @@ pub async fn api() -> Router {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn app() -> 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)
|
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
|
// we can also write a custom extractor that grabs a connection from the pool
|
||||||
// which setup is appropriate depends on your application
|
// which setup is appropriate depends on your application
|
||||||
pub struct DatabaseConnection(PooledConnection<'static, DieselConnectionManager<PgConnection>>);
|
pub struct DatabaseConnection(PooledConnection<'static, DieselConnectionManager<PgConnection>>);
|
||||||
|
|
Loading…
Reference in a new issue