read GlobalConfig from configuration.toml

This commit is contained in:
Ilion Beyst 2022-07-16 21:57:12 +02:00
parent 0cf7b5299d
commit dad19548d1
2 changed files with 30 additions and 19 deletions

View file

@ -1 +1,11 @@
database_url = "postgresql://planetwars:planetwars@localhost/planetwars" database_url = "postgresql://planetwars:planetwars@localhost/planetwars"
python_runner_image = "python:3.10-slim-buster"
container_registry_url = "localhost:9001"
bots_directory = "./data/bots"
match_logs_directory = "./data/matches"
maps_directory = "./data/maps"
registry_directory = "./data/registry"
registry_admin_password ="verysecretadminpassword"

View file

@ -18,7 +18,7 @@ use config::ConfigError;
use diesel::{Connection, PgConnection}; use diesel::{Connection, PgConnection};
use modules::ranking::run_ranker; use modules::ranking::run_ranker;
use modules::registry::registry_service; use modules::registry::registry_service;
use serde::Deserialize; use serde::{Deserialize, Serialize};
use axum::{ use axum::{
async_trait, async_trait,
@ -33,15 +33,29 @@ const SIMPLEBOT_PATH: &str = "../simplebot/simplebot.py";
type ConnectionPool = bb8::Pool<DieselConnectionManager<PgConnection>>; type ConnectionPool = bb8::Pool<DieselConnectionManager<PgConnection>>;
#[derive(Serialize, Deserialize)]
pub struct GlobalConfig { pub struct GlobalConfig {
/// url for the postgres database
pub database_url: String,
/// which image to use for running python bots
pub python_runner_image: String, pub python_runner_image: String,
/// url for the internal container registry
/// this will be used when running bots
pub container_registry_url: String, pub container_registry_url: String,
/// directory where bot code will be stored
pub bots_directory: String, pub bots_directory: String,
/// directory where match logs will be stored
pub match_logs_directory: String, pub match_logs_directory: String,
/// directory where map files will be stored
pub maps_directory: String, pub maps_directory: String,
/// base directory for registry data
pub registry_directory: String, pub registry_directory: String,
/// secret admin password for internal docker login
/// used to pull bots when running matches
pub registry_admin_password: String, pub registry_admin_password: String,
} }
@ -71,8 +85,8 @@ pub async fn seed_simplebot(config: &GlobalConfig, pool: &ConnectionPool) {
pub type DbPool = Pool<DieselConnectionManager<PgConnection>>; pub type DbPool = Pool<DieselConnectionManager<PgConnection>>;
pub async fn prepare_db(database_url: &str, config: &GlobalConfig) -> DbPool { pub async fn prepare_db(config: &GlobalConfig) -> DbPool {
let manager = DieselConnectionManager::<PgConnection>::new(database_url); let manager = DieselConnectionManager::<PgConnection>::new(&config.database_url);
let pool = bb8::Pool::builder().build(manager).await.unwrap(); let pool = bb8::Pool::builder().build(manager).await.unwrap();
seed_simplebot(&config, &pool).await; seed_simplebot(&config, &pool).await;
pool pool
@ -104,7 +118,7 @@ pub fn api() -> Router {
.route("/save_bot", post(routes::bots::save_bot)) .route("/save_bot", post(routes::bots::save_bot))
} }
pub fn get_config() -> Result<Configuration, ConfigError> { pub fn get_config() -> Result<GlobalConfig, ConfigError> {
config::Config::builder() config::Config::builder()
.add_source(config::File::with_name("configuration.toml")) .add_source(config::File::with_name("configuration.toml"))
.add_source(config::Environment::with_prefix("PLANETWARS")) .add_source(config::Environment::with_prefix("PLANETWARS"))
@ -128,21 +142,8 @@ async fn run_registry(config: Arc<GlobalConfig>, db_pool: DbPool) {
} }
pub async fn run_app() { pub async fn run_app() {
let configuration = get_config().unwrap(); let global_config = Arc::new(get_config().unwrap());
let db_pool = prepare_db(&global_config).await;
let global_config = Arc::new(GlobalConfig {
python_runner_image: "python:3.10-slim-buster".to_string(),
container_registry_url: "localhost:9001".to_string(),
bots_directory: "./data/bots".to_string(),
match_logs_directory: "./data/matches".to_string(),
maps_directory: "./data/maps".to_string(),
registry_directory: "./data/registry".to_string(),
registry_admin_password: "verysecretadminpassword".to_string(),
});
let db_pool = prepare_db(&configuration.database_url, &global_config).await;
tokio::spawn(run_ranker(global_config.clone(), db_pool.clone())); tokio::spawn(run_ranker(global_config.clone(), db_pool.clone()));
tokio::spawn(run_registry(global_config.clone(), db_pool.clone())); tokio::spawn(run_registry(global_config.clone(), db_pool.clone()));