mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-11 00:08:37 +01:00
new selfhosted version
This commit is contained in:
21
dashboard/server/plugins/aggregation.ts
Normal file
21
dashboard/server/plugins/aggregation.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { executeAggregation } from "../services/AggregationService";
|
||||
import cluster from 'cluster';
|
||||
|
||||
export default defineNitroPlugin(async nitroApp => {
|
||||
try {
|
||||
|
||||
if (!cluster.isPrimary) return console.log('NOT PRIMARY CLUSTER');
|
||||
console.log('PRIMARY CLUSTER');
|
||||
|
||||
setInterval(async () => {
|
||||
const date = new Date();
|
||||
console.log(date, 'AGGREGATING');
|
||||
await executeAggregation(date);
|
||||
console.log(date, 'AGGREGATED');
|
||||
}, 1000 * 60 * 60 * 6);
|
||||
|
||||
|
||||
} catch (ex) {
|
||||
console.error(ex);
|
||||
}
|
||||
})
|
||||
10
dashboard/server/plugins/cache.ts
Normal file
10
dashboard/server/plugins/cache.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
|
||||
export default defineNitroPlugin(async nitroApp => {
|
||||
try {
|
||||
await Redis.init();
|
||||
console.log('Cache connected')
|
||||
} catch (ex) {
|
||||
console.error(ex);
|
||||
}
|
||||
})
|
||||
14
dashboard/server/plugins/database.ts
Normal file
14
dashboard/server/plugins/database.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import mongoose, { Types } from "mongoose";
|
||||
import { VisitModel } from "~/shared/schema/metrics/VisitSchema";
|
||||
import fs from 'fs';
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
|
||||
export default defineNitroPlugin(async nitroApp => {
|
||||
try {
|
||||
await mongoose.connect(config.MONGO_CONNECTION_STRING);
|
||||
console.log('Database connected')
|
||||
} catch (ex) {
|
||||
console.error(ex);
|
||||
}
|
||||
})
|
||||
57
dashboard/server/plugins/selfhost.ts
Normal file
57
dashboard/server/plugins/selfhost.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { PasswordModel } from "~/shared/schema/PasswordSchema";
|
||||
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||
import { UserLimitModel } from "~/shared/schema/UserLimitSchema";
|
||||
import { UserModel } from "~/shared/schema/UserSchema";
|
||||
|
||||
export default defineNitroPlugin(async () => {
|
||||
|
||||
const selfhosted = isSelfhosted();
|
||||
const aiEnabled = isAiEnabled();
|
||||
|
||||
console.log({ selfhosted, aiEnabled });
|
||||
|
||||
if (!selfhosted) return;
|
||||
|
||||
console.log('[SELFHOSTED] Admin account Creation');
|
||||
|
||||
const { ADMIN_EMAIL, ADMIN_PASSWORD, LICENSE_KEY } = useRuntimeConfig();
|
||||
|
||||
const admin_email = ADMIN_EMAIL;
|
||||
const admin_password = ADMIN_PASSWORD;
|
||||
|
||||
if (!admin_email) throw createError({ status: 400, message: 'ADMIN_EMAIL is not defined in env' });
|
||||
if (!admin_password) throw createError({ status: 400, message: 'ADMIN_PASSWORD is not defined in env' });
|
||||
|
||||
const hashedPassword = await hashPassword(admin_password);
|
||||
|
||||
await UserModel.updateOne({ email: admin_email }, { email: admin_email, name: admin_email, given_name: admin_email, locale: '', picture: '' }, { upsert: true });
|
||||
await PasswordModel.updateOne({ email: admin_email }, { email: admin_email, password: hashedPassword }, { upsert: true });
|
||||
|
||||
const user = await UserModel.findOne({ email: admin_email }, { _id: 1 });
|
||||
if (!user) throw Error('USER NOT FOUND');
|
||||
|
||||
|
||||
const licenseOk: any = await $fetch<any>(`https://license.litlyx.com/license/${LICENSE_KEY}`);
|
||||
|
||||
await PremiumModel.updateOne({ user_id: user._id }, {
|
||||
premium_type: licenseOk.ok ? 9999 : 9998,
|
||||
customer_id: 'selfhosted',
|
||||
subscription_id: 'selfhosted',
|
||||
expire_at: Date.now() + 1000 * 60 * 60 * 24 * 365 * 100,
|
||||
plan_cancelled: false,
|
||||
payment_failed: false
|
||||
}, { upsert: true });
|
||||
|
||||
await UserLimitModel.updateOne({ user_id: user._id }, {
|
||||
events: 0,
|
||||
visits: 0,
|
||||
ai_messages: 0,
|
||||
ai_limit: 999_999_999,
|
||||
limit: 999_999_999,
|
||||
billing_start_at: Date.now(),
|
||||
billing_expire_at: Date.now() + 1000 * 60 * 60 * 24 * 365 * 100
|
||||
}, { upsert: true });
|
||||
|
||||
console.log('[SELFHOSTED] Admin account OK');
|
||||
|
||||
});
|
||||
16
dashboard/server/plugins/session.ts
Normal file
16
dashboard/server/plugins/session.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { UserModel } from "~/shared/schema/UserSchema";
|
||||
|
||||
export default defineNitroPlugin(() => {
|
||||
|
||||
sessionHooks.hook('fetch', async (session, event) => {
|
||||
const user_id = session.secure?.user_id;
|
||||
if (!user_id) throw createError({ status: 401, message: 'Session expired' });
|
||||
const user = await UserModel.findById({ _id: user_id });
|
||||
if (!user) throw createError({ status: 401, message: 'Session expired' });
|
||||
});
|
||||
|
||||
sessionHooks.hook('clear', async (session, event) => {
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
52
dashboard/server/plugins/trpc.ts
Normal file
52
dashboard/server/plugins/trpc.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
import type { AppRouter as PaymentsAppRouter } from '../../../payments/src/index';
|
||||
import type { AppRouter as EmailAppRouter } from '../../../emails/src/index';
|
||||
import { createTRPCClient, httpBatchLink, TRPCClient } from '@trpc/client';
|
||||
|
||||
|
||||
export type tRpcPaymentsType = TRPCClient<PaymentsAppRouter>;
|
||||
export type tRpcEmailsType = TRPCClient<EmailAppRouter>;
|
||||
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
|
||||
export default defineNitroPlugin(async nitroApp => {
|
||||
try {
|
||||
|
||||
if (isSelfhosted()) return;
|
||||
|
||||
const tRpcPayments = createTRPCClient<PaymentsAppRouter>({
|
||||
links: [
|
||||
httpBatchLink({
|
||||
url: config.PAYMENT_TRPC_URL,
|
||||
headers: {
|
||||
Authorization: `Bearer ${config.PAYMENT_SECRET}`
|
||||
}
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
const tRpcEmails = createTRPCClient<EmailAppRouter>({
|
||||
links: [
|
||||
httpBatchLink({
|
||||
url: config.EMAIL_TRPC_URL,
|
||||
headers: {
|
||||
Authorization: `Bearer ${config.EMAIL_SECRET}`
|
||||
}
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
|
||||
const app = nitroApp as any;
|
||||
|
||||
app.shared = app.shared || {};
|
||||
app.shared.tRpcPayments = tRpcPayments;
|
||||
app.shared.tRpcEmails = tRpcEmails;
|
||||
|
||||
console.log('tRpc setup complete.')
|
||||
|
||||
} catch (ex) {
|
||||
console.error(ex);
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user