diff --git a/core/globals.ts b/core/globals.ts index 7e9f2c2..646c532 100644 --- a/core/globals.ts +++ b/core/globals.ts @@ -1,11 +1,12 @@ import type {MinecraftServer} from "~/types/MinecraftServer"; import type {SettingsJsonFile} from "~/types/SettingsJsonFile"; +import * as os from 'os'; import {saveJsonFile} from "~/util/jsonLoader"; export namespace environment{ export namespace paths{ - export let data: string = "/home/weexnes/.mcservermanager"; - export let servers: string = "/home/weexnes/.mcservermanager/servers"; + export let data: string = `${os.homedir()}/.mcservermanager`; + export let servers: string = `${os.homedir()}/.mcservermanager/servers`; } diff --git a/package.json b/package.json index f4ba6af..a5cf7eb 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "nuxt": "^3.15.4", "vue": "latest", "vue-router": "latest", - "execa": "^9.5.2" + "execa": "^9.5.2", + "ini": "^5.0.0" } } diff --git a/pages/index.vue b/pages/index.vue index e75050d..41f76c5 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -2,16 +2,21 @@

Minecraft Servers

-
-

+
+

{{ server.name }}

Version: {{ server.version }}

Port {{ server.port }}

-

Slots: {{ server.maxPlayers }}

-

Max Memory: {{ server.maxMemory }}

+

Memory: {{ server.minMemory }}-{{ server.maxMemory }}GB

+ +
+ + +
+
@@ -41,12 +46,6 @@
- - - -

