From 05c69d26eda4561266a17a90707dfe39b35f63ba Mon Sep 17 00:00:00 2001 From: tomoron Date: Sat, 6 Jun 2026 18:19:29 +0200 Subject: [PATCH] start switch to sqlite --- Cargo.lock | 86 +++++++++++++++++++++++++--------- Cargo.toml | 2 +- src/cli/env_settings.rs | 2 +- src/docker/mod.rs | 2 + src/docker/start.rs | 3 ++ src/main.rs | 8 ++-- src/minecraft/client/status.rs | 4 +- src/models.rs | 6 +-- src/status.rs | 32 +++++-------- 9 files changed, 92 insertions(+), 53 deletions(-) create mode 100644 src/docker/mod.rs create mode 100644 src/docker/start.rs diff --git a/Cargo.lock b/Cargo.lock index 9731542..95e1cc8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -302,12 +302,6 @@ version = "3.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb" -[[package]] -name = "byteorder" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" - [[package]] name = "bytes" version = "1.11.1" @@ -534,13 +528,13 @@ version = "2.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9940fb8467a0a06312218ed384185cb8536aa10d8ec017d0ce7fad2c1bd882d5" dependencies = [ - "bitflags", - "byteorder", + "chrono", "diesel_derives", "downcast-rs", - "itoa", - "pq-sys", + "libsqlite3-sys", "r2d2", + "sqlite-wasm-rs", + "time", ] [[package]] @@ -721,6 +715,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + [[package]] name = "form_urlencoded" version = "1.2.2" @@ -845,7 +845,16 @@ version = "0.15.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" dependencies = [ - "foldhash", + "foldhash 0.1.5", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "foldhash 0.2.0", ] [[package]] @@ -1233,6 +1242,16 @@ version = "0.2.186" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" +[[package]] +name = "libsqlite3-sys" +version = "0.37.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f111c8c41e7c61a49cd34e44c7619462967221a6443b0ec299e0ac30cfb9b1" +dependencies = [ + "pkg-config", + "vcpkg", +] + [[package]] name = "linux-raw-sys" version = "0.12.1" @@ -1435,17 +1454,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "efca4c95a19a79d1c98f791f10aebd5c1363b473244630bb7dbde1dc98455a24" -[[package]] -name = "pq-sys" -version = "0.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "574ddd6a267294433f140b02a726b0640c43cf7c6f717084684aaa3b285aba61" -dependencies = [ - "libc", - "pkg-config", - "vcpkg", -] - [[package]] name = "prettyplease" version = "0.2.37" @@ -1535,6 +1543,16 @@ version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" +[[package]] +name = "rsqlite-vfs" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c51c9ae4df8a7fba42103df5c621fa3c37eccf3a3c650879e90fc48b11cc192c" +dependencies = [ + "hashbrown 0.16.1", + "thiserror", +] + [[package]] name = "rustix" version = "1.1.4" @@ -1713,6 +1731,18 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "sqlite-wasm-rs" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc3efc0da82635d7e1ced0053bbbfa8c7ab9645d0bf36ceb4f7127bb85315d75" +dependencies = [ + "cc", + "js-sys", + "rsqlite-vfs", + "wasm-bindgen", +] + [[package]] name = "stable_deref_trait" version = "1.2.1" @@ -1785,11 +1815,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "743bd48c283afc0388f9b8827b976905fb217ad9e647fae3a379a9283c4def2c" dependencies = [ "deranged", + "itoa", "js-sys", "num-conv", "powerfmt", "serde_core", "time-core", + "time-macros", ] [[package]] @@ -1798,6 +1830,16 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7694e1cfe791f8d31026952abf09c69ca6f6fa4e1a1229e18988f06a04a12dca" +[[package]] +name = "time-macros" +version = "0.2.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e70e4c5a0e0a8a4823ad65dfe1a6930e4f4d756dcd9dd7939022b5e8c501215" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tinystr" version = "0.8.3" diff --git a/Cargo.toml b/Cargo.toml index b182b66..4d5f558 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ async-std = "1.13.2" bollard = "0.21.0" chrono = "0.4.44" clap = "4.6.1" -diesel = { version = "2.3.9", features = ["postgres", "r2d2"] } +diesel = { version = "2.3.9", features = ["sqlite", "r2d2", "chrono"] } diesel-enum = "0.2.1" json = "0.12.4" regex = "1.12.3" diff --git a/src/cli/env_settings.rs b/src/cli/env_settings.rs index 9c7e050..d53740c 100644 --- a/src/cli/env_settings.rs +++ b/src/cli/env_settings.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use clap::{ Arg, ArgAction, ArgMatches, parser::ValueSource}; +use clap::{ Arg, ArgMatches, parser::ValueSource}; pub fn get_args() -> [Arg; 21] { [ diff --git a/src/docker/mod.rs b/src/docker/mod.rs new file mode 100644 index 0000000..cbe2574 --- /dev/null +++ b/src/docker/mod.rs @@ -0,0 +1,2 @@ +mod start; +pub use start::start_server; diff --git a/src/docker/start.rs b/src/docker/start.rs new file mode 100644 index 0000000..1e52853 --- /dev/null +++ b/src/docker/start.rs @@ -0,0 +1,3 @@ +pub async fn start_server() -> Result { + Ok("".to_string()) +} diff --git a/src/main.rs b/src/main.rs index 7299915..3867735 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/05/29 21:22:17 by tomoron #+# #+# */ -/* Updated: 2026/06/06 15:37:52 by tomoron ### ########.fr */ +/* Updated: 2026/06/06 18:18:03 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,18 +22,20 @@ pub mod models; pub mod status; +pub mod docker; + mod cli; use cli::*; mod config; pub use config::Config; -pub type DbPool = Pool>; +pub type DbPool = Pool>; fn get_connection_pool() -> DbPool { let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); - let manager = ConnectionManager::::new(&database_url); + let manager = ConnectionManager::::new(&database_url); Pool::builder() .build(manager) .unwrap_or_else(|_| panic!("Error creating pool for {}", database_url)) diff --git a/src/minecraft/client/status.rs b/src/minecraft/client/status.rs index 9773310..9cad1fa 100644 --- a/src/minecraft/client/status.rs +++ b/src/minecraft/client/status.rs @@ -7,7 +7,7 @@ use json::object; use crate::minecraft::varint::varint_write; use std::collections::VecDeque; -use chrono::{DateTime, Utc}; +use chrono::{DateTime, TimeZone, Utc}; impl Client { pub async fn status_intent_handle(&self, packet: &mut VecDeque, packet_id: i32) -> Result<(),String> { @@ -53,7 +53,7 @@ impl Client { Ok(server) => { let login = match server.last_login { Some(date) => { - let datetime: DateTime = date.into(); + let datetime: DateTime = Utc.from_utc_datetime(&date); format!("{}", datetime.format("%d-%m-%Y at %H:%M").to_string()) }, diff --git a/src/models.rs b/src/models.rs index 0ea1a14..b9c537e 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,7 +1,7 @@ use std::fmt; use std::time::SystemTime; -use chrono::{DateTime, Utc}; +use chrono::{DateTime, NaiveDateTime, Utc}; use diesel::prelude::*; use serde::Deserialize; @@ -12,11 +12,11 @@ use crate::schema; #[derive(Queryable, Selectable, Debug)] #[diesel(table_name = schema::servers)] -#[diesel(check_for_backend(diesel::pg::Pg))] +#[diesel(check_for_backend(diesel::sqlite::Sqlite))] pub struct Servers { pub id: i64, pub name: String, - pub last_login: Option, + pub last_login: Option, pub container_id: Option, pub status: ServerStatus, pub redirect_ip: Option diff --git a/src/status.rs b/src/status.rs index 5673c04..535ba8d 100644 --- a/src/status.rs +++ b/src/status.rs @@ -1,14 +1,10 @@ use diesel::deserialize::{self, FromSql, FromSqlRow}; -use diesel::pg::PgValue; -use diesel::expression::AsExpression; -use diesel::pg::Pg; +use diesel::sqlite::Sqlite; use diesel::serialize::{self, ToSql, Output}; use diesel::sql_types::SmallInt; - +use diesel::expression::AsExpression; use std::fmt; -use std::io::Write; - #[derive(Debug, Clone, Copy, PartialEq, Eq, AsExpression, FromSqlRow, serde::Serialize, serde::Deserialize)] #[diesel(sql_type = SmallInt)] pub enum ServerStatus { @@ -17,24 +13,19 @@ pub enum ServerStatus { Stopping = 2, Starting = 3, Running = 4, - Unknown = 5 + Unknown = 5, } -impl ToSql for ServerStatus { - fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result { +impl ToSql for ServerStatus { + fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result { let val = *self as i16; - out.write_all(&val.to_be_bytes())?; - Ok(serialize::IsNull::No) + >::to_sql(&val, out) } } -impl FromSql for ServerStatus { - fn from_sql(value: PgValue<'_>) -> deserialize::Result { - let bytes = value.as_bytes(); - if bytes.len() != 2 { - return Err("Invalid length for i16".into()); - } - let val = i16::from_be_bytes([bytes[0], bytes[1]]); +impl FromSql for ServerStatus { + fn from_sql(value: diesel::sqlite::SqliteValue<'_, '_, '_>) -> deserialize::Result { + let val = value.as_i64() as i16; match val { 0 => Ok(ServerStatus::Archived), 1 => Ok(ServerStatus::Stopped), @@ -49,13 +40,12 @@ impl FromSql for ServerStatus { impl fmt::Display for ServerStatus { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let str_status = match self { - ServerStatus::Archived => "archieved", + ServerStatus::Archived => "archived", ServerStatus::Stopped => "stopped", ServerStatus::Stopping => "stopping", ServerStatus::Starting => "starting", ServerStatus::Running => "running", - ServerStatus::Unknown => "(error : unknown status)" - + ServerStatus::Unknown => "(error : unknown status)", }; write!(f, "{}", str_status) }