aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-04-27 20:43:12 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-04-27 20:43:12 +0200
commite7da88c6a5135e78acea3a29040cfbf1e7e0b71f (patch)
tree4665e63cf40b8d7a96158a1273d0ca1b9fa261a4
parent450d22758ec169ece8f71bc6bd6c79603c136874 (diff)
downloadplanetwars.dev-e7da88c6a5135e78acea3a29040cfbf1e7e0b71f.tar.xz
planetwars.dev-e7da88c6a5135e78acea3a29040cfbf1e7e0b71f.zip
restructure server entrypoint for ranker
-rw-r--r--planetwars-server/src/lib.rs39
-rw-r--r--planetwars-server/src/main.rs11
-rw-r--r--planetwars-server/src/modules/mod.rs1
-rw-r--r--planetwars-server/src/modules/ranking.rs5
4 files changed, 34 insertions, 22 deletions
diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs
index 51d6613..3f6caa9 100644
--- a/planetwars-server/src/lib.rs
+++ b/planetwars-server/src/lib.rs
@@ -8,11 +8,14 @@ pub mod routes;
pub mod schema;
pub mod util;
+use std::net::SocketAddr;
use std::ops::Deref;
use bb8::{Pool, PooledConnection};
use bb8_diesel::{self, DieselConnectionManager};
+use config::ConfigError;
use diesel::{Connection, PgConnection};
+use modules::ranking::run_ranker;
use serde::Deserialize;
use axum::{
@@ -55,16 +58,16 @@ pub async fn seed_simplebot(pool: &ConnectionPool) {
});
}
-pub async fn prepare_db(database_url: &str) -> Pool<DieselConnectionManager<PgConnection>> {
+pub type DbPool = Pool<DieselConnectionManager<PgConnection>>;
+
+pub async fn prepare_db(database_url: &str) -> DbPool {
let manager = DieselConnectionManager::<PgConnection>::new(database_url);
let pool = bb8::Pool::builder().build(manager).await.unwrap();
seed_simplebot(&pool).await;
pool
}
-pub async fn api(configuration: Configuration) -> Router {
- let db_pool = prepare_db(&configuration.database_url).await;
-
+pub fn api() -> Router {
Router::new()
.route("/register", post(routes::users::register))
.route("/login", post(routes::users::login))
@@ -90,19 +93,31 @@ pub async fn api(configuration: Configuration) -> Router {
)
.route("/submit_bot", post(routes::demo::submit_bot))
.route("/save_bot", post(routes::bots::save_bot))
- .layer(AddExtensionLayer::new(db_pool))
}
-pub async fn app() -> Router {
- let configuration = config::Config::builder()
+pub fn get_config() -> Result<Configuration, ConfigError> {
+ config::Config::builder()
.add_source(config::File::with_name("configuration.toml"))
.add_source(config::Environment::with_prefix("PLANETWARS"))
- .build()
- .unwrap()
+ .build()?
.try_deserialize()
- .unwrap();
- let api = api(configuration).await;
- Router::new().nest("/api", api)
+}
+
+pub async fn run_app() {
+ let configuration = get_config().unwrap();
+ let db_pool = prepare_db(&configuration.database_url).await;
+
+ tokio::spawn(run_ranker(db_pool.clone()));
+
+ let api_service = Router::new()
+ .nest("/api", api())
+ .layer(AddExtensionLayer::new(db_pool))
+ .into_make_service();
+
+ // TODO: put in config
+ let addr = SocketAddr::from(([127, 0, 0, 1], 9000));
+
+ axum::Server::bind(&addr).serve(api_service).await.unwrap();
}
#[derive(Deserialize)]
diff --git a/planetwars-server/src/main.rs b/planetwars-server/src/main.rs
index 9bd283e..9ed77b3 100644
--- a/planetwars-server/src/main.rs
+++ b/planetwars-server/src/main.rs
@@ -1,16 +1,7 @@
-use std::net::SocketAddr;
-
extern crate planetwars_server;
extern crate tokio;
#[tokio::main]
async fn main() {
- let app = planetwars_server::app().await;
-
- let addr = SocketAddr::from(([127, 0, 0, 1], 9000));
-
- axum::Server::bind(&addr)
- .serve(app.into_make_service())
- .await
- .unwrap();
+ planetwars_server::run_app().await;
}
diff --git a/planetwars-server/src/modules/mod.rs b/planetwars-server/src/modules/mod.rs
index 57c1ef5..2efce4e 100644
--- a/planetwars-server/src/modules/mod.rs
+++ b/planetwars-server/src/modules/mod.rs
@@ -1,3 +1,4 @@
// This module implements general domain logic, not directly
// tied to the database or API layers.
pub mod bots;
+pub mod ranking;
diff --git a/planetwars-server/src/modules/ranking.rs b/planetwars-server/src/modules/ranking.rs
new file mode 100644
index 0000000..739e6a6
--- /dev/null
+++ b/planetwars-server/src/modules/ranking.rs
@@ -0,0 +1,5 @@
+use crate::DbPool;
+
+pub async fn run_ranker(_db_pool: DbPool) {
+ // do nothing, for now
+}