This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
79 lines (63 loc) · 2.38 KB
/
index.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
/*
Please use Replit's built in chat feature (It's the chat button in the left bar)
// This is ES6 not CommonJS, require() is imported also
(also remember to remove this comment in a github repo)
*/
import createBareServer from '@tomphttp/bare-server-node'
import http from 'node:http'
import { createRequire } from "module"
const require = createRequire(import.meta.url)
const Corrosion = require('corrosion')
const config = require("./config.json")
const port = process.env.PORT || 8080
// The following message MAY NOT be removed
console.log("Amethyst\nThis program comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to redistribute it\nunder the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nYou should have received a copy of the GNU General Public License\nalong with this program. If not, see <https://www.gnu.org/licenses/>.\n")
// Bare Server Stuff
const httpServer = http.createServer()
const bare = createBareServer('/bare/')
// Corrosion
const _corrosion = new Corrosion({
codec: 'xor',
prefix: "/corrosion/",
title: config.branding
})
_corrosion.bundleScripts();
// Create the express server (frontend)
import express from 'express'
const app = express();
app.use(express.static("./public", {
extensions: ["html"]
}));
app.get('/', (req, res) => {
res.sendFile("index.html", { root: "./public" })
})
// Express randomly sends a 301 moved permanently leading to a 404 page. This should fix it (temp fix)
// It happens randomly and should not happen again
app.get('/gs', (req, res) => {
res.sendFile("gs.html", { root: "./public" })
})
app.use((req, res) => {
res.status(404).sendFile("404.html", { root: "./public" })
})
// Create a HTTP server for bare server and Corrosion.
httpServer.on('request', (req, res) => {
if (bare.shouldRoute(req)) {
bare.routeRequest(req, res)
} else if (req.url.startsWith(_corrosion.prefix)) {
_corrosion.request(req, res)
} else {
app(req, res)
}
})
httpServer.on('upgrade', (req, socket, head) => {
if (bare.shouldRoute(req)) {
bare.routeUpgrade(req, socket, head)
} else {
socket.end()
}
});
httpServer.on('listening', () => {
console.log(`Amethyst is running at localhost:${port}`)
console.log(`Timestamp ${new Date().toUTCString()}`)
});
httpServer.listen({ port: port })