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
34 changes: 20 additions & 14 deletions client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export class Client {
* @return {Promise<string | null>} username
* */
async getUser() {
throw new Error("Not implemented");
return fetch('api/user').then(async res => {
return (await res.json())['username'];
})
}

/**
Expand All @@ -17,7 +19,11 @@ export class Client {
* @return {Promise<string | null>} username
* */
async loginUser(username) {
throw new Error("Not implemented");
return fetch(`api/login?username=${username}`).then(async res => {
let json = await res.json();
console.log("client/login: " + json['username'])
return json['username'];
})
}

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

/**
Expand All @@ -49,8 +55,8 @@ export class Client {
* @property {string} summary
* @return {Promise<About>}
* */
async getInfo() {
throw new Error("Not implemented");
async getInfo() { //let response =
return (await fetch('https://api.spacexdata.com/v3/info')).json();
}

/**
Expand All @@ -63,7 +69,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 +86,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 +99,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 +124,7 @@ export class Client {
* @return {Promise<RocketFull>}
* */
async getRocket(id) {
throw new Error("Not implemented");
return (await fetch(`https://api.spacexdata.com/v3/rocket/${id}`)).json();
}

/**
Expand All @@ -135,7 +141,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 +158,7 @@ export class Client {
* @return {Promise<Item[]>}
* */
async getSentToMars() {
throw new Error("Not implemented");
throw new Error("Not implemented");
}

/**
Expand All @@ -170,7 +176,7 @@ export class Client {
* @return {Promise<Item[]>}
* */
async sendToMars(item) {
throw new Error("Not implemented");
throw new Error("Not implemented");
}

/**
Expand All @@ -181,6 +187,6 @@ export class Client {
* @return {Promise<Item[]>}
* */
async cancelSendingToMars(item) {
throw new Error("Not implemented");
throw new Error("Not implemented");
}
}
}
52 changes: 43 additions & 9 deletions server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,52 @@ const rootDir = process.cwd();
const port = 3000;
const app = express();

function loginMiddleware(req, res, next) {
let notRedirectingUrls = ['static', 'login', 'api'];
let root = req.url.split('/')[1];
let isFile = root.split('.').length > 1;
if (notRedirectingUrls.includes(root) || isFile || req.cookies.username)
next();
else
res.redirect('/login');
};

app.use(express.static('spa/build'));
app.use(cookieParser());
app.use(loginMiddleware);

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(":)");
app.get("/api/login", (req, res) => {
res.cookie('username', req.query.username, { 'httpOnly': true, 'secure': true, 'sameSite': 'Strict' });
res.json({ 'username': req.query.username });
});

app.listen(port, () => {
console.log(`App listening on port ${port}`);
app.get("/api/user", (req, res) => {
res.json({ 'username': req.cookies.username });
});

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

app.get('/*', (req, res) => {
res.redirect('/');
});


https.createServer({
key: fs.readFileSync("certs/server.key"),
cert: fs.readFileSync("certs/server.cert"),
},
app
).listen(port, function() {
console.log(`Я работаю на ${port}`);
});