origin and ws abstractions

This commit is contained in:
2026-02-15 00:04:28 +01:00
parent 371186c277
commit ac530ab6cf
5 changed files with 28 additions and 17 deletions
+2 -2
View File
@@ -5,7 +5,7 @@ import { getOrigin } from "@/lib/origin/server";
export async function GET(req: NextRequest) {
const code = req.nextUrl.searchParams.get("code");
const { protocol, host } = await getOrigin();
const { httpOrigin } = await getOrigin();
if (!code) {
return NextResponse.json({ error: "code param required" }, { status: 400 });
@@ -20,5 +20,5 @@ export async function GET(req: NextRequest) {
return NextResponse.json({ error: error.message }, { status: 500 });
}
return NextResponse.redirect(`${protocol}://${host}/problems`);
return NextResponse.redirect(`${httpOrigin}/problems`);
}
+3 -3
View File
@@ -56,9 +56,9 @@ export default function Editor({
// WebSocket connection
useEffect(() => {
if (!run || !origin?.host) return;
if (!run || !origin?.wsOrigin) return;
const ws = new WebSocket(`ws://${origin.host}/api/view-run`);
const ws = new WebSocket(`${origin.wsOrigin}/api/view-run`);
ws.onopen = () => {
ws.send(run.id);
@@ -100,7 +100,7 @@ export default function Editor({
};
return () => ws.close();
}, [run, ]);
}, [run]);
return (
<CodeMirror
+3 -7
View File
@@ -1,11 +1,7 @@
"use server";
import { headers } from "next/headers";
import * as Core from "./server";
export async function getOrigin(): Promise<{ protocol: string; host: string }> {
const h = await headers();
const host = process.env.ORIGIN || h.get(":authority") || "localhost:3000";
const protocol = h.get("x-forwarded-proto") || h.get(":scheme") || "https";
return { protocol, host };
export async function getOrigin() {
return Core.getOrigin();
}
+18 -3
View File
@@ -1,7 +1,14 @@
import { headers } from "next/headers";
import "server-only";
export async function getOrigin(): Promise<{ protocol: string; host: string }> {
import { headers } from "next/headers";
export async function getOrigin(): Promise<{
protocol: string;
host: string;
wsProtocol: string;
httpOrigin: string;
wsOrigin: string;
}> {
const h = await headers();
const host = process.env.ORIGIN || h.get(":authority") || "localhost:3000";
const protocol =
@@ -10,5 +17,13 @@ export async function getOrigin(): Promise<{ protocol: string; host: string }> {
h.get(":scheme") ||
"https";
return { protocol, host };
const wsProtocol = protocol === "https" ? "wss" : "ws";
return {
protocol,
host,
wsProtocol,
httpOrigin: `${protocol}://${host}`,
wsOrigin: `${wsProtocol}://${host}`,
};
}
+2 -2
View File
@@ -8,12 +8,12 @@ import { redirect } from "next/navigation";
export async function signInWithGitHub() {
const supabase = await createClient();
const { host, protocol } = await getOrigin();
const { httpOrigin } = await getOrigin();
const { data, error } = await supabase.auth.signInWithOAuth({
provider: "github",
options: {
redirectTo: `${protocol}://${host}/api/auth/callback`,
redirectTo: `${httpOrigin}/api/auth/callback`,
},
});