summaryrefslogtreecommitdiff
path: root/src/2b2w/webserver.ts
diff options
context:
space:
mode:
author8cy <[email protected]>2020-04-19 06:40:16 -0700
committer8cy <[email protected]>2020-04-19 06:40:16 -0700
commited6e347bbcce356bde0cfeba498dab3d39a6c946 (patch)
tree419225e81e9a3967ccf6f385a6e1c9c204d0b19c /src/2b2w/webserver.ts
parentlink favi, v7.2.4 (diff)
downloaddep-core-test-2b2t.tar.xz
dep-core-test-2b2t.zip
fix on a rainy day, added most of the supporttest-2b2t
issue, TypeError: Cannot read property 'end' of undefined when trying to stop
Diffstat (limited to 'src/2b2w/webserver.ts')
-rw-r--r--src/2b2w/webserver.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/2b2w/webserver.ts b/src/2b2w/webserver.ts
new file mode 100644
index 0000000..fd62718
--- /dev/null
+++ b/src/2b2w/webserver.ts
@@ -0,0 +1,54 @@
+//this module exposes functions and variables to control the HTTP server.
+import http from 'http'; //to serve the pages
+import fs from 'fs'; //to read the webpages from disk
+
+module.exports = {
+ createServer: (port) => {
+ http.createServer((req, res) => {
+ if (req.url === "./") { // main wage of web app
+ res.writeHead(200, {'Content-type': 'text/html'});
+ res.write(fs.readFileSync('index.html'));
+ res.end();
+ } else if (req.url === "./index.css") {
+ res.writeHead(200, {'Content-type': 'text/css'});
+ res.write(fs.readFileSync('index.css'));
+ res.end();
+ } else if (module.exports.password == "" || req.headers.xpassword == module.exports.password) { // before anything, test if password is valid
+ if (req.url === "/update") { // API endpoint to get position, ETA, and status in JSON format
+ res.writeHead(200, {'Content-type': 'text/json'});
+ res.write("{\"username\": \""+ module.exports.username +"\",\"place\": \""+ module.exports.queuePlace +"\",\"ETA\": \""+ module.exports.ETA +"\", \"inQueue\": " + module.exports.isInQueue+", \"restartQueue\":"+ module.exports.restartQueue+"}")
+ res.end();
+ } else if (req.url === "/start") { // API endpoint to start queuing
+ res.writeHead(200);
+ res.end();
+ module.exports.onstartcallback();
+ } else if (req.url === "/stop") { // API endpoint to stop queuing
+ res.writeHead(200);
+ res.end();
+ module.exports.onstopcallback();
+ } else if (req.url === "/togglerestart"){
+ module.exports.restartQueue = !module.exports.restartQueue;
+ } else {
+ res.writeHead(404);
+ res.end();
+ }
+ } else {
+ res.writeHead(403);
+ res.end();
+ }
+ }).listen(port);
+ },
+ onstart: (callback) => { // function to set the action to do when starting
+ module.exports.onstartcallback = callback;
+ },
+ onstop: (callback) => { // same but to stop
+ module.exports.onstopcallback = callback;
+ },
+ queuePlace : "None", // place in queue
+ ETA: "None", // ETA
+ isInQueue: false, // am i in queue
+ onstartcallback: null, // a save of the action to start
+ onstopcallback: null, // same but to stop
+ restartQueue: false, // when at the end of the queue, restart if no client is connected?
+ password: "" // password for webapp
+};