Switch server to sqlite

This commit is contained in:
2024-07-27 13:38:55 -04:00
Unverified
parent c134a78062
commit daeb5180fa
6 changed files with 164 additions and 242 deletions

View File

@@ -4,13 +4,16 @@ use std::net::{IpAddr, Ipv6Addr};
use dotenvy::dotenv;
use futures::future::{self};
use futures::StreamExt;
use sqlx::mysql::MySqlPoolOptions;
use sqlx::migrate::MigrateDatabase;
use sqlx::{migrate, Sqlite, SqlitePool};
use sqlx::sqlite::SqlitePoolOptions;
use tarpc::{
server::{Channel},
tokio_serde::formats::Json,
};
use tarpc::server::incoming::Incoming;
use tarpc::server::BaseChannel;
use tracing::{info, subscriber, warn};
use realm_server::server::RealmChatServer;
use realm_server::types::RealmChat;
@@ -21,56 +24,41 @@ async fn spawn(fut: impl Future<Output = ()> + Send + 'static) {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv().ok();
let db_pool = MySqlPoolOptions::new()
.max_connections(64)
.connect(env::var("DATABASE_URL").expect("DATABASE_URL must be set").as_str()).await?;
sqlx::query(
"CREATE DATABASE IF NOT EXISTS realmchat; USE realmchat;"
).fetch_one(&db_pool).await?;
sqlx::query(
"CREATE TABLE IF NOT EXISTS room (
id SERIAL,
room_id VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
admin_only_send BOOL NOT NULL,
admin_only_view BOOL NOT NULL
);"
).execute(&db_pool).await?;
let subscriber = tracing_subscriber::fmt()
.compact()
.with_file(true)
.with_line_number(true)
.with_thread_ids(true)
.with_target(false)
.finish();
sqlx::query(
"CREATE TABLE IF NOT EXISTS user (
id SERIAL,
user_id VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
online BOOL NOT NULL,
admin BOOL NOT NULL
);"
).execute(&db_pool).await?;
subscriber::set_global_default(subscriber).unwrap();
sqlx::query(
"CREATE TABLE IF NOT EXISTS message (
id SERIAL,
timestamp DATETIME NOT NULL,
user INT NOT NULL,
room INT NOT NULL,
type ENUM('text', 'attachment', 'reply', 'edit', 'reaction', 'redaction') NOT NULL,
let database_url: &str = &env::var("DATABASE_URL").expect("DATABASE_URL must be set");
msgText TEXT,
referencingID INT,
emoji TEXT,
redaction BOOL
);"
).execute(&db_pool).await?;
if !Sqlite::database_exists(database_url).await.unwrap_or(false) {
info!("Creating database {}", database_url);
match Sqlite::create_database(database_url).await {
Ok(_) => info!("Create db success"),
Err(error) => panic!("error: {}", error),
}
} else {
warn!("Database already exists");
} // TODO: Do in Docker with Sqlx-cli
let db_pool = SqlitePool::connect(database_url).await.unwrap();
info!("Running migrations...");
migrate!().run(&db_pool).await?; // TODO: Do in Docker with Sqlx-cli
info!("Migrations complete!");
let server_addr = (IpAddr::V6(Ipv6Addr::LOCALHOST), env::var("PORT").expect("PORT must be set").parse::<u16>().unwrap());
// JSON transport is provided by the json_transport tarpc module. It makes it easy
// to start up a serde-powered json serialization strategy over TCP.
let mut listener = tarpc::serde_transport::tcp::listen(&server_addr, Json::default).await?;
tracing::info!("Listening on port {}", listener.local_addr().port());
info!("Listening on port {}", listener.local_addr().port());
listener.config_mut().max_frame_length(usize::MAX);
listener
// Ignore accept errors.