Self-hosting! (#49)

* added self-hosting
This commit is contained in:
Carl-Gerhard Lindesvärd
2024-08-28 09:28:44 +02:00
committed by GitHub
parent f0b7526847
commit df05e2dab3
70 changed files with 2310 additions and 272 deletions

View File

@@ -5,6 +5,7 @@ export const TABLE_NAMES = {
events: 'events_v2',
profiles: 'profiles',
alias: 'profile_aliases',
self_hosting: 'self_hosting',
};
export const originalCh = createClient({

View File

@@ -1,3 +1,5 @@
import { generateSalt } from '@openpanel/common';
import { db } from '../prisma-client';
export async function getCurrentSalt() {
@@ -27,7 +29,7 @@ export async function getSalts() {
}
if (!prev) {
throw new Error('No previous salt found');
throw new Error('No salt found');
}
return {
@@ -35,3 +37,44 @@ export async function getSalts() {
previous: prev.salt,
};
}
export async function createInitialSalts() {
const MAX_RETRIES = 5;
const BASE_DELAY = 1000; // 1 second
const createSaltsWithRetry = async (retryCount = 0): Promise<void> => {
try {
await getSalts();
} catch (error) {
if (error instanceof Error && error.message === 'No salt found') {
console.log('Creating salts for the first time');
await db.salt.create({
data: {
salt: generateSalt(),
createdAt: new Date(new Date().getTime() - 1000 * 60 * 60 * 24),
},
});
await db.salt.create({
data: {
salt: generateSalt(),
},
});
} else {
console.log('Error getting salts', error);
if (retryCount < MAX_RETRIES) {
const delay = BASE_DELAY * Math.pow(2, retryCount);
console.log(
`Retrying in ${delay}ms... (Attempt ${retryCount + 1}/${MAX_RETRIES})`
);
await new Promise((resolve) => setTimeout(resolve, delay));
return createSaltsWithRetry(retryCount + 1);
} else {
throw new Error(
`Failed to create salts after ${MAX_RETRIES} attempts`
);
}
}
}
};
await createSaltsWithRetry();
}