restructure server entrypoint for ranker
This commit is contained in:
parent
450d22758e
commit
e7da88c6a5
4 changed files with 34 additions and 22 deletions
|
@ -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)]
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
5
planetwars-server/src/modules/ranking.rs
Normal file
5
planetwars-server/src/modules/ranking.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
use crate::DbPool;
|
||||
|
||||
pub async fn run_ranker(_db_pool: DbPool) {
|
||||
// do nothing, for now
|
||||
}
|
Loading…
Reference in a new issue