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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
/AppImage
84 changes: 43 additions & 41 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@

const cors = require('cors')
const cors = require("cors");
const express = require("express");
const app = express()
const dayjs = require('dayjs')
dayjs().format()
const app = express();
const dayjs = require("dayjs");
dayjs().format();

app.use(cors());
app.options('*', (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.sendStatus(200);
})
app.options("/*splat", (req, res) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
res.header(
"Access-Control-Allow-Headers",
"Content-Type, Authorization, Content-Length, X-Requested-With",
);
res.sendStatus(200);
});
// app.options('*', cors());

app.get("/stats",cors(), async (req, res) => {
try { const slack_id = req.query.slack_id;
const startofday = dayjs().startOf('day');
const endofday = dayjs().endOf('day');
const response = await fetch(`https://hackatime.hackclub.com/api/v1/users/${slack_id}/stats?start_date=${startofday}&end_date=${endofday}`);

console.log(slack_id);
const text = await response.text(); // read raw text
console.log("API returned:", text)
console.log(startofday + " " + endofday);

if (!response.ok) {
return res.status(response.status).send(text);
}

let data;

try {
data = JSON.parse(text);
} catch (err) {
return res.status(500).send("API did not return valid JSON");
}
app.get("/stats", cors(), async (req, res) => {
try {
const slack_id = req.query.slack_id;
const startofday = dayjs().startOf("day");
const endofday = dayjs().endOf("day");
const response = await fetch(
`https://hackatime.hackclub.com/api/v1/users/${slack_id}/stats?start_date=${startofday}&end_date=${endofday}`,
);

console.log(slack_id);
const text = await response.text(); // read raw text
console.log("API returned:", text);
console.log(startofday + " " + endofday);

if (!response.ok) {
return res.status(response.status).send(text);
}

res.json(data);
let data;

try {
data = JSON.parse(text);
} catch (err) {
return res.status(500).send("API did not return valid JSON");
}

catch (error) {
console.error(error);
console.log(error); // log full error
res.status(500).json({ error: "Failed to fetch stats" });
}

res.json(data);
} catch (error) {
console.error(error);
console.log(error); // log full error
res.status(500).json({ error: "Failed to fetch stats" });
}
});

app.listen(3000, () => console.log("server running at localhost:3000"));
app.listen(3000, () => console.log("server running at localhost:3000"));
Loading