管理配置增加重载
This commit is contained in:
+31
-6
@@ -10438,6 +10438,23 @@ function AdminPageClient() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 新增: 重载配置处理函数
|
||||||
|
const handleReloadConfig = async () => {
|
||||||
|
await withLoading('reloadConfig', async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/admin/reload`);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`重载失败: ${response.status}`);
|
||||||
|
}
|
||||||
|
showSuccess('重载成功,配置缓存已清除!', showAlert);
|
||||||
|
await fetchConfig();
|
||||||
|
} catch (err) {
|
||||||
|
showError(err instanceof Error ? err.message : '重载失败', showAlert);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return (
|
return (
|
||||||
<PageLayout activePath='/admin'>
|
<PageLayout activePath='/admin'>
|
||||||
@@ -10475,12 +10492,20 @@ function AdminPageClient() {
|
|||||||
管理员设置
|
管理员设置
|
||||||
</h1>
|
</h1>
|
||||||
{config && role === 'owner' && (
|
{config && role === 'owner' && (
|
||||||
<button
|
<>
|
||||||
onClick={handleResetConfig}
|
<button
|
||||||
className={`px-3 py-1 text-xs rounded-md transition-colors ${buttonStyles.dangerSmall}`}
|
onClick={handleResetConfig}
|
||||||
>
|
className={`px-3 py-1 text-xs rounded-md transition-colors ${buttonStyles.dangerSmall}`}
|
||||||
重置配置
|
>
|
||||||
</button>
|
重置配置
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={handleReloadConfig}
|
||||||
|
className={`px-3 py-1 text-xs rounded-md transition-colors ${buttonStyles.primarySmall}`}
|
||||||
|
>
|
||||||
|
重载配置
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
|
|
||||||
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||||
|
import { clearConfigCache } from '@/lib/config';
|
||||||
|
|
||||||
|
export const runtime = 'nodejs';
|
||||||
|
|
||||||
|
export async function GET(request: NextRequest) {
|
||||||
|
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
|
||||||
|
if (storageType === 'localstorage') {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: '不支持本地存储进行管理员配置',
|
||||||
|
},
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const authInfo = getAuthInfoFromCookie(request);
|
||||||
|
if (!authInfo || !authInfo.username) {
|
||||||
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||||
|
}
|
||||||
|
const username = authInfo.username;
|
||||||
|
|
||||||
|
if (username !== process.env.USERNAME) {
|
||||||
|
return NextResponse.json({ error: '仅支持站长重载配置' }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await clearConfigCache();
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{ ok: true },
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Cache-Control': 'no-store',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: '重载配置失败',
|
||||||
|
details: (error as Error).message,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -610,4 +610,9 @@ export async function getAvailableApiSites(user?: string): Promise<ApiSite[]> {
|
|||||||
|
|
||||||
export async function setCachedConfig(config: AdminConfig) {
|
export async function setCachedConfig(config: AdminConfig) {
|
||||||
cachedConfig = config;
|
cachedConfig = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function clearConfigCache() {
|
||||||
|
cachedConfig = null as any;
|
||||||
|
configInitPromise = null;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user