Skip to content

Commit

Permalink
Added capability to host multiple servers
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEmeraldBee committed Mar 20, 2024
1 parent 86b79ed commit 7669111
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
19 changes: 18 additions & 1 deletion dist/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,27 @@ <h2>Welcome {{ user }}!</h2>
<p>{{ status }}</p>
<div class="button-container">
<a class="cool-link" href="/logout"><button class="cool-button">Logout</button></a>
<a class="cool-link" href="/action/boot"><button class="cool-button">Start Server</button></a>

<input placeholder="What server ID do you want to boot?" id="server">
<button class="cool-button" onclick="boot()">Start Server</button>
{{ extra }}
</div>
</div>
</body>

<script>
function boot() {
let data = document.getElementById("server");

document.getElementById("server").value = ""

fetch('/action/boot/' + data, {
method: "GET",
headers: {'Content-Type': 'application/json'},
}).then(res => {
console.log("Request complete! response:", res);
});
}
</script>

</html>
9 changes: 7 additions & 2 deletions roast-options.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ admin_pass = "pass"
main_user = "User"
main_pass = "pass"

# File that runs your server
run_path = "echo \"Hello World\""
[[runnables]]
name = "Numbers"
path = "./run.sh"

[[runnables]]
name = "Numbers 2"
path = "./run.sh"
2 changes: 2 additions & 0 deletions src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::blocks_in_conditions)]

use std::{collections::HashMap, sync::Mutex};

use lazy_static::lazy_static;
Expand Down
24 changes: 24 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
use serde::Deserialize;

#[derive(Deserialize)]
pub struct RunPath {
pub name: String,
pub path: String,
}

#[derive(Deserialize)]
pub struct Config {
pub address: String,
pub port: u16,

pub runnables: Vec<RunPath>,

pub admin_user: String,
pub admin_pass: String,

pub main_user: String,
pub main_pass: String,
}

/*
#[derive(Deserialize)]
pub struct Config {
pub address: String,
Expand All @@ -13,3 +35,5 @@ pub struct Config {
pub main_user: String,
pub main_pass: String,
}
*/
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ lazy_static! {
static ref COMMAND: RwLock<ServerCommand> = {
let command = ServerCommand::new(
Command::new("sh")
.arg(CONFIG.run_path.clone())
.arg(CONFIG.runnables[0].path.clone())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
Expand Down
14 changes: 11 additions & 3 deletions src/website/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub async fn command(command: Json<CommandForm>, cookies: &CookieJar<'_>) -> &'s
"Complete"
}

#[get("/boot")]
pub fn boot(cookies: &CookieJar<'_>) -> Redirect {
#[get("/boot/<com>")]
pub fn boot(cookies: &CookieJar<'_>, com: String) -> Redirect {
if logged_in(cookies).is_none() {
return Redirect::to(uri!("/"));
}
Expand All @@ -41,8 +41,16 @@ pub fn boot(cookies: &CookieJar<'_>) -> Redirect {
if command.running {
Redirect::to(uri!("/user"))
} else {
let path = match CONFIG.runnables.iter().find(|x| x.name == com) {
Some(s) => s,
None => &CONFIG.runnables[0],
}
.path
.clone();

*command = ServerCommand::new(
Command::new(CONFIG.run_path.clone())
Command::new("sh")
.arg(path)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
Expand Down

0 comments on commit 7669111

Please sign in to comment.