start switch to sqlite
This commit is contained in:
@ -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] {
|
||||
[
|
||||
|
||||
2
src/docker/mod.rs
Normal file
2
src/docker/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
mod start;
|
||||
pub use start::start_server;
|
||||
3
src/docker/start.rs
Normal file
3
src/docker/start.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub async fn start_server() -> Result<String, String> {
|
||||
Ok("".to_string())
|
||||
}
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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<ConnectionManager<PgConnection>>;
|
||||
pub type DbPool = Pool<ConnectionManager<SqliteConnection>>;
|
||||
|
||||
fn get_connection_pool() -> DbPool {
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
|
||||
let manager = ConnectionManager::<PgConnection>::new(&database_url);
|
||||
let manager = ConnectionManager::<SqliteConnection>::new(&database_url);
|
||||
Pool::builder()
|
||||
.build(manager)
|
||||
.unwrap_or_else(|_| panic!("Error creating pool for {}", database_url))
|
||||
|
||||
@ -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<u8>, packet_id: i32) -> Result<(),String> {
|
||||
@ -53,7 +53,7 @@ impl Client {
|
||||
Ok(server) => {
|
||||
let login = match server.last_login {
|
||||
Some(date) => {
|
||||
let datetime: DateTime<Utc> = date.into();
|
||||
let datetime: DateTime<Utc> = Utc.from_utc_datetime(&date);
|
||||
|
||||
format!("{}", datetime.format("%d-%m-%Y at %H:%M").to_string())
|
||||
},
|
||||
|
||||
@ -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<SystemTime>,
|
||||
pub last_login: Option<NaiveDateTime>,
|
||||
pub container_id: Option<String>,
|
||||
pub status: ServerStatus,
|
||||
pub redirect_ip: Option<String>
|
||||
|
||||
@ -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<SmallInt, Pg> for ServerStatus {
|
||||
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
|
||||
impl ToSql<SmallInt, Sqlite> 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)
|
||||
<i16 as ToSql<SmallInt, Sqlite>>::to_sql(&val, out)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql<SmallInt, Pg> for ServerStatus {
|
||||
fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
|
||||
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<SmallInt, Sqlite> for ServerStatus {
|
||||
fn from_sql(value: diesel::sqlite::SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
|
||||
let val = value.as_i64() as i16;
|
||||
match val {
|
||||
0 => Ok(ServerStatus::Archived),
|
||||
1 => Ok(ServerStatus::Stopped),
|
||||
@ -49,13 +40,12 @@ impl FromSql<SmallInt, Pg> 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user