2023-10-21 05:11:42 +02:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
2023-11-02 11:29:34 +01:00
|
|
|
use log::error;
|
|
|
|
use log::info;
|
2023-11-14 08:40:45 +01:00
|
|
|
use sqlx::postgres::PgPoolOptions;
|
|
|
|
use sqlx::PgPool;
|
2023-10-10 00:03:04 +02:00
|
|
|
|
2023-10-21 05:11:42 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct CaptchaState {
|
|
|
|
pub capthca_db: Arc<Mutex<BTreeMap<i32, String>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CaptchaState {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
capthca_db: Arc::new(Mutex::new(BTreeMap::new())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 00:03:04 +02:00
|
|
|
#[derive(Clone)]
|
2023-10-18 04:04:16 +02:00
|
|
|
pub struct ServerState {
|
2023-11-14 08:40:45 +01:00
|
|
|
pub pool: PgPool,
|
2023-10-10 00:03:04 +02:00
|
|
|
}
|
|
|
|
|
2023-10-18 04:04:16 +02:00
|
|
|
impl ServerState {
|
2023-10-10 00:03:04 +02:00
|
|
|
pub async fn new() -> Self {
|
2023-10-20 20:57:58 +02:00
|
|
|
// This is almost certainly bad practice for more reasons than I can count
|
|
|
|
dotenvy::dotenv().ok();
|
2023-11-06 00:23:44 +01:00
|
|
|
let db_url = dotenvy::var("DATABASE_URL").unwrap_or_else(|_| {
|
|
|
|
error!("DATABASE_URL not set in environment!");
|
|
|
|
std::process::exit(1);
|
|
|
|
});
|
|
|
|
|
2023-11-02 11:29:34 +01:00
|
|
|
info!("Using db_url: {}", &db_url);
|
2023-10-20 20:57:58 +02:00
|
|
|
|
2023-11-14 08:40:45 +01:00
|
|
|
let pool = PgPoolOptions::new()
|
2023-10-10 00:03:04 +02:00
|
|
|
.max_connections(5)
|
2023-10-20 20:57:58 +02:00
|
|
|
.connect(&db_url)
|
2023-10-10 00:03:04 +02:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2023-11-14 08:40:45 +01:00
|
|
|
sqlx::migrate!("./migrations_pg").run(&pool).await.unwrap();
|
2023-10-10 00:03:04 +02:00
|
|
|
|
2023-11-02 11:29:34 +01:00
|
|
|
match crate::db::db_new_user("imbus".to_string(), "kartellen1234".to_string(), &pool).await
|
|
|
|
{
|
|
|
|
Some(u) => info!("Created default user {}", u.username),
|
|
|
|
None => error!("Failed to create default user..."),
|
|
|
|
}
|
|
|
|
|
2023-10-20 22:49:09 +02:00
|
|
|
#[cfg(debug_assertions)]
|
2023-10-21 00:59:35 +02:00
|
|
|
debug_setup(&pool).await.unwrap();
|
2023-10-20 22:49:09 +02:00
|
|
|
|
|
|
|
Self { pool }
|
2023-10-10 00:03:04 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-20 22:49:09 +02:00
|
|
|
|
|
|
|
// Inserts a bunch of dummy data into the database
|
|
|
|
// Mostly useful for debugging new posts, as we need to satisfy foreign key constraints.
|
|
|
|
#[cfg(debug_assertions)]
|
2023-11-14 08:40:45 +01:00
|
|
|
async fn debug_setup(pool: &PgPool) -> Result<(), sqlx::Error> {
|
2023-10-20 22:49:09 +02:00
|
|
|
use sqlx::query;
|
|
|
|
|
2023-10-21 07:54:26 +02:00
|
|
|
use crate::db::db_new_user;
|
|
|
|
|
2023-10-21 08:51:51 +02:00
|
|
|
db_new_user("user".to_string(), "pass".to_string(), pool).await;
|
2023-10-21 00:59:35 +02:00
|
|
|
|
2023-10-21 08:58:21 +02:00
|
|
|
// Check if the demo post already exists
|
|
|
|
let posted = query!("SELECT * FROM posts WHERE id = 1",)
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
|
|
|
.ok();
|
|
|
|
|
|
|
|
// If the demo user already has a post, don't insert another one
|
|
|
|
if !posted.is_some() {
|
|
|
|
// This requires that the user with id 1 exists in the user table
|
|
|
|
query!("INSERT INTO posts (user_id, content) VALUES (1, 'Hello world! The demo username is user and the password is pass.')",)
|
2023-10-21 00:59:35 +02:00
|
|
|
.execute(pool)
|
|
|
|
.await?;
|
2023-10-21 08:58:21 +02:00
|
|
|
}
|
2023-10-21 00:59:35 +02:00
|
|
|
|
|
|
|
Ok(())
|
2023-10-20 22:49:09 +02:00
|
|
|
}
|