Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Implement public sites caching and update site management API

Bill Yang22509e3a7edd
diff --git a/server/src/api/sites/addSite.ts b/server/src/api/sites/addSite.ts
index 6779c93ad..0e100c406 100644
--- a/server/src/api/sites/addSite.ts
+++ b/server/src/api/sites/addSite.ts
@@ -4,6 +4,7 @@ import { db } from "../../db/postgres/postgres.js";
import { sites } from "../../db/postgres/schema.js";
import { loadAllowedDomains } from "../../lib/allowedDomains.js";
import { auth } from "../../lib/auth.js";
+import { publicSites } from "../../lib/publicSites.js";
export async function addSite(
request: FastifyRequest<{
@@ -88,8 +89,12 @@ export async function addSite(
})
.returning();
+ // Update allowed domains
await loadAllowedDomains();
+ // Update publicSites cache with the new site
+ publicSites.addSite(newSite[0].siteId, newSite[0].public || false);
+
return reply.status(201).send(newSite[0]);
} catch (error) {
console.error("Error adding site:", error);
diff --git a/server/src/api/sites/changeSitePublic.ts b/server/src/api/sites/changeSitePublic.ts
index 9c1dd9458..375d61b77 100644
--- a/server/src/api/sites/changeSitePublic.ts
+++ b/server/src/api/sites/changeSitePublic.ts
@@ -3,6 +3,7 @@ import { db } from "../../db/postgres/postgres.js";
import { sites } from "../../db/postgres/schema.js";
import { eq } from "drizzle-orm";
import { getUserHasAccessToSite } from "../../lib/auth-utils.js";
+import { publicSites } from "../../lib/publicSites.js";
interface ChangeSitePublicRequest {
Body: {
@@ -49,6 +50,9 @@ export async function changeSitePublic(
})
.where(eq(sites.siteId, siteId));
+ // Update the public sites cache
+ publicSites.updateSitePublicStatus(siteId, isPublic);
+
return reply.status(200).send({ success: true });
} catch (error) {
console.error("Error changing site public status:", error);
diff --git a/server/src/api/sites/deleteSite.ts b/server/src/api/sites/deleteSite.ts
index 33020ff15..897423eca 100644
--- a/server/src/api/sites/deleteSite.ts
+++ b/server/src/api/sites/deleteSite.ts
@@ -4,6 +4,7 @@ import { db } from "../../db/postgres/postgres.js";
import { sites } from "../../db/postgres/schema.js";
import { loadAllowedDomains } from "../../lib/allowedDomains.js";
import { getUserHasAccessToSite } from "../../lib/auth-utils.js";
+import { publicSites } from "../../lib/publicSites.js";
export async function deleteSite(
request: FastifyRequest<{ Params: { id: string } }>,
@@ -22,5 +23,8 @@ export async function deleteSite(
// });
await loadAllowedDomains();
+ // Remove the site from the publicSites cache
+ publicSites.removeSite(Number(id));
+
return reply.status(200).send();
}
diff --git a/server/src/index.ts b/server/src/index.ts
index a3ea12a62..22f41ffed 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -41,6 +41,7 @@ import { mapHeaders } from "./lib/auth-utils.js";
import { auth } from "./lib/auth.js";
import { trackEvent } from "./tracker/trackEvent.js";
import { extractSiteId, isSitePublic } from "./utils.js";
+import { publicSites } from "./lib/publicSites.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
@@ -204,6 +205,10 @@ const start = async () => {
// Initialize the database
await Promise.all([initializeClickhouse(), initializePostgres()]);
await loadAllowedDomains();
+
+ // Load public sites cache
+ await publicSites.loadPublicSites();
+
// Start the server
await server.listen({ port: 3001, host: "0.0.0.0" });
diff --git a/server/src/lib/publicSites.ts b/server/src/lib/publicSites.ts
new file mode 100644
index 000000000..22171f57c
--- /dev/null
+++ b/server/src/lib/publicSites.ts
@@ -0,0 +1,75 @@
+import { db } from "../db/postgres/postgres.js";
+import { sites } from "../db/postgres/schema.js";
+
+class PublicSites {
+ private publicSitesMap: Map<number, boolean> = new Map();
+ private initialized: boolean = false;
+
+ async loadPublicSites() {
+ try {
+ const allSites = await db
+ .select({
+ siteId: sites.siteId,
+ public: sites.public,
+ })
+ .from(sites);
+
+ // Reset the map
+ this.publicSitesMap.clear();
+
+ // Populate the map with site IDs and their public status
+ for (const site of allSites) {
+ this.publicSitesMap.set(site.siteId, site.public || false);
+ }
+
+ this.initialized = true;
+ console.log(
+ `Loaded ${this.publicSitesMap.size} sites into public sites cache`
+ );
+ } catch (error) {
+ console.error("Error loading public sites:", error);
+ this.initialized = false;
+ }
+ }
+
+ /**
+ * Check if a site is public without hitting the database
+ */
+ isSitePublic(siteId: string | number): boolean {
+ const numericSiteId = Number(siteId);
+ return this.publicSitesMap.get(numericSiteId) || false;
+ }
+
+ /**
+ * Update the public status of a site in the cache
+ */
+ updateSitePublicStatus(siteId: number, isPublic: boolean): void {
+ this.publicSitesMap.set(siteId, isPublic);
+ }
+
+ /**
+ * Add a new site to the cache
+ */
+ addSite(siteId: number, isPublic: boolean): void {
+ this.publicSitesMap.set(siteId, isPublic);
+ }
+
+ /**
+ * Remove a site from the cache
+ */
+ removeSite(siteId: number): void {
+ this.publicSitesMap.delete(siteId);
+ }
+
+ /**
+ * Ensure the cache is initialized
+ */
+ async ensureInitialized() {
+ if (!this.initialized) {
+ await this.loadPublicSites();
+ }
+ }
+}
+
+// Singleton instance
+export const publicSites = new PublicSites();
diff --git a/server/src/utils.ts b/server/src/utils.ts
index f7b746a3a..8184561d7 100644
--- a/server/src/utils.ts
+++ b/server/src/utils.ts
@@ -3,6 +3,7 @@ import crypto from "crypto";
import { db } from "./db/postgres/postgres.js";
import { sites } from "./db/postgres/schema.js";
import { eq } from "drizzle-orm";
+import { publicSites } from "./lib/publicSites.js";
export function getUserId(ip: string, userAgent: string) {
return crypto
@@ -171,13 +172,11 @@ export const getIpAddress = (request: FastifyRequest): string => {
// Check if a site is public
export const isSitePublic = async (siteId: string | number) => {
try {
- const site = await db.query.sites.findFirst({
- where: eq(sites.siteId, Number(siteId)),
- columns: {
- public: true,
- },
- });
- return site?.public || false;
+ // Ensure the publicSites cache is initialized
+ await publicSites.ensureInitialized();
+
+ // Use the cached value
+ return publicSites.isSitePublic(siteId);
} catch (err) {
console.error("Error checking if site is public:", err);
return false;