update version.js
This commit is contained in:
+34
-25
@@ -1,53 +1,62 @@
|
|||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
|
const moment = require("moment-timezone");
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const moment = require('moment-timezone');
|
|
||||||
|
let latestCommitCache = null;
|
||||||
|
let lastFetchedTime = 0;
|
||||||
|
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes in milliseconds
|
||||||
|
|
||||||
// Fetch the latest commit from a GitHub repository
|
|
||||||
async function fetchLatestCommitFromGitHub() {
|
async function fetchLatestCommitFromGitHub() {
|
||||||
const githubOwner = process.env.GITHUB_OWNER; // Your GitHub username or org
|
const githubOwner = process.env.GITHUB_OWNER;
|
||||||
const githubRepo = process.env.GITHUB_REPO; // Your repository name
|
const githubRepo = process.env.GITHUB_REPO;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const url = `https://api.github.com/repos/${githubOwner}/${githubRepo}/commits`;
|
const url = `https://api.github.com/repos/${githubOwner}/${githubRepo}/commits`;
|
||||||
|
|
||||||
// Removed the unnecessary "body" property from the request
|
|
||||||
const response = await axios.get(url, {
|
const response = await axios.get(url, {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
"Content-Type": "application/json",
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.data && response.data.length > 0) {
|
if (response.data && response.data.length > 0) {
|
||||||
const latestCommit = response.data[0];
|
const latestCommit = response.data[0];
|
||||||
return {
|
latestCommitCache = {
|
||||||
sha: latestCommit.sha,
|
sha: latestCommit.sha,
|
||||||
message: latestCommit.commit.message,
|
message: latestCommit.commit.message,
|
||||||
author: latestCommit.commit.author.name,
|
author: latestCommit.commit.author.name,
|
||||||
date: latestCommit.commit.author.date,
|
date: latestCommit.commit.author.date,
|
||||||
};
|
};
|
||||||
|
lastFetchedTime = Date.now();
|
||||||
} else {
|
} else {
|
||||||
throw new Error('No commits found.');
|
throw new Error("No commits found.");
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(`Error fetching commit: ${error.message}`);
|
console.error("Error fetching commit:", error.message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initial fetch to populate cache
|
||||||
|
fetchLatestCommitFromGitHub();
|
||||||
|
|
||||||
|
// Refresh cache every 5 minutes
|
||||||
|
setInterval(fetchLatestCommitFromGitHub, CACHE_DURATION);
|
||||||
|
|
||||||
|
// Route to get the latest commit (cached)
|
||||||
|
router.get("/", async (req, res) => {
|
||||||
|
if (!latestCommitCache) {
|
||||||
|
return res.status(500).json({ message: "Commit data is not available yet." });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Route to get the version (latest commit from GitHub)
|
|
||||||
router.get("/", async (req, res) => {
|
|
||||||
try {
|
|
||||||
const latestCommit = await fetchLatestCommitFromGitHub();
|
|
||||||
res.json({
|
res.json({
|
||||||
version: (latestCommit.sha).substring(0,5),
|
version: latestCommitCache.sha.substring(0, 5),
|
||||||
message: latestCommit.message,
|
message: latestCommitCache.message,
|
||||||
author: latestCommit.author,
|
author: latestCommitCache.author,
|
||||||
date: moment(latestCommit.date).tz('America/New_York').format('YYYY-MM-DD HH:mm:ss'),
|
date: moment(latestCommitCache.date)
|
||||||
});
|
.tz("America/New_York")
|
||||||
} catch (error) {
|
.format("YYYY-MM-DD HH:mm:ss"),
|
||||||
console.error("Error fetching latest commit:", error);
|
|
||||||
res.status(500).json({ message: "Error retrieving version information" });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
Reference in New Issue
Block a user