Browse Source

feat: #1640 support free gpt endpoint

Yidadaa 1 year ago
parent
commit
203ac0970d
3 changed files with 7 additions and 4 deletions
  1. 2 1
      app/api/auth.ts
  2. 3 3
      app/api/common.ts
  3. 2 0
      app/config/server.ts

+ 2 - 1
app/api/auth.ts

@@ -2,6 +2,7 @@ import { NextRequest } from "next/server";
 import { getServerSideConfig } from "../config/server";
 import md5 from "spark-md5";
 import { ACCESS_CODE_PREFIX } from "../constant";
+import { OPENAI_URL } from "./common";
 
 function getIP(req: NextRequest) {
   let ip = req.ip ?? req.headers.get("x-real-ip");
@@ -55,7 +56,7 @@ export function auth(req: NextRequest) {
     } else {
       console.log("[Auth] admin did not provide an api key");
       return {
-        error: true,
+        error: serverConfig.baseUrl?.includes(OPENAI_URL),
         msg: "admin did not provide an api key",
       };
     }

+ 3 - 3
app/api/common.ts

@@ -1,6 +1,6 @@
 import { NextRequest } from "next/server";
 
-const OPENAI_URL = "api.openai.com";
+export const OPENAI_URL = "api.openai.com";
 const DEFAULT_PROTOCOL = "https";
 const PROTOCOL = process.env.PROTOCOL ?? DEFAULT_PROTOCOL;
 const BASE_URL = process.env.BASE_URL ?? OPENAI_URL;
@@ -45,8 +45,8 @@ export async function requestOpenai(req: NextRequest) {
       signal: controller.signal,
     });
   } catch (err: unknown) {
-    if (err instanceof Error && err.name === 'AbortError') {
-      console.log('Fetch aborted');
+    if (err instanceof Error && err.name === "AbortError") {
+      console.log("Fetch aborted");
     } else {
       throw err;
     }

+ 2 - 0
app/config/server.ts

@@ -5,6 +5,7 @@ declare global {
     interface ProcessEnv {
       OPENAI_API_KEY?: string;
       CODE?: string;
+      BASE_URL?: string;
       PROXY_URL?: string;
       VERCEL?: string;
       HIDE_USER_API_KEY?: string; // disable user's api key input
@@ -38,6 +39,7 @@ export const getServerSideConfig = () => {
     code: process.env.CODE,
     codes: ACCESS_CODES,
     needCode: ACCESS_CODES.size > 0,
+    baseUrl: process.env.BASE_URL,
     proxyUrl: process.env.PROXY_URL,
     isVercel: !!process.env.VERCEL,
     hideUserApiKey: !!process.env.HIDE_USER_API_KEY,