- + + + + + + + +
+ @@ -139,6 +112,10 @@ import type {MinecraftServer} from "~/types/MinecraftServer"; import axios from 'axios'; +const testrec = reactive({ + logs: [] as string[], +}) + const settings = reactive({ servers: [] as MinecraftServer[], }) @@ -164,16 +141,41 @@ const getServers = async () => { } + +const getLogs = async () => { + // @ts-ignore + if(!editServerModal.checked) + return + + try{ + const response = await axios.post('/api/getLogs', { + name: editDialog.editedServer.name + }); + //console.log(response) + const strRet: string = response.data.logs; + const stringArray = strRet.split('\n'); + //console.log(stringArray); + testrec.logs.length = 0; + stringArray.slice(-20).forEach(element => { + testrec.logs.push(element); + }) + }catch(error){ + console.error(`Error fetch: ${error}`); + } +} + + const editServerDialog = async (server: MinecraftServer) => { editDialog.editedServer = server + // @ts-ignore - editServerModal.showModal() + //editServerModal.showModal() + editServerModal.checked = true } const addServer = async () => { if(!settings.servers.some(server => server.name === addDialog.newServer.name)){ - addDialog.newServer.logs = ["Server Created via Webui"] settings.servers.push(addDialog.newServer) await syncServers() } @@ -185,9 +187,7 @@ const deleteServer = async () => { await syncServers() } -const setNewServerDifficulty = async (newDiff: "peaceful" | "easy" | "normal" | "hard") => { - addDialog.newServer.difficulty = newDiff; -} + const syncServers = async () => { try { @@ -201,10 +201,21 @@ const syncServers = async () => { }; -const startServer = async () => { +const startServer = async (server: MinecraftServer) => { try { const response = await axios.post('/api/startServer', { - name: editDialog.editedServer.name + name: server.name + }); + console.log(response.data); + } catch (error) { + console.error(`Error `, error); + } +}; + +const stopServer = async (server: MinecraftServer) => { + try { + const response = await axios.post('/api/stopServer', { + name: server.name }); console.log(response.data); } catch (error) { @@ -214,12 +225,19 @@ const startServer = async () => { +let intervalId: NodeJS.Timeout; onMounted(async()=>{ await getServers() + intervalId = setInterval(getLogs, 500); }) +onBeforeUnmount(()=>{ + clearInterval(intervalId); +}) + + diff --git a/server/api/getLogs.ts b/server/api/getLogs.ts new file mode 100644 index 0000000..decbb40 --- /dev/null +++ b/server/api/getLogs.ts @@ -0,0 +1,33 @@ +import {environment} from "~/core/globals"; +import { defineEventHandler, getCookie, createError } from 'h3'; +import {saveJsonFile} from "~/util/jsonLoader"; +import {MinecraftServer} from "~/types/MinecraftServer"; +import {execa} from "execa"; +import * as fs from 'fs/promises'; + + +export default defineEventHandler(async (event) => { + const body = await readBody(event); + const server_name: string = body.name + + const server = environment.settings.servers.find(server => server.name == server_name); + + if(!server) { + return { message: "Server with name " + server_name + " does not exist" }; + } + + + const workingDir = environment.paths.servers + "/" + server.name; + const logsPath = environment.paths.servers + "/" + server.name + "/logs/latest.log"; + + try { + + const data = await fs.readFile(logsPath, 'utf-8'); + return { + message: `Logs for ${server_name}`, + logs: data + }; + } catch (error: any) { + return { message: "Failed to get logs for " + server_name, error: error.message }; + } +}); diff --git a/server/api/startServer.ts b/server/api/startServer.ts index c42077e..5f0b70c 100644 --- a/server/api/startServer.ts +++ b/server/api/startServer.ts @@ -14,6 +14,11 @@ export default defineEventHandler(async (event) => { return { message: "Server with name " + server_name + " does not exist" }; } + if (server.process) { + return { message: "Minecraft server is already running." }; + } + + const workingDir = environment.paths.servers + "/" + server.name; const jarPath = environment.paths.servers + "/" + server.name + "/server.jar"; @@ -33,9 +38,10 @@ export default defineEventHandler(async (event) => { }); server.process.on('exit', (code: string) => { - console.log(`Minecraft server process exited with code ${code}`); + console.log(`Server exit signal from startServer with code ${code}`); server.process = null; - }); + }) + return { message: "Minecraft server started." }; } catch (error: any) { diff --git a/server/api/stopServer.ts b/server/api/stopServer.ts new file mode 100644 index 0000000..3833383 --- /dev/null +++ b/server/api/stopServer.ts @@ -0,0 +1,38 @@ +import { environment } from "~/core/globals"; +import { execa } from "execa"; + +export default defineEventHandler(async (event) => { + const body = await readBody(event); + const server_name: string = body.name; + + const server = environment.settings.servers.find(server => server.name == server_name); + + if (!server) { + return { message: "Server with name " + server_name + " does not exist" }; + } + + + if (!server.process) { + return { message: "Minecraft server is not running." }; + } + + console.log(server.process) + + console.log("currently contains: " + server.process) + + try { + // Send "stop" command to the server process stdin + server.process.stdin.write('stop\n'); + + // Optionally, listen for the process exit or cleanup + server.process.on('exit', (code: string) => { + console.log(`Server exit signal from stopServer with code ${code}`); + server.process = null; + }); + + + return { message: "Minecraft server is stopping." }; + } catch (error: any) { + return { message: "Failed to stop Minecraft server.", error: error.message }; + } +}); diff --git a/types/MinecraftServer.ts b/types/MinecraftServer.ts index 423143f..af20863 100644 --- a/types/MinecraftServer.ts +++ b/types/MinecraftServer.ts @@ -3,25 +3,12 @@ import type {ResultPromise} from "execa"; export interface MinecraftServer { name: string; port: number; - maxPlayers: number; version: string; jar_url: string; - isRunning: boolean; - currentPlayers: number; - - - difficulty: "peaceful" | "easy" | "normal" | "hard"; - spawnProtection: number; - viewDistance: number; - - minMemory: number; maxMemory: number; - logs: string[]; - - process: any; } diff --git a/util/jsonLoader.ts b/util/jsonLoader.ts index 6c85687..b45acc6 100644 --- a/util/jsonLoader.ts +++ b/util/jsonLoader.ts @@ -12,11 +12,6 @@ export function loadJsonFile(){ environment.settings = JSON.parse(jsonStr); checkAllServerDirectories() - - - - - } catch (error) { console.error('Error reading or parsing JSON file:', error); } @@ -27,6 +22,7 @@ export function saveJsonFile() { const jsonSettings = JSON.stringify(environment.settings, null, 2); fs.writeFileSync(environment.files.settings, jsonSettings); console.log(`Settings saved to ` + environment.files.settings); + checkAllServerDirectories() } @@ -67,7 +63,7 @@ export function createPathIfNotExists(dirPath: string) { } -function checkAllServerDirectories(){ +export function checkAllServerDirectories(){ environment.settings.servers.forEach(server => { createPathIfNotExists(environment.paths.servers + "/" + server.name) })