- 新增 WorldMap 组件,支持 2D 世界地图视图 - 主页添加 2D/3D 视图切换按钮 - 实现确定性坐标偏移算法,分散同城用户位置 - 更新 heatmap 和 register API 使用坐标偏移
101 lines
2.8 KiB
TypeScript
101 lines
2.8 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { nanoid } from "nanoid";
|
|
import { db } from "@/lib/db";
|
|
import { claws } from "@/lib/db/schema";
|
|
import {
|
|
setClawOnline,
|
|
updateActiveClaws,
|
|
incrementGlobalStat,
|
|
incrementRegionCount,
|
|
publishEvent,
|
|
} from "@/lib/redis";
|
|
import { generateApiKey } from "@/lib/auth/api-key";
|
|
import { getGeoLocation } from "@/lib/geo/ip-location";
|
|
import { applyDeterministicOffset } from "@/lib/geo/offset";
|
|
import { registerSchema } from "@/lib/validators/schemas";
|
|
|
|
function getClientIp(req: NextRequest): string {
|
|
const forwarded = req.headers.get("x-forwarded-for");
|
|
if (forwarded) return forwarded.split(",")[0].trim();
|
|
const realIp = req.headers.get("x-real-ip");
|
|
if (realIp) return realIp.trim();
|
|
return "127.0.0.1";
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const body = await req.json();
|
|
const parsed = registerSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{ error: "Validation failed", details: parsed.error.flatten() },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const { name, model, platform } = parsed.data;
|
|
const clawId = nanoid(21);
|
|
const apiKey = generateApiKey();
|
|
const clientIp = getClientIp(req);
|
|
const geo = await getGeoLocation(clientIp);
|
|
const now = new Date();
|
|
|
|
// Apply deterministic offset to spread out users in the same city
|
|
let finalLat: number | null = null;
|
|
let finalLng: number | null = null;
|
|
if (geo) {
|
|
const offset = applyDeterministicOffset(geo.latitude, geo.longitude, clawId);
|
|
finalLat = offset.lat;
|
|
finalLng = offset.lng;
|
|
}
|
|
|
|
await db.insert(claws).values({
|
|
id: clawId,
|
|
apiKey,
|
|
name,
|
|
model: model ?? null,
|
|
platform: platform ?? null,
|
|
ip: clientIp,
|
|
latitude: finalLat !== null ? String(finalLat) : null,
|
|
longitude: finalLng !== null ? String(finalLng) : null,
|
|
city: geo?.city ?? null,
|
|
country: geo?.country ?? null,
|
|
countryCode: geo?.countryCode ?? null,
|
|
region: geo?.region ?? null,
|
|
lastHeartbeat: now,
|
|
totalTasks: 0,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
|
|
await setClawOnline(clawId, clientIp);
|
|
await updateActiveClaws(clawId);
|
|
await incrementGlobalStat("total_claws");
|
|
|
|
if (geo?.region) {
|
|
await incrementRegionCount(geo.region);
|
|
}
|
|
|
|
await publishEvent({
|
|
type: "registered",
|
|
clawId,
|
|
clawName: name,
|
|
city: geo?.city ?? null,
|
|
country: geo?.country ?? null,
|
|
});
|
|
|
|
return NextResponse.json({
|
|
clawId,
|
|
apiKey,
|
|
name,
|
|
endpoint: `${process.env.NEXT_PUBLIC_APP_URL}/api/v1`,
|
|
});
|
|
} catch (error) {
|
|
console.error("Register error:", error);
|
|
return NextResponse.json(
|
|
{ error: "Internal server error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|