-
Notifications
You must be signed in to change notification settings - Fork 59.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #697 from Yidadaa/bugfix-0410
fix: runtime config and proxy fix
- Loading branch information
Showing
17 changed files
with
176 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
import { getServerSideConfig } from "../../config/server"; | ||
|
||
const serverConfig = getServerSideConfig(); | ||
|
||
// Danger! Don not write any secret value here! | ||
// 警告!不要在这里写入任何敏感信息! | ||
const DANGER_CONFIG = { | ||
needCode: serverConfig.needCode, | ||
}; | ||
|
||
declare global { | ||
type DangerConfig = typeof DANGER_CONFIG; | ||
} | ||
|
||
export async function POST(req: NextRequest) { | ||
return NextResponse.json({ | ||
needCode: serverConfig.needCode, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const COMMIT_ID: string = (() => { | ||
try { | ||
const childProcess = require("child_process"); | ||
return ( | ||
childProcess | ||
// .execSync("git describe --tags --abbrev=0") | ||
.execSync("git rev-parse --short HEAD") | ||
.toString() | ||
.trim() | ||
); | ||
} catch (e) { | ||
console.error("[Build Config] No git or not from git repo."); | ||
return "unknown"; | ||
} | ||
})(); | ||
|
||
export const getBuildConfig = () => { | ||
if (typeof process === "undefined") { | ||
throw Error( | ||
"[Server Config] you are importing a nodejs-only module outside of nodejs", | ||
); | ||
} | ||
|
||
return { | ||
commitId: COMMIT_ID, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import md5 from "spark-md5"; | ||
|
||
declare global { | ||
namespace NodeJS { | ||
interface ProcessEnv { | ||
OPENAI_API_KEY?: string; | ||
CODE?: string; | ||
PROXY_URL?: string; | ||
VERCEL?: string; | ||
} | ||
} | ||
} | ||
|
||
const ACCESS_CODES = (function getAccessCodes(): Set<string> { | ||
const code = process.env.CODE; | ||
|
||
try { | ||
const codes = (code?.split(",") ?? []) | ||
.filter((v) => !!v) | ||
.map((v) => md5.hash(v.trim())); | ||
return new Set(codes); | ||
} catch (e) { | ||
return new Set(); | ||
} | ||
})(); | ||
|
||
export const getServerSideConfig = () => { | ||
if (typeof process === "undefined") { | ||
throw Error( | ||
"[Server Config] you are importing a nodejs-only module outside of nodejs", | ||
); | ||
} | ||
|
||
return { | ||
apiKey: process.env.OPENAI_API_KEY, | ||
code: process.env.CODE, | ||
codes: ACCESS_CODES, | ||
needCode: ACCESS_CODES.size > 0, | ||
proxyUrl: process.env.PROXY_URL, | ||
isVercel: !!process.env.VERCEL, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.