Initial commit
This commit is contained in:
46
app/api/agent/register/route.ts
Normal file
46
app/api/agent/register/route.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { db } from '@/lib/db'
|
||||
import { machines } from '@/lib/db/schema'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const { accessKey, name, hostname, os, osVersion, agentVersion, ipAddress } = body
|
||||
|
||||
if (!accessKey) {
|
||||
return NextResponse.json({ error: 'Access key required' }, { status: 400 })
|
||||
}
|
||||
|
||||
const result = await db
|
||||
.select()
|
||||
.from(machines)
|
||||
.where(eq(machines.accessKey, accessKey))
|
||||
.limit(1)
|
||||
|
||||
const machine = result[0]
|
||||
if (!machine) {
|
||||
return NextResponse.json({ error: 'Invalid access key' }, { status: 401 })
|
||||
}
|
||||
|
||||
await db
|
||||
.update(machines)
|
||||
.set({
|
||||
name: name || machine.name,
|
||||
hostname: hostname || machine.hostname,
|
||||
os: os || machine.os,
|
||||
osVersion: osVersion || machine.osVersion,
|
||||
agentVersion: agentVersion || machine.agentVersion,
|
||||
ipAddress: ipAddress || machine.ipAddress,
|
||||
isOnline: true,
|
||||
lastSeen: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(machines.id, machine.id))
|
||||
|
||||
return NextResponse.json({ success: true, machineId: machine.id })
|
||||
} catch (error) {
|
||||
console.error('[Agent Register] Error:', error)
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user