first commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import sql from '@/lib/db'
|
||||
|
||||
// PATCH /api/apps/[id]/config/[entryId]
|
||||
export async function PATCH(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string; entryId: string }> }
|
||||
) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const { id, entryId } = await params
|
||||
const { value, environment } = await req.json()
|
||||
|
||||
const [old] = await sql`SELECT key, value FROM config_entries WHERE id = ${entryId}`
|
||||
|
||||
const [entry] = await sql`
|
||||
UPDATE config_entries SET value = ${value}
|
||||
WHERE id = ${entryId}
|
||||
RETURNING *
|
||||
`
|
||||
|
||||
await sql`
|
||||
INSERT INTO audit_logs (app_id, environment, action, key, old_value, actor)
|
||||
VALUES (
|
||||
${id}, ${environment}, 'update',
|
||||
${old?.key}, ${old?.value?.slice(0, 100)},
|
||||
${session.user?.name ?? 'admin'}
|
||||
)
|
||||
`
|
||||
|
||||
return NextResponse.json(entry)
|
||||
}
|
||||
|
||||
// DELETE /api/apps/[id]/config/[entryId]
|
||||
export async function DELETE(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string; entryId: string }> }
|
||||
) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const { id, entryId } = await params
|
||||
const env = req.nextUrl.searchParams.get('environment') ?? ''
|
||||
const [old] = await sql`SELECT key FROM config_entries WHERE id = ${entryId}`
|
||||
|
||||
await sql`DELETE FROM config_entries WHERE id = ${entryId}`
|
||||
|
||||
await sql`
|
||||
INSERT INTO audit_logs (app_id, environment, action, key, actor)
|
||||
VALUES (${id}, ${env}, 'delete', ${old?.key}, ${session.user?.name ?? 'admin'})
|
||||
`
|
||||
|
||||
return NextResponse.json({ success: true })
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import sql from '@/lib/db'
|
||||
|
||||
// GET /api/apps/[id]/config?env=production
|
||||
export async function GET(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const { id } = await params
|
||||
const env = req.nextUrl.searchParams.get('env') ?? 'production'
|
||||
|
||||
const entries = await sql`
|
||||
SELECT ce.*
|
||||
FROM config_entries ce
|
||||
JOIN environments e ON e.id = ce.environment_id
|
||||
WHERE e.app_id = ${id} AND e.name = ${env}
|
||||
ORDER BY ce.key
|
||||
`
|
||||
return NextResponse.json(entries)
|
||||
}
|
||||
|
||||
// POST /api/apps/[id]/config
|
||||
export async function POST(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const { id } = await params
|
||||
const body = await req.json()
|
||||
const { environment_id, key, value, type, description, environment } = body
|
||||
|
||||
if (!environment_id || !key || value === undefined) {
|
||||
return NextResponse.json({ error: 'environment_id, key, value are required' }, { status: 400 })
|
||||
}
|
||||
|
||||
try {
|
||||
const [entry] = await sql`
|
||||
INSERT INTO config_entries (environment_id, key, value, type, description)
|
||||
VALUES (${environment_id}, ${key.toUpperCase()}, ${value}, ${type ?? 'text'}, ${description ?? null})
|
||||
RETURNING *
|
||||
`
|
||||
|
||||
await sql`
|
||||
INSERT INTO audit_logs (app_id, environment, action, key, actor)
|
||||
VALUES (${id}, ${environment}, 'create', ${key}, ${session.user?.name ?? 'admin'})
|
||||
`
|
||||
|
||||
return NextResponse.json(entry, { status: 201 })
|
||||
} catch (err: any) {
|
||||
if (err.code === '23505') {
|
||||
return NextResponse.json({ error: `Key "${key}" already exists in this environment` }, { status: 409 })
|
||||
}
|
||||
return NextResponse.json({ error: err.message }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import sql from '@/lib/db'
|
||||
import { randomBytes } from 'crypto'
|
||||
|
||||
export async function POST(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const { id } = await params
|
||||
const newKey = randomBytes(32).toString('hex')
|
||||
|
||||
const [app] = await sql`
|
||||
UPDATE apps SET api_key = ${newKey}
|
||||
WHERE id = ${id}
|
||||
RETURNING api_key
|
||||
`
|
||||
|
||||
await sql`
|
||||
INSERT INTO audit_logs (app_id, action, actor)
|
||||
VALUES (${id}, 'rotate_key', ${session.user?.name ?? 'admin'})
|
||||
`
|
||||
|
||||
return NextResponse.json({ api_key: app.api_key })
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import sql from '@/lib/db'
|
||||
|
||||
export async function DELETE(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const { id } = await params
|
||||
await sql`DELETE FROM apps WHERE id = ${id}`
|
||||
return NextResponse.json({ success: true })
|
||||
}
|
||||
|
||||
export async function PATCH(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const { id } = await params
|
||||
const { name, description, icon_url } = await req.json()
|
||||
|
||||
const [app] = await sql`
|
||||
UPDATE apps SET
|
||||
name = COALESCE(${name}, name),
|
||||
description = ${description !== undefined ? description : sql`description`},
|
||||
icon_url = ${icon_url !== undefined ? icon_url : sql`icon_url`},
|
||||
updated_at = NOW()
|
||||
WHERE id = ${id}
|
||||
RETURNING *
|
||||
`
|
||||
return NextResponse.json(app)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import sql from '@/lib/db'
|
||||
import { slugify } from '@/lib/utils'
|
||||
|
||||
// GET /api/apps
|
||||
export async function GET() {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const apps = await sql`SELECT * FROM apps ORDER BY created_at DESC`
|
||||
return NextResponse.json(apps)
|
||||
}
|
||||
|
||||
// POST /api/apps
|
||||
export async function POST(req: NextRequest) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const body = await req.json()
|
||||
const { name, description, icon_url } = body
|
||||
const slug = body.slug ?? slugify(name)
|
||||
|
||||
if (!name || !slug) {
|
||||
return NextResponse.json({ error: 'name and slug are required' }, { status: 400 })
|
||||
}
|
||||
|
||||
try {
|
||||
const [app] = await sql`
|
||||
INSERT INTO apps (name, slug, description, icon_url)
|
||||
VALUES (${name}, ${slug}, ${description ?? null}, ${icon_url ?? null})
|
||||
RETURNING *
|
||||
`
|
||||
|
||||
await sql`
|
||||
INSERT INTO audit_logs (app_id, action, actor)
|
||||
VALUES (${app.id}, 'create_app', ${session.user?.name ?? 'admin'})
|
||||
`
|
||||
|
||||
return NextResponse.json(app, { status: 201 })
|
||||
} catch (err: any) {
|
||||
if (err.code === '23505') {
|
||||
return NextResponse.json({ error: 'Slug already exists.' }, { status: 409 })
|
||||
}
|
||||
return NextResponse.json({ error: err.message }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import NextAuth from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
|
||||
const handler = NextAuth(authOptions)
|
||||
export { handler as GET, handler as POST }
|
||||
@@ -0,0 +1,62 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import sql from '@/lib/db'
|
||||
|
||||
/**
|
||||
* GET /api/v1/config?env=production
|
||||
*
|
||||
* Mobile app bu endpoint'i çağırır.
|
||||
* Header: X-Api-Key: <app_api_key>
|
||||
* Query: env = development | staging | production (default: production)
|
||||
*
|
||||
* Response: { "KEY": "value", "ANOTHER_KEY": "value2", ... }
|
||||
*/
|
||||
export async function GET(req: NextRequest) {
|
||||
const apiKey =
|
||||
req.headers.get('x-api-key') ??
|
||||
req.headers.get('authorization')?.replace('Bearer ', '')
|
||||
|
||||
if (!apiKey) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Missing API key. Pass X-Api-Key header.' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
const env = req.nextUrl.searchParams.get('env') ?? 'production'
|
||||
const validEnvs = ['development', 'staging', 'production']
|
||||
if (!validEnvs.includes(env)) {
|
||||
return NextResponse.json(
|
||||
{ error: `Invalid env. Must be one of: ${validEnvs.join(', ')}` },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Find app by API key
|
||||
const [app] = await sql`
|
||||
SELECT id, name FROM apps WHERE api_key = ${apiKey} LIMIT 1
|
||||
`
|
||||
if (!app) {
|
||||
return NextResponse.json({ error: 'Invalid API key' }, { status: 401 })
|
||||
}
|
||||
|
||||
// Fetch config entries for this app + environment in one join
|
||||
const entries = await sql`
|
||||
SELECT ce.key, ce.value
|
||||
FROM config_entries ce
|
||||
JOIN environments e ON e.id = ce.environment_id
|
||||
WHERE e.app_id = ${app.id} AND e.name = ${env}
|
||||
`
|
||||
|
||||
const config: Record<string, string> = {}
|
||||
for (const entry of entries) {
|
||||
config[entry.key] = entry.value
|
||||
}
|
||||
|
||||
return NextResponse.json(config, {
|
||||
headers: {
|
||||
'Cache-Control': 'no-store, no-cache, must-revalidate',
|
||||
'X-App': app.name,
|
||||
'X-Env': env,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user