Refactor code structure for improved readability and maintainability

This commit is contained in:
shuaiplus
2026-02-26 04:54:11 +08:00
committed by Shuai
parent d80821edeb
commit 6bbc7554c1
3 changed files with 4 additions and 1487 deletions
+2 -27
View File
@@ -1,31 +1,6 @@
import { Env, DEFAULT_DEV_SECRET } from '../types';
import { Env } from '../types';
import { StorageService } from '../services/storage';
import { jsonResponse, htmlResponse } from '../utils/response';
import { renderRegisterPageHTML } from '../setup/pageTemplate';
import { LIMITS } from '../config/limits';
type JwtSecretState = 'missing' | 'default' | 'too_short';
function getJwtSecretState(env: Env): JwtSecretState | null {
const secret = (env.JWT_SECRET || '').trim();
if (!secret) return 'missing';
// Block common "forgot to change" sample value (matches .dev.vars.example)
if (secret === DEFAULT_DEV_SECRET) return 'default';
if (secret.length < LIMITS.auth.jwtSecretMinLength) return 'too_short';
return null;
}
async function handleRegisterPage(request: Request, env: Env, jwtState: JwtSecretState | null): Promise<Response> {
void request;
void env;
return htmlResponse(renderRegisterPageHTML(jwtState));
}
// GET / - Setup page
export async function handleSetupPage(request: Request, env: Env): Promise<Response> {
const jwtState = getJwtSecretState(env);
return handleRegisterPage(request, env, jwtState);
}
import { jsonResponse } from '../utils/response';
// GET /setup/status
export async function handleSetupStatus(request: Request, env: Env): Promise<Response> {
+2 -7
View File
@@ -48,7 +48,7 @@ import {
import { handleSync } from './handlers/sync';
// Setup handlers
import { handleSetupPage, handleSetupStatus } from './handlers/setup';
import { handleSetupStatus } from './handlers/setup';
import { handleWebClientPage } from './handlers/web';
import { handleKnownDevice, handleGetDevices, handleUpdateDeviceToken } from './handlers/devices';
@@ -187,15 +187,10 @@ export async function handleRequest(request: Request, env: Env): Promise<Respons
try {
// Web client entry (single-path app)
if ((path === '/' || path === '/register' || path === '/login') && method === 'GET') {
if ((path === '/' || path === '/register' || path === '/login' || path === '/setup' || path === '/setup/legacy') && method === 'GET') {
return handleWebClientPage(request, env);
}
// Legacy setup page
if ((path === '/setup' || path === '/setup/legacy') && method === 'GET') {
return handleSetupPage(request, env);
}
// Setup status
if (path === '/setup/status' && method === 'GET') {
return handleSetupStatus(request, env);
File diff suppressed because it is too large Load Diff