restructure server entrypoint for ranker

This commit is contained in:
Ilion Beyst 2022-04-27 20:43:12 +02:00
parent 450d22758e
commit e7da88c6a5
4 changed files with 34 additions and 22 deletions

View file

@ -8,11 +8,14 @@ pub mod routes;
pub mod schema; pub mod schema;
pub mod util; pub mod util;
use std::net::SocketAddr;
use std::ops::Deref; use std::ops::Deref;
use bb8::{Pool, PooledConnection}; use bb8::{Pool, PooledConnection};
use bb8_diesel::{self, DieselConnectionManager}; use bb8_diesel::{self, DieselConnectionManager};
use config::ConfigError;
use diesel::{Connection, PgConnection}; use diesel::{Connection, PgConnection};
use modules::ranking::run_ranker;
use serde::Deserialize; use serde::Deserialize;
use axum::{ 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 manager = DieselConnectionManager::<PgConnection>::new(database_url);
let pool = bb8::Pool::builder().build(manager).await.unwrap(); let pool = bb8::Pool::builder().build(manager).await.unwrap();
seed_simplebot(&pool).await; seed_simplebot(&pool).await;
pool pool
} }
pub async fn api(configuration: Configuration) -> Router { pub fn api() -> Router {
let db_pool = prepare_db(&configuration.database_url).await;
Router::new() Router::new()
.route("/register", post(routes::users::register)) .route("/register", post(routes::users::register))
.route("/login", post(routes::users::login)) .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("/submit_bot", post(routes::demo::submit_bot))
.route("/save_bot", post(routes::bots::save_bot)) .route("/save_bot", post(routes::bots::save_bot))
.layer(AddExtensionLayer::new(db_pool))
} }
pub async fn app() -> Router { pub fn get_config() -> Result<Configuration, ConfigError> {
let configuration = 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"))
.build() .build()?
.unwrap()
.try_deserialize() .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)] #[derive(Deserialize)]

View file

@ -1,16 +1,7 @@
use std::net::SocketAddr;
extern crate planetwars_server; extern crate planetwars_server;
extern crate tokio; extern crate tokio;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let app = planetwars_server::app().await; planetwars_server::run_app().await;
let addr = SocketAddr::from(([127, 0, 0, 1], 9000));
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
} }

View file

@ -1,3 +1,4 @@
// This module implements general domain logic, not directly // This module implements general domain logic, not directly
// tied to the database or API layers. // tied to the database or API layers.
pub mod bots; pub mod bots;
pub mod ranking;

View file

@ -0,0 +1,5 @@
use crate::DbPool;
pub async fn run_ranker(_db_pool: DbPool) {
// do nothing, for now
}