first commit! 🎉
This commit is contained in:
commit
c35c18bbbc
19 changed files with 1046 additions and 0 deletions
112
server/main.js
Normal file
112
server/main.js
Normal file
|
@ -0,0 +1,112 @@
|
|||
import http from "http";
|
||||
import path from "path";
|
||||
import fs, { openSync } from "fs";
|
||||
import Log from "../common/log.js";
|
||||
import { init as initWS } from "./ws.js";
|
||||
|
||||
var PORT = 3000;
|
||||
var mime_types = {
|
||||
'html': 'text/html',
|
||||
'css': 'text/css',
|
||||
'js': 'application/javascript',
|
||||
'png': 'image/png',
|
||||
'jpg': 'image/jpg',
|
||||
'gif': 'image/gif',
|
||||
};
|
||||
|
||||
process.argv.forEach((arg, index) => {
|
||||
if (index < 2) return;
|
||||
if (!arg.startsWith('-')) return;
|
||||
switch (arg.substring(1)) {
|
||||
case "port":
|
||||
if (process.argv.length < index + 2) {
|
||||
Log.error("FATAL: -port was supplied with no arguments.");
|
||||
exit(1);
|
||||
}
|
||||
var val = process.argv[index + 1];
|
||||
if (val.startsWith('-')) {
|
||||
Log.error("FATAL: -port was supplied with no arguments.");
|
||||
exit(1);
|
||||
}
|
||||
var port = Number(val);
|
||||
if (isNaN(port)) {
|
||||
Log.error("FATAL: -port was supplied with invalid arguments.");
|
||||
exit(1);
|
||||
}
|
||||
PORT = port;
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
var code = await new Promise((resolve) => {
|
||||
if (req.method !== "GET") {
|
||||
res.writeHead(501, {
|
||||
"Content-Type": "text/plain",
|
||||
"Server": "ari's awesome server"
|
||||
});
|
||||
res.end("Not Implemented");
|
||||
return resolve(501);
|
||||
}
|
||||
|
||||
// find the file
|
||||
var filepath = path.join("client", req.url);
|
||||
if (req.url.startsWith("/common/")) {
|
||||
filepath = path.join("common", req.url.slice(8));
|
||||
}
|
||||
if (filepath.endsWith('/') || filepath.endsWith('\\')) {
|
||||
filepath = path.join(filepath, "index.html");
|
||||
}
|
||||
if (!fs.existsSync(filepath)) {
|
||||
res.writeHead(404, {
|
||||
"Content-Type": "text/plain",
|
||||
"Server": "ari's awesome server"
|
||||
});
|
||||
res.end("Not Found");
|
||||
return resolve(404);
|
||||
}
|
||||
|
||||
try {
|
||||
var ext = path.extname(filepath).slice(1);
|
||||
var mime_type = mime_types[ext] || "application/octet-stream";
|
||||
|
||||
const stream = fs.createReadStream(filepath);
|
||||
res.writeHead(200, {
|
||||
"Content-Type": mime_type,
|
||||
"Server": "ari's awesome server"
|
||||
});
|
||||
stream.pipe(res);
|
||||
return resolve(200);
|
||||
stream.on("open", () => {
|
||||
res.writeHead(200, {
|
||||
"Content-Type": mime_type,
|
||||
"Server": "ari's awesome server"
|
||||
});
|
||||
stream.pipe(res);
|
||||
res.end();
|
||||
return resolve(200);
|
||||
});
|
||||
stream.on("error", error => {
|
||||
throw error;
|
||||
});
|
||||
} catch (error) {
|
||||
Log.error(error);
|
||||
res.writeHead(500, {
|
||||
"Content-Type": "text/plain",
|
||||
"Server": "ari's awesome server"
|
||||
});
|
||||
res.end("Internal Server Error");
|
||||
return resolve(500);
|
||||
}
|
||||
});
|
||||
|
||||
Log.info(`${code} - ${req.method} ${req.url}`);
|
||||
});
|
||||
|
||||
initWS(server);
|
||||
|
||||
server.on("listening", () => {
|
||||
Log.info("server listening on port " + PORT);
|
||||
});
|
||||
|
||||
server.listen(PORT, "0.0.0.0")
|
Loading…
Add table
Add a link
Reference in a new issue