remove rpc, cli start
This commit is contained in:
12
src/cli/add.rs
Normal file
12
src/cli/add.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use clap::{ArgMatches, Command, arg};
|
||||
|
||||
pub fn create_subcommand() -> Command {
|
||||
Command::new("add")
|
||||
.about("add a server")
|
||||
.arg(arg!(<NAME> "Name of the server"))
|
||||
.arg(arg!(-v --version <VERSION> "minecraft version of the server"))
|
||||
}
|
||||
|
||||
pub fn subcommand(arg: &ArgMatches) {
|
||||
println!("got add, sub : {:?}", arg);
|
||||
}
|
||||
6
src/cli/edit.rs
Normal file
6
src/cli/edit.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use clap::{Command, arg};
|
||||
|
||||
pub fn create_subcommand() -> Command {
|
||||
Command::new("edit")
|
||||
.about("edit a server")
|
||||
}
|
||||
6
src/cli/ls.rs
Normal file
6
src/cli/ls.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use clap::{Command, arg};
|
||||
|
||||
pub fn create_subcommand() -> Command {
|
||||
Command::new("ls")
|
||||
.about("list the servers")
|
||||
}
|
||||
4
src/cli/mod.rs
Normal file
4
src/cli/mod.rs
Normal file
@ -0,0 +1,4 @@
|
||||
pub mod add;
|
||||
pub mod edit;
|
||||
pub mod remove;
|
||||
pub mod ls;
|
||||
6
src/cli/remove.rs
Normal file
6
src/cli/remove.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use clap::{Command, arg};
|
||||
|
||||
pub fn create_subcommand() -> Command {
|
||||
Command::new("remove")
|
||||
.about("remove a server")
|
||||
}
|
||||
131
src/main.rs
131
src/main.rs
@ -6,11 +6,10 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/05/29 21:22:17 by tomoron #+# #+# */
|
||||
/* Updated: 2026/05/31 15:02:01 by tomoron ### ########.fr */
|
||||
/* Updated: 2026/06/04 12:37:14 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::io;
|
||||
//use bollard::{
|
||||
// query_parameters::{
|
||||
@ -24,21 +23,13 @@ use tokio::io;
|
||||
// Docker
|
||||
//};
|
||||
|
||||
|
||||
|
||||
mod minecraft;
|
||||
use minecraft::process_mc_socket;
|
||||
|
||||
mod rpc;
|
||||
use rpc::process_rpc_socket;
|
||||
|
||||
use minecraft::mc_socket_listen;
|
||||
|
||||
use diesel::{prelude::*, r2d2::{ConnectionManager, Pool}};
|
||||
|
||||
//mod models;
|
||||
|
||||
//use dockermcmgr::schema;
|
||||
|
||||
//use crate::models::{ Servers, CreateServer };
|
||||
|
||||
use std::env;
|
||||
|
||||
pub mod schema;
|
||||
@ -46,8 +37,9 @@ pub mod models;
|
||||
|
||||
pub mod status;
|
||||
|
||||
use status::ServerStatus;
|
||||
use crate::models::Servers;
|
||||
mod cli;
|
||||
|
||||
use cli::*;
|
||||
|
||||
|
||||
pub type DbPool = Pool<ConnectionManager<PgConnection>>;
|
||||
@ -61,69 +53,70 @@ fn get_connection_pool() -> DbPool {
|
||||
.unwrap_or_else(|_| panic!("Error creating pool for {}", database_url))
|
||||
}
|
||||
|
||||
|
||||
use clap::{Command, arg};
|
||||
|
||||
|
||||
fn cli() -> Command {
|
||||
Command::new("dmm")
|
||||
.about("Docker Minecraft server Manager cli")
|
||||
.subcommand_required(true)
|
||||
.arg_required_else_help(true)
|
||||
.subcommand(
|
||||
Command::new("serverMode")
|
||||
.about("start the server manager")
|
||||
|
||||
)
|
||||
.subcommand(add::create_subcommand())
|
||||
.subcommand(remove::create_subcommand())
|
||||
.subcommand(edit::create_subcommand())
|
||||
.subcommand(ls::create_subcommand())
|
||||
}
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() -> io::Result<()> {
|
||||
|
||||
let arg = cli().get_matches();
|
||||
let pool = get_connection_pool();
|
||||
|
||||
let mc_listener = TcpListener::bind("0.0.0.0:25565").await?;
|
||||
let rpc_listener = TcpListener::bind("0.0.0.0:8080").await?;
|
||||
|
||||
// let servers = schema::servers::table.select(Servers::as_select()).load(conn).unwrap();
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
Ok((socket, _)) = mc_listener.accept() => {
|
||||
let cloned = pool.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = process_mc_socket(socket, cloned).await {
|
||||
eprintln!("mc error: {:?}", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Ok((socket, _)) = rpc_listener.accept() => {
|
||||
let cloned = pool.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = process_rpc_socket(socket, cloned).await {
|
||||
eprintln!("rpc error: {:?}", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
match arg.subcommand() {
|
||||
Some(("serverMode", _)) => { mc_socket_listen(pool).await?; },
|
||||
Some(("add", sub_matches)) => { add::subcommand(sub_matches); },
|
||||
_ => {panic!("subcommand not implemented")}
|
||||
}
|
||||
/*
|
||||
|
||||
let conn = &mut pool.get().unwrap();
|
||||
//
|
||||
|
||||
|
||||
let new_server = CreateServer {
|
||||
name: "potato",
|
||||
volume_path: "potato2",
|
||||
last_login: None,
|
||||
container_id: None,
|
||||
status: ServerStatus::Stopped,
|
||||
redirect_ip: None
|
||||
};
|
||||
|
||||
match diesel::insert_into(schema::servers::table)
|
||||
.values(&new_server)
|
||||
.execute(conn) {
|
||||
Ok(a) => { println!("{:?}", a); },
|
||||
Err(reason) => {println!("got an error : {:?}", reason) }
|
||||
}
|
||||
|
||||
let start = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
|
||||
let servers = schema::servers::table.select(Servers::as_select()).load(conn).unwrap();
|
||||
let end = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
|
||||
println!("servers : {:?}", servers);
|
||||
println!("retreived in {:?}", end - start);
|
||||
|
||||
|
||||
println!("server id : {}",servers[0].id);
|
||||
|
||||
Ok(())
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
println!("{:?}", docker);
|
||||
let options = ListImagesOptionsBuilder::default()
|
||||
@ -155,4 +148,4 @@ async fn main() -> io::Result<()> {
|
||||
*/
|
||||
/*
|
||||
*/
|
||||
}
|
||||
//}
|
||||
|
||||
@ -4,4 +4,4 @@ mod client;
|
||||
|
||||
mod socket;
|
||||
|
||||
pub use socket::process_mc_socket;
|
||||
pub use socket::mc_socket_listen;
|
||||
|
||||
@ -3,7 +3,25 @@ use tokio::net::TcpStream;
|
||||
|
||||
use crate::DbPool;
|
||||
|
||||
pub async fn process_mc_socket(stream: TcpStream, pool: DbPool) -> Result<(), String> {
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
use std::io;
|
||||
|
||||
pub async fn mc_socket_listen(pool: DbPool) -> io::Result<()> {
|
||||
let mc_listener = TcpListener::bind("0.0.0.0:25565").await?;
|
||||
loop {
|
||||
if let Ok((socket, _)) = mc_listener.accept().await {
|
||||
let cloned = pool.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = process_mc_socket(socket, cloned).await {
|
||||
eprintln!("mc error: {:?}", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn process_mc_socket(stream: TcpStream, pool: DbPool) -> Result<(), String> {
|
||||
println!("new client {:?}", stream);
|
||||
|
||||
let mut client = Client::create(stream, pool);
|
||||
|
||||
@ -1,2 +0,0 @@
|
||||
mod ping;
|
||||
//pub use ping::ping;
|
||||
@ -1,4 +0,0 @@
|
||||
|
||||
//pub fn ping(data: json::object) -> json::object {
|
||||
//
|
||||
//}
|
||||
@ -1,6 +0,0 @@
|
||||
mod socket;
|
||||
|
||||
pub use socket::process_rpc_socket;
|
||||
|
||||
pub mod actions;
|
||||
pub mod rpc_client;
|
||||
@ -1,17 +0,0 @@
|
||||
pub struct RpcClient{
|
||||
pub read_buffer: Vec<u8>,
|
||||
pub buffer: Vec<u8>,
|
||||
|
||||
pub close: bool
|
||||
}
|
||||
|
||||
impl RpcClient {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
read_buffer: vec![0; 1024],
|
||||
buffer: vec![0; 10240],
|
||||
|
||||
close: true
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
use std::io;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
use crate::DbPool;
|
||||
|
||||
use crate::rpc::rpc_client::RpcClient;
|
||||
|
||||
|
||||
/*
|
||||
* request_format :
|
||||
* - "action" : 1 byte
|
||||
* - length : 4 bytes (no more than 1MB)
|
||||
* - json object : `length` bytes
|
||||
* containing the parameters for this specific action (will vary
|
||||
* depending on the action)
|
||||
*
|
||||
* response format :
|
||||
* - response packet : may be more than one depending on the action
|
||||
* - length: 4 bytes (no more than 1MB)
|
||||
* - data: json object containing the response data
|
||||
*/
|
||||
|
||||
pub async fn process_rpc_socket(_stream: TcpStream, _pool: DbPool) -> io::Result<()> {
|
||||
let client = RpcClient::new();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user