storage module init

This commit is contained in:
Nikita Vilunov 2023-07-07 15:09:24 +02:00
parent 1a43a3c2d7
commit 9f0bcb9279
8 changed files with 936 additions and 2 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target /target
/db.sqlite

882
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -22,6 +22,7 @@ rustls-pemfile = "1.0.2"
quick-xml = { version = "0.30.0", features = ["async-tokio"] } quick-xml = { version = "0.30.0", features = ["async-tokio"] }
derive_more = "0.99.17" derive_more = "0.99.17"
uuid = { version = "1.3.0", features = ["v4"] } uuid = { version = "1.3.0", features = ["v4"] }
sqlx = { version = "0.7.0-alpha.2", features = ["sqlite", "runtime-tokio-rustls", "migrate"] }
[dev-dependencies] [dev-dependencies]
assert_matches = "1.5.0" assert_matches = "1.5.0"

View File

@ -9,3 +9,6 @@ server_name = "irc.localhost"
listen_on = "127.0.0.1:5222" listen_on = "127.0.0.1:5222"
cert = "./certs/xmpp.pem" cert = "./certs/xmpp.pem"
key = "./certs/xmpp.key" key = "./certs/xmpp.key"
[storage]
db_path = "db.sqlite"

15
migrations/0_first.sql Normal file
View File

@ -0,0 +1,15 @@
create table users(
id integer primary key autoincrement not null,
name string unique not null
);
-- for development only, replace with properly hashed passwords later
create table challenges_plain_password(
user_id integer primary key not null,
password string not null
);
create table rooms(
id integer primary key autoincrement not null,
name string unique not null
);

View File

@ -1,3 +1,4 @@
//! Domain definitions and implementation of common chat logic. //! Domain definitions and implementation of common chat logic.
pub mod player; pub mod player;
pub mod repo;
pub mod room; pub mod room;

31
src/core/repo/mod.rs Normal file
View File

@ -0,0 +1,31 @@
//! Storage and persistence logic.
use std::str::FromStr;
use serde::Deserialize;
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::{ConnectOptions, SqliteConnection};
use crate::prelude::*;
#[derive(Deserialize, Debug, Clone)]
pub struct StorageConfig {
pub db_path: String,
}
pub struct Storage {
conn: SqliteConnection,
}
impl Storage {
pub async fn open(config: StorageConfig) -> Result<Storage> {
let opts = SqliteConnectOptions::from_str(&*config.db_path)?.create_if_missing(true);
let mut conn = opts.connect().await?;
let migrator = sqlx::migrate!();
migrator.run(&mut conn).await?;
log::info!("Migrations passed");
Ok(Storage { conn })
}
}

View File

@ -19,6 +19,7 @@ use prometheus::Registry as MetricsRegistry;
use serde::Deserialize; use serde::Deserialize;
use crate::core::player::PlayerRegistry; use crate::core::player::PlayerRegistry;
use crate::core::repo::Storage;
use crate::core::room::RoomRegistry; use crate::core::room::RoomRegistry;
use crate::prelude::*; use crate::prelude::*;
@ -27,6 +28,7 @@ struct ServerConfig {
telemetry: util::telemetry::ServerConfig, telemetry: util::telemetry::ServerConfig,
irc: projections::irc::ServerConfig, irc: projections::irc::ServerConfig,
xmpp: projections::xmpp::ServerConfig, xmpp: projections::xmpp::ServerConfig,
storage: core::repo::StorageConfig,
} }
fn load_config() -> Result<ServerConfig> { fn load_config() -> Result<ServerConfig> {
@ -47,8 +49,10 @@ async fn main() -> Result<()> {
telemetry: telemetry_config, telemetry: telemetry_config,
irc: irc_config, irc: irc_config,
xmpp: xmpp_config, xmpp: xmpp_config,
storage: storage_config,
} = config; } = config;
let mut metrics = MetricsRegistry::new(); let mut metrics = MetricsRegistry::new();
let storage = Storage::open(storage_config).await?;
let rooms = RoomRegistry::empty(&mut metrics)?; let rooms = RoomRegistry::empty(&mut metrics)?;
let players = PlayerRegistry::empty(rooms.clone(), &mut metrics)?; let players = PlayerRegistry::empty(rooms.clone(), &mut metrics)?;
let telemetry_terminator = let telemetry_terminator =