Skip to content

Commit

Permalink
feat: deep link API
Browse files Browse the repository at this point in the history
# Why

To integrate with the OTel GPT, it would be necessary that OTelBin
exposes an API to create deep links.

# What

Add an API under `POST /deep-link` that takes a collector config
within the request body and that outputs a deep link within the
`Location` response header and within the body (`text/plain` body).
  • Loading branch information
bripkens committed Jan 18, 2024
1 parent b76fe8f commit a81da4f
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/otelbin/src/app/deep-link/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: 2024 Dash0 Inc.
// SPDX-License-Identifier: Apache-2.0

import { type NextRequest, NextResponse } from "next/server";
import { serializeUrlState } from "~/lib/urlState/serializeUrlState";
import { editorBinding } from "~/components/monaco-editor/editorBinding";

export const runtime = "edge";

export function OPTIONS(request: NextRequest): NextResponse {
return new NextResponse(null, {
status: 200,
headers: getCorsHeaders(request),
});
}

export async function POST(request: NextRequest): Promise<NextResponse> {
const body = await request.text();
const emptyParams = new URLSearchParams();
const pathName = serializeUrlState([editorBinding], "/", emptyParams, emptyParams, {
[editorBinding.name]: body,
});
const url = new URL(pathName, request.nextUrl).toString();
return new NextResponse(url, {
status: 200,
headers: {
...getCorsHeaders(request),
Location: url,
},
});
}

function getCorsHeaders(request: NextRequest) {
return {
"Access-Control-Allow-Origin": request.headers.get("Origin") || "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
};
}

0 comments on commit a81da4f

Please sign in to comment.