Skip to content

Commit

Permalink
Merge pull request ChatGPTNextWeb#5190 from ConnectAI-E/feaure/hmac
Browse files Browse the repository at this point in the history
reduce cloudflare functions build size
  • Loading branch information
Dogtiti authored Aug 5, 2024
2 parents 764c0cb + 141ce2c commit aa40015
Show file tree
Hide file tree
Showing 15 changed files with 332 additions and 218 deletions.
64 changes: 64 additions & 0 deletions app/api/[provider]/[...path]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { ApiPath } from "@/app/constant";
import { NextRequest, NextResponse } from "next/server";
import { handle as openaiHandler } from "../../openai";
import { handle as azureHandler } from "../../azure";
import { handle as googleHandler } from "../../google";
import { handle as anthropicHandler } from "../../anthropic";
import { handle as baiduHandler } from "../../baidu";
import { handle as bytedanceHandler } from "../../bytedance";
import { handle as alibabaHandler } from "../../alibaba";
import { handle as moonshotHandler } from "../../moonshot";
import { handle as stabilityHandler } from "../../stability";

async function handle(
req: NextRequest,
{ params }: { params: { provider: string; path: string[] } },
) {
const apiPath = `/api/${params.provider}`;
console.log(`[${params.provider} Route] params `, params);
switch (apiPath) {
case ApiPath.Azure:
return azureHandler(req, { params });
case ApiPath.Google:
return googleHandler(req, { params });
case ApiPath.Anthropic:
return anthropicHandler(req, { params });
case ApiPath.Baidu:
return baiduHandler(req, { params });
case ApiPath.ByteDance:
return bytedanceHandler(req, { params });
case ApiPath.Alibaba:
return alibabaHandler(req, { params });
// case ApiPath.Tencent: using "/api/tencent"
case ApiPath.Moonshot:
return moonshotHandler(req, { params });
case ApiPath.Stability:
return stabilityHandler(req, { params });
default:
return openaiHandler(req, { params });
}
}

export const GET = handle;
export const POST = handle;

export const runtime = "edge";
export const preferredRegion = [
"arn1",
"bom1",
"cdg1",
"cle1",
"cpt1",
"dub1",
"fra1",
"gru1",
"hnd1",
"iad1",
"icn1",
"kix1",
"lhr1",
"pdx1",
"sfo1",
"sin1",
"syd1",
];
26 changes: 1 addition & 25 deletions app/api/alibaba/[...path]/route.ts → app/api/alibaba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { RequestPayload } from "@/app/client/platforms/openai";

const serverConfig = getServerSideConfig();

async function handle(
export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
Expand All @@ -40,30 +40,6 @@ async function handle(
}
}

export const GET = handle;
export const POST = handle;

export const runtime = "edge";
export const preferredRegion = [
"arn1",
"bom1",
"cdg1",
"cle1",
"cpt1",
"dub1",
"fra1",
"gru1",
"hnd1",
"iad1",
"icn1",
"kix1",
"lhr1",
"pdx1",
"sfo1",
"sin1",
"syd1",
];

