diff --git a/app/api/auth/[...nextauth]/options.ts b/app/api/auth/[...nextauth]/options.ts index 42360a7..7a4e012 100644 --- a/app/api/auth/[...nextauth]/options.ts +++ b/app/api/auth/[...nextauth]/options.ts @@ -1,9 +1,55 @@ import { RedisClientType, createClient } from "falkordb"; import CredentialsProvider from "next-auth/providers/credentials" -import { AuthOptions } from "next-auth" +import { AuthOptions, User } from "next-auth" -export const connections = new Map(); +const connections = new Map(); + +async function newClient(credentials: {host: string, port: string, password: string, username: string}, id: number) { + const client = await createClient({ + socket: { + host: credentials.host ?? "localhost", + port: credentials.port ? parseInt(credentials.port, 10) : 6379, + reconnectStrategy: false + }, + password: credentials.password ?? undefined, + username: credentials.username ?? undefined + }) + + // Save connection in connections map for later use + connections.set(id, client as RedisClientType) + + await client.on('error', err => { + // Close coonection on error and remove from connections map + console.error('FalkorDB Client Error', err) + const connection = connections.get(id) + if (connection) { + connections.delete(id) + connection.disconnect() + .catch((e) => { + console.warn('FalkorDB Client Disconnect Error', e) + }) + } + }).connect(); + + // Verify connection + await client.ping() + return client as RedisClientType +} + +export async function getConnection(user: User) { + let conn = connections.get(user.id) + if (!conn) { + conn = await newClient({ + host: user.host, + port: user.port.toString() ?? "6379", + username: user.username, + password: user.password, + }, user.id) + } + return conn +} + let userId = 1; const authOptions: AuthOptions = { @@ -26,34 +72,7 @@ const authOptions: AuthOptions = { const id = userId; userId += 1; - const client = await createClient({ - socket: { - host: credentials.host ?? "localhost", - port: credentials.port ? parseInt(credentials.port, 10) : 6379, - reconnectStrategy: false - }, - password: credentials.password ?? undefined, - username: credentials.username ?? undefined - }) - - // Save connection in connections map for later use - connections.set(id, client as RedisClientType) - - await client.on('error', err => { - // Close coonection on error and remove from connections map - console.error('FalkorDB Client Error', err) - const connection = connections.get(id) - if (connection) { - connections.delete(id) - connection.disconnect() - .catch((e) => { - console.warn('FalkorDB Client Disconnect Error', e) - }) - } - }).connect(); - - // Verify connection - await client.ping() + await newClient(credentials, id) const res = { id, diff --git a/app/api/graph/[graph]/[node]/route.ts b/app/api/graph/[graph]/[node]/route.ts index 921e4d4..056aa7e 100644 --- a/app/api/graph/[graph]/[node]/route.ts +++ b/app/api/graph/[graph]/[node]/route.ts @@ -1,7 +1,7 @@ import { NextRequest, NextResponse } from "next/server"; import { Graph } from 'falkordb'; import { getServerSession } from "next-auth/next"; -import authOptions, { connections } from "../../../auth/[...nextauth]/options"; +import authOptions, { getConnection } from "../../../auth/[...nextauth]/options"; // eslint-disable-next-line import/prefer-default-export export async function GET(request: NextRequest, { params }: { params: { graph: string, node: string } }) { @@ -12,7 +12,7 @@ export async function GET(request: NextRequest, { params }: { params: { graph: s return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) } - const client = connections.get(id) + const client = await getConnection(session.user) if (!client) { return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) } diff --git a/app/api/graph/route.ts b/app/api/graph/route.ts index 37d917a..aee459b 100644 --- a/app/api/graph/route.ts +++ b/app/api/graph/route.ts @@ -1,7 +1,7 @@ import { NextRequest, NextResponse } from "next/server"; import { Graph } from 'falkordb'; import { getServerSession } from "next-auth/next"; -import authOptions, { connections } from "../auth/[...nextauth]/options"; +import authOptions, { getConnection } from "../auth/[...nextauth]/options"; // eslint-disable-next-line import/prefer-default-export export async function GET(request: NextRequest) { @@ -12,7 +12,7 @@ export async function GET(request: NextRequest) { return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) } - const client = connections.get(id) + const client = await getConnection(session.user) if(!client) { return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) } diff --git a/app/api/monitor/route.ts b/app/api/monitor/route.ts new file mode 100644 index 0000000..05df63b --- /dev/null +++ b/app/api/monitor/route.ts @@ -0,0 +1,29 @@ +import { NextResponse } from "next/server"; +import { getServerSession } from "next-auth/next"; +import authOptions, { getConnection } from "../auth/[...nextauth]/options"; + +// eslint-disable-next-line import/prefer-default-export +export async function GET() { + + const session = await getServerSession(authOptions) + const id = session?.user?.id + if (!id) { + return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) + } + + const client = await getConnection(session.user) + if (!client) { + return NextResponse.json({ message: "Not authenticated" }, { status: 401 }) + } + + const data = (await client.info("memory")).split('\r\n').map((item) => { + const name = item.split(':')[0] + const num = item.split(':')[1] + return { name, series: num } + }) + + data.splice(0, 1) + + return NextResponse.json(data, { status: 200 }) + +}