feat: Implement default API site seeding, add a dialect-aware setting upsert utility, and introduce check-in API routes with background task processing.

This commit is contained in:
cita
2026-03-11 14:31:23 +08:00
parent f43247e91d
commit 921c551a3a
6 changed files with 52 additions and 38 deletions
+2 -2
View File
@@ -1,5 +1,6 @@
import { FastifyInstance } from 'fastify';
import { db, schema } from '../../db/index.js';
import { upsertSetting } from '../../db/upsertSetting.js';
import { eq, desc } from 'drizzle-orm';
import { checkinAccount, checkinAll } from '../../services/checkinService.js';
import { updateCheckinCron } from '../../services/checkinScheduler.js';
@@ -144,8 +145,7 @@ export async function checkinRoutes(app: FastifyInstance) {
app.put<{ Body: { cron: string } }>('/api/checkin/schedule', async (request) => {
try {
updateCheckinCron(request.body.cron);
await db.insert(schema.settings).values({ key: 'checkin_cron', value: JSON.stringify(request.body.cron) })
.onConflictDoUpdate({ target: schema.settings.key, set: { value: JSON.stringify(request.body.cron) } }).run();
await upsertSetting('checkin_cron', request.body.cron);
return { success: true, cron: request.body.cron };
} catch (err: any) {
return { error: err.message };
+2 -9
View File
@@ -1,5 +1,6 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import { db, schema } from '../../db/index.js';
import { upsertSetting } from '../../db/upsertSetting.js';
import { config } from '../../config.js';
import { eq } from 'drizzle-orm';
@@ -7,15 +8,7 @@ const MONITOR_AUTH_COOKIE = 'meta_monitor_auth';
const LDOH_BASE_URL = 'https://ldoh.105117.xyz';
const LDOH_COOKIE_SETTING_KEY = 'monitor_ldoh_cookie';
async function upsertSetting(key: string, value: unknown) {
await db.insert(schema.settings)
.values({ key, value: JSON.stringify(value) })
.onConflictDoUpdate({
target: schema.settings.key,
set: { value: JSON.stringify(value) },
})
.run();
}
async function getSettingString(key: string): Promise<string> {
const row = await db.select().from(schema.settings).where(eq(schema.settings.key, key)).get();
+3 -10
View File
@@ -2,6 +2,7 @@
import cron from 'node-cron';
import { config } from '../../config.js';
import { db, runtimeDbDialect, schema } from '../../db/index.js';
import { upsertSetting } from '../../db/upsertSetting.js';
import { refreshModelsAndRebuildRoutes } from '../../services/modelService.js';
import { updateBalanceRefreshCron, updateCheckinCron } from '../../services/checkinScheduler.js';
import { sendNotification } from '../../services/notifyService.js';
@@ -77,15 +78,7 @@ function maskSecret(value: string): string {
return `${value.slice(0, 4)}****${value.slice(-4)}`;
}
async function upsertSetting(key: string, value: unknown) {
await db.insert(schema.settings)
.values({ key, value: JSON.stringify(value) })
.onConflictDoUpdate({
target: schema.settings.key,
set: { value: JSON.stringify(value) },
})
.run();
}
async function appendSettingsEvent(input: {
type: 'checkin' | 'balance' | 'proxy' | 'status' | 'token';
@@ -103,7 +96,7 @@ async function appendSettingsEvent(input: {
relatedType: 'settings',
createdAt,
}).run();
} catch {}
} catch { }
}
function toPositiveNumberOrFallback(value: unknown, fallback: number) {