Delete & Post Game Endpoints. Various other changes

This commit is contained in:
Neshura 2023-08-30 04:16:43 +02:00
parent 790a7dd8ee
commit 07e6df6b64
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
8 changed files with 493 additions and 107 deletions

View file

@ -1,4 +1,11 @@
use std::{error::Error, fs, net::Ipv4Addr, net::Ipv6Addr, thread, time::Duration, sync::{Mutex, Arc, atomic::{AtomicBool, Ordering}}};
#[macro_use]
extern crate dotenv_codegen;
extern crate dotenv;
use dotenv::dotenv;
use tokio::time::sleep;
use std::{env, error::Error, fs, net::Ipv4Addr, net::Ipv6Addr, thread, time::Duration, sync::{Mutex, Arc, atomic::{AtomicBool, Ordering}}};
use chrono::Local;
@ -8,8 +15,6 @@ use sqlx::{PgPool, Pool, Postgres, Connection};
use utoipa::OpenApi;
use utoipa_swagger_ui::{Config, SwaggerUi, Url};
use crate::db::connect_postgres;
mod db;
mod v2;
mod v3;
@ -34,19 +39,9 @@ macro_rules! api_base_3 {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub(crate) struct ConfigToml {
database: PostgresConfig,
auth: AuthenticationTokens,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub(crate) struct PostgresConfig {
host: String,
port: u16,
user: String,
password: String,
db: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub(crate) struct AuthenticationTokens {
moderator: String,
@ -71,21 +66,13 @@ async fn postgres_watchdog(pool: PgPool, is_alive: Arc<AtomicBool>, shutdown: Ar
};
match conn.ping().await {
Ok(_) => {eprintln!("Pinged DB Server at {}", Local::now().format("%H:%M:%S"))},
Err(_) => todo!(),
Ok(_) => {println!("Pinged DB Server at {}", Local::now().format("%H:%M:%S"))},
Err(_) => {println!("Error pinging Server"); break;},
};
let passed = (Local::now() - start).to_std().expect(&format!("Unable to get Time Difference for '{}' and '{}'", start, Local::now()));
thread::sleep(Duration::from_secs(5) - passed);
if shutdown.load(Ordering::Relaxed) {
break;
}
thread::sleep(Duration::from_secs(10) - passed);
if shutdown.load(Ordering::Relaxed) {
break;
}
thread::sleep(Duration::from_secs(15) - passed);
sleep(Duration::from_secs(15) - passed).await;
}
is_alive.store(false, Ordering::Relaxed);
}
@ -93,6 +80,7 @@ async fn postgres_watchdog(pool: PgPool, is_alive: Arc<AtomicBool>, shutdown: Ar
#[actix_web::main]
async fn main() -> Result<()> {
env_logger::init();
dotenv().ok();
let shutdown: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
@ -107,6 +95,8 @@ async fn main() -> Result<()> {
let config: ConfigToml = toml::from_str(&toml_str).expect("Failed to parse config.toml");
println!("DATABASE_URL: {}", env::var("DATABASE_URL").unwrap()); // DBEUG
#[derive(OpenApi)]
#[openapi(
paths(
@ -134,12 +124,24 @@ async fn main() -> Result<()> {
#[openapi(
paths(
v3::full_view_data,
v3::auth,
v3::list_games,
v3::get_game_data,
v3::create_game,
v3::delete_game
),
components(schemas(
v3::schemas::AuthParamsOptional,
v3::schemas::AuthParams,
v3::schemas::AuthReturn,
v3::schemas::GetGameParam,
v3::schemas::PostGameParams,
v3::schemas::DeleteGameParam,
v3::schemas::FullViewData,
v3::schemas::Ethic,
v3::schemas::EmpireEthic,
v3::schemas::ChellarisGame,
v3::schemas::ChellarisGameLite,
v3::schemas::Species,
v3::schemas::ChellarisGameGroup,
v3::schemas::Portrait,
@ -157,7 +159,7 @@ async fn main() -> Result<()> {
loop {
let db_auth_tokens = config.auth.clone();
let pool = connect_postgres(config.database.clone()).await.unwrap();
let pool = PgPool::connect(dotenv!("DATABASE_URL")).await.unwrap();
let pool_copy = pool.clone();
let swagger_config = Config::new(openapi_urls.clone());
@ -182,6 +184,11 @@ async fn main() -> Result<()> {
.service(v2::portraits)
// API v3 Endpoints
.service(v3::full_view_data)
.service(v3::auth)
.service(v3::list_games)
.service(v3::get_game_data)
.service(v3::create_game)
.service(v3::delete_game)
// Swagger UI
.service(
SwaggerUi::new(concat!(api_base!(), "/swagger/{_:.*}"))
@ -202,7 +209,7 @@ async fn main() -> Result<()> {
.run();
let server_thread = tokio::spawn(async {
eprintln!("Awaiting server");
println!("Awaiting server");
let _ = server.await;
println!("Stopped awaiting server");
});