-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
87 lines (82 loc) · 2.5 KB
/
Copy pathserver.js
File metadata and controls
87 lines (82 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require("dotenv").config();
//DROPBOX
var Dropbox = require("dropbox").Dropbox;
const fetch = require("isomorphic-fetch");
var dbx = new Dropbox({
fetch: fetch,
accessToken: process.env.DROPBOX_TOKEN,
});
//Ngrok
const Ngrok = require("./ngrokManager");
const ngrok = new Ngrok("tcp", process.env.NGROK_TOKEN, "25565");
//KEEPING ALIVE SERVER AND BACKUPS
const Cron = require("cron").CronJob;
const axios = require("axios");
//Initialize Cronjob
//Managers
const { State, STATES } = require("./ServerStateManager");
const CommandManager = require("./CommandManager");
const Eonil = require("eonil");
const MinecraftServerManager = require("./MinecraftServerManager");
//Instances
const state = State();
const app = new Eonil({ port: process.env.PORT, directory: "./public" }).StartServer().GetApp();
const express = require("express");
app.use(express.json({}));
const MSM = new MinecraftServerManager(dbx, ngrok);
let CM;
var keepAlive = new Cron({
cronTime: "*/5 * * * *",
onTick: async () => {
await MSM.BackupServer();
await axios.get("https://mcserverdmj.herokuapp.com/");
console.log("Pinging each 5 minutes");
},
start: false,
});
app.get("/startserver", async (req, res) => {
const { link: minecraftJarLink } = req.query;
if (!state.IsOnline(state.CurrentState())) {
state.ChangeState(STATES.starting);
await MSM.DownloadMinecraftJar(minecraftJarLink);
console.log("Finished piping");
await MSM.DownloadServerMinecraftWorld();
console.log("Finished downloading world");
CM = new CommandManager(await MSM.StartServer());
console.log("Starting server");
state.ChangeState(STATES.started);
keepAlive.start();
}
});
app.get("/stopserver", async (req, res) => {
if (state.IsOnline(state.CurrentState())) {
await CM.StopServer();
state.ChangeState(STATES.stopped);
keepAlive.stop();
}
});
app.get("/saveserver", async (req, res) => {
if (state.IsOnline(state.CurrentState())) {
await CM.SaveServer();
}
});
app.get("/backupserver", async (req, res) => {
if (state.IsOnline(state.CurrentState())) {
await CM.SaveServer();
await MSM.BackupServer();
}
});
app.get("/command", async (req, res) => {
const { command, name } = req.query;
if (state.IsOnline(state.CurrentState())) {
if (command) await CM.onCommand(command);
if (name) await CM.opPlayer(name);
}
});
app.get("/downloadproperties", async (req, res) => {
const properties = await MSM.DownloadServerProperties();
res.send(properties);
});
app.post("/uploadproperties", async (req, res) => {
await MSM.UploadServerProperties(req.body);
});