Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class Client {
* @return {Promise<string | null>} username
* */
async getUser() {
throw new Error("Not implemented");
return await fetch('/api/username').then(async res => await res.text());
}

/**
Expand All @@ -17,7 +17,8 @@ export class Client {
* @return {Promise<string | null>} username
* */
async loginUser(username) {
throw new Error("Not implemented");
await fetch(`/api/login?username=${username}`).then(async res => await res.text());
return username || null
}

/**
Expand All @@ -26,7 +27,7 @@ export class Client {
* @return {void}
* */
async logoutUser() {
throw new Error("Not implemented");
await fetch('/api/logout');
}

/**
Expand All @@ -50,7 +51,7 @@ export class Client {
* @return {Promise<About>}
* */
async getInfo() {
throw new Error("Not implemented");
return (await fetch("https://api.spacexdata.com/v3/info")).json();
}

/**
Expand All @@ -63,7 +64,7 @@ export class Client {
* @return {Promise<EventBrief[]>}
* */
async getHistory() {
throw new Error("Not implemented");
return (await fetch('https://api.spacexdata.com/v3/history')).json();
}

/**
Expand All @@ -80,7 +81,7 @@ export class Client {
* @return {Promise<EventFull>}
* */
async getHistoryEvent(id) {
throw new Error("Not implemented");
return (await fetch(`https://api.spacexdata.com/v3/history/${id}`)).json();
}

/**
Expand All @@ -93,7 +94,7 @@ export class Client {
* @return {Promise<RocketBrief[]>}
* */
async getRockets() {
throw new Error("Not implemented");
return (await fetch('https://api.spacexdata.com/v3/rockets')).json();
}

/**
Expand All @@ -118,7 +119,7 @@ export class Client {
* @return {Promise<RocketFull>}
* */
async getRocket(id) {
throw new Error("Not implemented");
return (await fetch(`https://api.spacexdata.com/v3/rockets/${id}`)).json();
}

/**
Expand All @@ -135,7 +136,7 @@ export class Client {
* @return {Promise<Roadster>}
* */
async getRoadster() {
throw new Error("Not implemented");
return (await fetch('https://api.spacexdata.com/v3/roadster')).json();
}

/**
Expand All @@ -152,7 +153,8 @@ export class Client {
* @return {Promise<Item[]>}
* */
async getSentToMars() {
throw new Error("Not implemented");
let response = await fetch('api/user/sendToMars/get');
return (await response.json());
}

/**
Expand All @@ -170,7 +172,15 @@ export class Client {
* @return {Promise<Item[]>}
* */
async sendToMars(item) {
throw new Error("Not implemented");
let response = await fetch('api/user/sendToMars/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({'item': item})
});

return (await response.json());
}

/**
Expand All @@ -181,6 +191,14 @@ export class Client {
* @return {Promise<Item[]>}
* */
async cancelSendingToMars(item) {
throw new Error("Not implemented");
let response = await fetch('api/user/sendToMars/cancel', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({'item': item})
});

return (await response.json());
}
}
67 changes: 16 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 80 additions & 10 deletions server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,96 @@ import * as path from "path";
import fs from "fs";
import express from "express";
import https from "https";
import cookieParser from "cookie-parser";
import bodyParser from "body-parser";
import fetch from "node-fetch";
import cookieParser from "cookie-parser";

const rootDir = process.cwd();
const port = 3000;
const app = express();
const notRedirectingUrls = ['login', 'api', 'static'];

app.use(express.static("spa/build"));
app.use(cookieParser());
app.use(bodyParser.json());

app.use(function (req, res, next) {
const root = req.url.split('/')[1];
const shouldBeSkipped = notRedirectingUrls.includes(root);
const isFile = root.split('.').length > 1;
const haveCookie = req.cookies.username !== undefined;
if (shouldBeSkipped || isFile || haveCookie) {
next()
} else
res.redirect('/login');
})

app.get("/client.mjs", (_, res) => {
res.header("Cache-Control", "private, no-cache, no-store, must-revalidate");
res.sendFile(path.join(rootDir, "client.mjs"), {
maxAge: -1,
cacheControl: false,
});
res.header("Cache-Control", "private, no-cache, no-store, must-revalidate");
res.sendFile(path.join(rootDir, "client.mjs"), {
maxAge: -1,
cacheControl: false,
});
});

app.get("/", (_, res) => {
res.send(":)");
res.send(":)");
});

app.get("/api/username", (req, res) => {
res.send(req.cookies.username || null);
});

app.listen(port, () => {
console.log(`App listening on port ${port}`);
app.get("/api/login", (req, res) => {
const username = req.query.username;
res.cookie("username", username);
res.send(username);
});

app.get("/api/logout", (req, res) => {
res.clearCookie('username');
res.status(201).end();
});

const mars = {}

app.post("/api/user/sendToMars/send", (req, res) => {
const name = req.cookies.username;

const item = req.body['item'];

if (name in mars) {
mars[name].push(item)
} else {
mars[name] = [item];
}

res.json(mars[name]);
});
app.post("/api/user/sendToMars/cancel", (req, res) => {
const name = req.cookies.username;

const item = req.body['item'];

if (name in mars) {
const index = mars[name].indexOf(item);
mars[name].splice(index, 1);
}

res.json(mars[name]);
});
app.get("/api/user/sendToMars/get", (req, res) => {
const name = req.cookies.username;

res.json(mars[name] || []);
});

app.get("/*", (req, res) => {
res.sendFile(path.join(rootDir, "spa/build/index.html"));
});

https.createServer({
key: fs.readFileSync("certs/server.key"),
cert: fs.readFileSync("certs/server.cert"),
}, app).listen(port, function () {
console.log(`app on https://localhost:3000/`);
});