Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/VMGWARE/Camphouse
Browse files Browse the repository at this point in the history
  • Loading branch information
Codycody31 committed Jun 7, 2024
2 parents 1cbc174 + 0d07518 commit 1c97915
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 137 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,7 @@ version

# Ignore IDE files
.idea
.vscode
.vscode

# Sitemaps
backend/public/*-sitemap.xml
3 changes: 0 additions & 3 deletions .woodpecker/.docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,3 @@ steps:
tag: pull_${CI_COMMIT_PULL_REQUEST}
when:
event: pull_request

depends_on:
- test
73 changes: 0 additions & 73 deletions .woodpecker/.test.yaml

This file was deleted.

53 changes: 52 additions & 1 deletion backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ const express = require("express");
const rateLimit = require("express-rate-limit");
const mongoStore = require("rate-limit-mongo");
const helmet = require("helmet");
const fs = require("fs");
const cors = require("cors");
const { fs } = require("fs");
const chalk = require("chalk");
const swaggerJsdoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const { getVersion } = require("./utils/general");
const path = require("path");
const fileUpload = require("express-fileupload");
const {
updatePostsSitemap,
updateUsersSitemap,
USERS_SITEMAP_FILE,
POSTS_SITEMAP_FILE,
} = require("./controllers/sitemap");

// Initialize Exceptionless
let Exceptionless;
Expand All @@ -29,6 +36,8 @@ require("dotenv").config();
const app = express();
const port = process.env.APP_PORT || 3000;

app.use(cors());

// Begin the server
(async () => {
// Initialize Exceptionless
Expand Down Expand Up @@ -189,6 +198,8 @@ const port = process.env.APP_PORT || 3000;
app.use(express.json());
// Used to parse the form data that is sent to the server
app.use(express.urlencoded({ extended: true }));
// Public directory
app.use(express.static(path.join(__dirname, "public")));

// Load the controllers
const PostController = require("./controllers/PostController");
Expand Down Expand Up @@ -220,6 +231,35 @@ const port = process.env.APP_PORT || 3000;
app.use("/api/v1/blocked-email-domains", BlockedEmailDomainController);
app.use("/api/v1/audit-logs", AuditLogController);

// Custom routes to serve the sitemap files
app.get("/api/users-sitemap.xml", async (req, res) => {
res.sendFile(USERS_SITEMAP_FILE, (err) => {
if (err) {
console.error("Error sending users sitemap file:", err);
res.status(500).json({
status: "error",
code: 500,
message: "Internal server error",
data: null,
});
}
});
});

app.get("/api/posts-sitemap.xml", async (req, res) => {
res.sendFile(POSTS_SITEMAP_FILE, (err) => {
if (err) {
console.error("Error sending users sitemap file:", err);
res.status(500).json({
status: "error",
code: 500,
message: "Internal server error",
data: null,
});
}
});
});

// Swagger documentation
const options = require("./configs/swagger");
const specs = swaggerJsdoc(options);
Expand Down Expand Up @@ -278,5 +318,16 @@ const port = process.env.APP_PORT || 3000;
console.log(
chalk.yellow(`📚 API docs at http://localhost:${port}/api/docs`)
);

setTimeout(() => {
updatePostsSitemap();
updateUsersSitemap();
}, 5000);

// Recheck every 10 minutes
setInterval(() => {
updatePostsSitemap();
updateUsersSitemap();
}, 600000);
});
})();
87 changes: 87 additions & 0 deletions backend/controllers/sitemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const { createWriteStream } = require("fs");
const axios = require("axios");
const { SitemapStream, streamToPromise } = require("sitemap");
const path = require("path");

const USERS_SITEMAP_FILE = path.join(__dirname, "../public", "users-sitemap.xml");
const POSTS_SITEMAP_FILE = path.join(__dirname, "../public", "posts-sitemap.xml");

const updateUsersSitemap = async () => {
try {
const response = await axios.get(
"https://camphouse.vmgware.dev/api/v1/users?page=1&limit=1000"
);
const userData = response.data;
const users = userData.data.users;

const sitemapStream = new SitemapStream({
hostname: "https://camphouse.vmgware.dev/",
lastmodDateOnly: false,
});

for (const user of users) {
const userHandle = user.handle;
const userLink = `https://camphouse.vmgware.dev/@${userHandle}`;

sitemapStream.write({
url: userLink,
changefreq: "weekly",
lastmod: user.updatedAt,
});
}

sitemapStream.end();
const sitemapXML = await streamToPromise(sitemapStream);
const sitemapFile = createWriteStream(USERS_SITEMAP_FILE);
sitemapFile.write(sitemapXML.toString());
sitemapFile.end();

console.log("Users sitemap updated successfully!");
} catch (error) {
console.error("Failed to update users sitemap:", error);
}
};

// Function to update the posts sitemap
const updatePostsSitemap = async () => {
try {
const response = await axios.get(
"https://camphouse.vmgware.dev/api/v1/posts?page=1&limit=1000"
);
const postData = response.data;
const posts = postData.data.posts;

const sitemapStream = new SitemapStream({
hostname: "https://camphouse.vmgware.dev/",
lastmodDateOnly: false,
});

for (const post of posts) {
const postId = post._id;
const postLink = `https://camphouse.vmgware.dev/post/${postId}`;

sitemapStream.write({
url: postLink,
changefreq: "daily",
lastmod: post.updatedAt,
});
}

sitemapStream.end();
const sitemapXML = await streamToPromise(sitemapStream);
const sitemapFile = createWriteStream(POSTS_SITEMAP_FILE);
sitemapFile.write(sitemapXML.toString());
sitemapFile.end();

console.log("Posts sitemap updated successfully!");
} catch (error) {
console.error("Failed to update posts sitemap:", error);
}
};

module.exports = {
updateUsersSitemap,
updatePostsSitemap,
USERS_SITEMAP_FILE,
POSTS_SITEMAP_FILE,
};
Loading

0 comments on commit 1c97915

Please sign in to comment.