start diesel setup
This commit is contained in:
18
src/lib.rs
18
src/lib.rs
@ -1,6 +1,20 @@
|
||||
use bollard::Docker;
|
||||
|
||||
|
||||
use std::env;
|
||||
|
||||
use std::sync::LazyLock;
|
||||
|
||||
pub static mut DOCKER: LazyLock<Docker> = LazyLock::new(|| Docker::connect_with_local_defaults().expect("Failed to connect to the docker socket") );
|
||||
|
||||
pub static mut DB_CONN: LazyLock<
|
||||
use diesel::PgConnection;
|
||||
use diesel::r2d2::{
|
||||
Pool,
|
||||
ConnectionManager
|
||||
};
|
||||
|
||||
|
||||
pub mod schema;
|
||||
|
||||
|
||||
pub static mut DOCKER: LazyLock<Docker> = LazyLock::new(|| Docker::connect_with_local_defaults().expect("Failed to connect to the docker socket") );
|
||||
|
||||
|
||||
64
src/main.rs
64
src/main.rs
@ -1,25 +1,69 @@
|
||||
//use tokio::net::TcpListener;
|
||||
use std::io;
|
||||
|
||||
use bollard::{
|
||||
query_parameters::{
|
||||
ListImagesOptionsBuilder,
|
||||
ListContainersOptionsBuilder,
|
||||
CreateContainerOptionsBuilder
|
||||
},
|
||||
models::{
|
||||
ContainerCreateBody
|
||||
},
|
||||
Docker
|
||||
};
|
||||
|
||||
//mod minecraft;
|
||||
//use minecraft::process_mc_socket;
|
||||
|
||||
//mod rpc;
|
||||
//use rpc::process_rpc_socket;
|
||||
//
|
||||
|
||||
|
||||
use diesel::{prelude::*, r2d2::{ConnectionManager, Pool}};
|
||||
|
||||
mod models;
|
||||
|
||||
use dockermcmgr::schema;
|
||||
|
||||
use crate::models::{ Servers, CreateServer };
|
||||
|
||||
use std::env;
|
||||
|
||||
pub mod status;
|
||||
|
||||
use status::ServerStatus;
|
||||
|
||||
|
||||
|
||||
pub mod server;
|
||||
use async_std::sync::{Mutex, Arc};
|
||||
use server::Server;
|
||||
use server::ServerStatus;
|
||||
|
||||
mod serverList;
|
||||
fn get_connection_pool() -> Pool<ConnectionManager<PgConnection>> {
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
|
||||
let manager = ConnectionManager::<PgConnection>::new(&database_url);
|
||||
Pool::builder()
|
||||
.build(manager)
|
||||
.unwrap_or_else(|_| panic!("Error creating pool for {}", database_url))
|
||||
}
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() -> io::Result<()> {
|
||||
// let conn = DB_CONN.get().expect("failed to get the connection from the pool");
|
||||
let pool = get_connection_pool();
|
||||
|
||||
let conn = &mut pool.get().unwrap();
|
||||
|
||||
let servers = schema::servers::table.select(Servers::as_select()).load(conn);
|
||||
println!("servers : {:?}", servers);
|
||||
|
||||
let new_server = CreateServer {
|
||||
name: "potato",
|
||||
volume_path: "potato2",
|
||||
last_login: None,
|
||||
container_id: None,
|
||||
status: ServerStatus::Stopped,
|
||||
redirect_ip: None
|
||||
};
|
||||
|
||||
Ok(())
|
||||
/*
|
||||
println!("{:?}", docker);
|
||||
@ -50,12 +94,6 @@ async fn main() -> io::Result<()> {
|
||||
|
||||
let start_res = docker.start_container("rust-created-container", None).await;
|
||||
*/
|
||||
// let docker_conn = bollard::Docker::connect_with_local_defaults().expect("Failed to connect to the docker socket");
|
||||
// *crate::server::DOCKER.lock().unwrap() = Some(docker_conn);
|
||||
// let srv = Server { container_name: "something".to_string(), server_name: "potato".to_string(), server_id: 1, status: ServerStatus::Running };
|
||||
// srv.start();
|
||||
//
|
||||
// Ok(())
|
||||
/*
|
||||
let mc_listener = TcpListener::bind("0.0.0.0:25565").await?;
|
||||
let rpc_listener = TcpListener::bind("0.0.0.0:8080").await?;
|
||||
|
||||
33
src/models.rs
Normal file
33
src/models.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use std::time::SystemTime;
|
||||
|
||||
use diesel::prelude::*;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::status::ServerStatus;
|
||||
|
||||
|
||||
#[derive(Queryable, Selectable, Debug)]
|
||||
#[diesel(table_name = dockermcmgr::schema::servers)]
|
||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||
pub struct Servers {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub volume_path: String,
|
||||
pub last_login: Option<SystemTime>,
|
||||
pub container_id: Option<String>,
|
||||
pub status: i16,
|
||||
pub redirect_ip: Option<i32>
|
||||
}
|
||||
|
||||
|
||||
#[derive(Deserialize, Insertable)]
|
||||
#[diesel(table_name = dockermcmgr::schema::servers)]
|
||||
pub struct CreateServer<'a> {
|
||||
pub name: &'a str,
|
||||
pub volume_path: &'a str,
|
||||
pub last_login: Option<SystemTime>,
|
||||
pub container_id: Option<&'a str>,
|
||||
pub status: ServerStatus,
|
||||
pub redirect_ip: Option<i32>
|
||||
}
|
||||
16
src/schema.rs
Normal file
16
src/schema.rs
Normal file
@ -0,0 +1,16 @@
|
||||
// @generated automatically by Diesel CLI.
|
||||
|
||||
diesel::table! {
|
||||
servers (id) {
|
||||
id -> Int4,
|
||||
#[max_length = 255]
|
||||
name -> Varchar,
|
||||
#[max_length = 255]
|
||||
volume_path -> Varchar,
|
||||
last_login -> Nullable<Timestamp>,
|
||||
#[max_length = 30]
|
||||
container_id -> Nullable<Varchar>,
|
||||
status -> Int2,
|
||||
redirect_ip -> Nullable<Int4>,
|
||||
}
|
||||
}
|
||||
@ -1,6 +0,0 @@
|
||||
mod server;
|
||||
pub use server::Server;
|
||||
|
||||
mod status;
|
||||
pub use status::ServerStatus;
|
||||
|
||||
@ -1,48 +0,0 @@
|
||||
use bollard::{
|
||||
query_parameters::{
|
||||
ListImagesOptionsBuilder,
|
||||
ListContainersOptionsBuilder,
|
||||
CreateContainerOptionsBuilder
|
||||
},
|
||||
models::{
|
||||
ContainerCreateBody
|
||||
},
|
||||
Docker
|
||||
};
|
||||
|
||||
use crate::server::ServerStatus;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Server {
|
||||
pub container_name: String,
|
||||
pub server_name: String,
|
||||
pub server_id: u64,
|
||||
|
||||
pub status: ServerStatus,
|
||||
pub docker: Docker
|
||||
}
|
||||
|
||||
impl Server {
|
||||
pub async fn new(name: String, id: u64) -> Self {
|
||||
let docker = Docker::connect_with_local_defaults().expect("Failed to connect to the docker socket");
|
||||
Self {
|
||||
|
||||
container_name: "idk".to_string(),
|
||||
server_name: name,
|
||||
server_id: id,
|
||||
|
||||
status: ServerStatus::Stopped,
|
||||
docker
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub async fn start(&self) -> Result<(), String> {
|
||||
let options = ListImagesOptionsBuilder::default()
|
||||
.all(true)
|
||||
.build();
|
||||
let images = self.docker.list_images(Some(options)).await.unwrap();
|
||||
println!("\n\nimages : {:?}", images);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ServerStatus {
|
||||
Running,
|
||||
Starting,
|
||||
Stopped,
|
||||
Archived
|
||||
}
|
||||
@ -1,2 +0,0 @@
|
||||
mod serverList;
|
||||
pub use serverList::*;
|
||||
@ -1,17 +0,0 @@
|
||||
use crate::server::Server;
|
||||
use async_std::sync::{Mutex, Arc};
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub static SERVER_LIST: LazyLock<Mutex<HashMap<u64, Arc<Mutex<Server>>>>> = LazyLock::new(|| Mutex::new(HashMap::new()));
|
||||
|
||||
pub async fn get(id: u64) -> Option<Arc<Mutex<Server>>> {
|
||||
let server_list = SERVER_LIST.lock().await;
|
||||
match server_list.get(&id) {
|
||||
Some(found) => { Some(found.clone()) },
|
||||
None => { None }
|
||||
}
|
||||
}
|
||||
|
||||
// (*SERVER_LIST.lock().await).push(Arc::new(Mutex::new(Server::new("test".to_string(), 1u64).await)));
|
||||
46
src/status.rs
Normal file
46
src/status.rs
Normal file
@ -0,0 +1,46 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, diesel::expression::AsExpression, diesel::deserialize::FromSqlRow)]
|
||||
#[diesel(sql_type = diesel::sql_types::SmallInt)]
|
||||
pub enum ServerStatus {
|
||||
Archived,
|
||||
Stopped,
|
||||
Stopping,
|
||||
Starting,
|
||||
Running,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl<DB: diesel::backend::Backend> diesel::serialize::ToSql<diesel::sql_types::SmallInt, DB> for ServerStatus
|
||||
where
|
||||
<DB as diesel::backend::Backend>::RawValue: for<'a> diesel::serialize::ToSql<diesel::sql_types::SmallInt, DB<'a>> + ?Sized,
|
||||
{
|
||||
fn to_sql<'b>(&'b self, out: &mut diesel::serialize::Output<'b, '_, DB>) -> diesel::serialize::Result<diesel::serialize::IsNull> {
|
||||
let val = match self {
|
||||
ServerStatus::Archived => 0i16,
|
||||
ServerStatus::Stopped => 1i16,
|
||||
ServerStatus::Stopping => 2i16,
|
||||
ServerStatus::Starting => 3i16,
|
||||
ServerStatus::Running => 4i16,
|
||||
ServerStatus::Unknown => -1i16,
|
||||
};
|
||||
diesel::serialize::ToSql::<diesel::sql_types::SmallInt, DB>::to_sql(&val, out)
|
||||
}
|
||||
}
|
||||
|
||||
impl<DB: diesel::backend::Backend> diesel::deserialize::FromSql<diesel::sql_types::SmallInt, DB> for ServerStatus
|
||||
where
|
||||
<DB as diesel::backend::Backend>::RawValue: for<'a> diesel::deserialize::FromSql<'a, diesel::sql_types::SmallInt, DB> + ?Sized,
|
||||
{
|
||||
fn from_sql(bytes: <DB as diesel::backend::Backend>::RawValue<'_>) -> diesel::deserialize::Result<Self> {
|
||||
let val = i16::from_sql(bytes)?;
|
||||
match val {
|
||||
0 => Ok(ServerStatus::Archived),
|
||||
1 => Ok(ServerStatus::Stopped),
|
||||
2 => Ok(ServerStatus::Stopping),
|
||||
3 => Ok(ServerStatus::Starting),
|
||||
4 => Ok(ServerStatus::Running),
|
||||
_ => Ok(ServerStatus::Unknown),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user