async function request(req: NextRequest) {
const controller = new AbortController();

Expand Down
28 changes: 2 additions & 26 deletions app/api/anthropic/[...path]/route.ts → app/api/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {
} from "@/app/constant";
import { prettyObject } from "@/app/utils/format";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
import { auth } from "./auth";
import { isModelAvailableInServer } from "@/app/utils/model";
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";

const ALLOWD_PATH = new Set([Anthropic.ChatPath, Anthropic.ChatPath1]);

async function handle(
export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
Expand Down Expand Up @@ -56,30 +56,6 @@ async function handle(
}
}

export const GET = handle;
export const POST = handle;

export const runtime = "edge";
export const preferredRegion = [
"arn1",
"bom1",
"cdg1",
"cle1",
"cpt1",
"dub1",
"fra1",
"gru1",
"hnd1",
"iad1",
"icn1",
"kix1",
"lhr1",
"pdx1",
"sfo1",
"sin1",
"syd1",
];

const serverConfig = getServerSideConfig();

async function request(req: NextRequest) {
Expand Down
30 changes: 3 additions & 27 deletions app/api/azure/[...path]/route.ts → app/api/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { getServerSideConfig } from "@/app/config/server";
import { ModelProvider } from "@/app/constant";
import { prettyObject } from "@/app/utils/format";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
import { requestOpenai } from "../../common";
import { auth } from "./auth";
import { requestOpenai } from "./common";

async function handle(
export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
Expand All @@ -31,27 +31,3 @@ async function handle(
return NextResponse.json(prettyObject(e));
}
}

export const GET = handle;
export const POST = handle;

export const runtime = "edge";
export const preferredRegion = [
"arn1",
"bom1",
"cdg1",
"cle1",
"cpt1",
"dub1",
"fra1",
"gru1",
"hnd1",
"iad1",
"icn1",
"kix1",
"lhr1",
"pdx1",
"sfo1",
"sin1",
"syd1",
];
26 changes: 1 addition & 25 deletions app/api/baidu/[...path]/route.ts → app/api/baidu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getAccessToken } from "@/app/utils/baidu";

const serverConfig = getServerSideConfig();

async function handle(
export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
Expand Down Expand Up @@ -52,30 +52,6 @@ async function handle(
}
}

export const GET = handle;
export const POST = handle;

export const runtime = "edge";
export const preferredRegion = [
"arn1",
"bom1",
"cdg1",
"cle1",
"cpt1",
"dub1",
"fra1",
"gru1",
"hnd1",
"iad1",
"icn1",
"kix1",
"lhr1",
"pdx1",
"sfo1",
"sin1",
"syd1",
];

async function request(req: NextRequest) {
const controller = new AbortController();

Expand Down
26 changes: 1 addition & 25 deletions app/api/bytedance/[...path]/route.ts → app/api/bytedance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { isModelAvailableInServer } from "@/app/utils/model";

const serverConfig = getServerSideConfig();

async function handle(
export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
Expand All @@ -38,30 +38,6 @@ async function handle(
}
}

export const GET = handle;
export const POST = handle;

export const runtime = "edge";
export const preferredRegion = [
"arn1",
"bom1",
"cdg1",
"cle1",
"cpt1",
"dub1",
"fra1",
"gru1",
"hnd1",
"iad1",
"icn1",
"kix1",
"lhr1",
"pdx1",
"sfo1",
"sin1",
"syd1",
];

async function request(req: NextRequest) {
const controller = new AbortController();

Expand Down
6 changes: 3 additions & 3 deletions app/api/google/[...path]/route.ts → app/api/google.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
import { auth } from "./auth";
import { getServerSideConfig } from "@/app/config/server";
import {
ApiPath,
Expand All @@ -11,9 +11,9 @@ import { prettyObject } from "@/app/utils/format";

const serverConfig = getServerSideConfig();

async function handle(
export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
{ params }: { params: { provider: string; path: string[] } },
) {
console.log("[Google Route] params ", params);

Expand Down
26 changes: 1 addition & 25 deletions app/api/moonshot/[...path]/route.ts → app/api/moonshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { RequestPayload } from "@/app/client/platforms/openai";

const serverConfig = getServerSideConfig();

async function handle(
export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
Expand All @@ -40,30 +40,6 @@ async function handle(
}
}

export const GET = handle;
export const POST = handle;

export const runtime = "edge";
export const preferredRegion = [
"arn1",
"bom1",
"cdg1",
"cle1",
"cpt1",
"dub1",
"fra1",
"gru1",
"hnd1",
"iad1",
"icn1",
"kix1",
"lhr1",
"pdx1",
"sfo1",
"sin1",
"syd1",
];

async function request(req: NextRequest) {
const controller = new AbortController();

Expand Down
30 changes: 3 additions & 27 deletions app/api/openai/[...path]/route.ts → app/api/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { getServerSideConfig } from "@/app/config/server";
import { ModelProvider, OpenaiPath } from "@/app/constant";
import { prettyObject } from "@/app/utils/format";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
import { requestOpenai } from "../../common";
import { auth } from "./auth";
import { requestOpenai } from "./common";

const ALLOWD_PATH = new Set(Object.values(OpenaiPath));

Expand All @@ -20,7 +20,7 @@ function getModels(remoteModelRes: OpenAIListModelResponse) {
return remoteModelRes;
}

async function handle(
export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
Expand Down Expand Up @@ -70,27 +70,3 @@ async function handle(
return NextResponse.json(prettyObject(e));
}
}

export const GET = handle;
export const POST = handle;

export const runtime = "edge";
export const preferredRegion = [
"arn1",
"bom1",
"cdg1",
"cle1",
"cpt1",
"dub1",
"fra1",
"gru1",
"hnd1",
"iad1",
"icn1",
"kix1",
"lhr1",
"pdx1",
"sfo1",
"sin1",
"syd1",
];
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getServerSideConfig } from "@/app/config/server";
import { ModelProvider, STABILITY_BASE_URL } from "@/app/constant";
import { auth } from "@/app/api/auth";

async function handle(
export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
Expand Down Expand Up @@ -97,8 +97,3 @@ async function handle(
clearTimeout(timeoutId);
}
}

export const GET = handle;
export const POST = handle;

export const runtime = "edge";
Loading

0 comments on commit aa40015

Please sign in to comment.