-
Notifications
You must be signed in to change notification settings - Fork 138
/
bootstrap.js
96 lines (75 loc) · 3.29 KB
/
bootstrap.js
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
88
89
90
91
92
93
94
95
96
const fs = require("fs")
const fetch = require("node-fetch")
const APP_NAME = process.env.APP_NAME || false;
const INCLUDE_PRERELEASES = process.env.PRERELEASES === "true";
const GITHUB_REPO = process.env.GITHUB_REPO || "freyacodes/Lavalink";
let application = fs.readFileSync("./application.yml", "utf8")
if (process.env.PORT) {
application = application.replace("DYNAMICPORT", process.env.PORT)
}
if (process.env.PASS) {
application = application.replace("youshallnotpass", process.env.PASS)
}
fs.writeFileSync("./application.yml", application)
const download = function (url, dest, cb) {
const file = fs.createWriteStream(dest);
fetch(url).then(res=>{
res.body.pipe(file)
console.log("Downloading Lavalink.jar")
file.on("finish", function () {
console.log("Downloaded Lavalink.jar")
file.close(cb);
});
file.on("error", function(err){
console.error("Filestream error while downloading Lavalink: "+err)
})
})
.catch(function(err){
console.error("Fetch error while downloading Lavalink: "+err)
})
};
function keepAlive(){
console.log("heroku-lavalink: running keepAlive")
fetch(`https://${APP_NAME}.herokuapp.com/`).catch((err)=>{
console.log("Error while running keepAlive: "+ err)
})
}
function startLavalink() {
console.log("Checking if APP_NAME is specified...")
if(APP_NAME){
console.log("I will visit myself every 20 minutes because APP_NAME specified!");
setInterval(keepAlive, 20*60*1000);
}else{
console.log("I will not visit myself every 20 minutes, APP_NAME is not specified!")
console.log("If you are using the free tier, Heroku will make this project sleep after 30 minutes unless there is http activity.")
}
const spawn = require("child_process").spawn;
const child = spawn("java", ["-jar", "Lavalink.jar"],{"stdio":"inherit"})
child.on("error", (error) => {
console.error(error);
});
child.on("close", (code) => {
console.log(`Lavalink exited with code ${code}`);
});
}
console.log("Fetching latest Lavalink.jar url...")
fetch("https://api.github.com/repos/"+GITHUB_REPO+"/releases")
.then(res => res.json())
.then(json => {
for(let i = 0; i < json.length; ++i ){ // (json[i].prerelease && INCLUDE_PRERELEASES)
if(json[i].assets[0] && json[i].assets[0].browser_download_url){ //if dl exists
if(!json[i].prerelease || INCLUDE_PRERELEASES){ //if not prerelease or if allow prerelease
console.log("Found version " + json[i].tag_name +" attempting to download...")
download(json[i].assets[0].browser_download_url, "./Lavalink.jar", startLavalink)
break;
}else{
console.log("Skipping version " + json[i].tag_name + " because it is a prerelease and PRERELEASES is set to false.")
}
}else{
console.log("Skipping version " + json[i].tag_name + " because no download is available.")
}
}
})
.catch(err =>{
console.error("Error occured when fetching latest release url: "+err)
});