parent
7aee53ea05
commit
9b61cb1335
@ -1,17 +0,0 @@
|
||||
import md5 from "spark-md5";
|
||||
|
||||
export 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 ACCESS_CODES = getAccessCodes();
|
||||
export const IS_IN_DOCKER = process.env.DOCKER;
|
@ -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,
|
||||
};
|
||||
};
|
@ -0,0 +1,42 @@
|
||||
import { RUNTIME_CONFIG_DOM } from "../constant";
|
||||
|
||||
function queryMeta(key: string, defaultValue?: string): string {
|
||||
let ret: string;
|
||||
if (document) {
|
||||
const meta = document.head.querySelector(
|
||||
`meta[name='${key}']`,
|
||||
) as HTMLMetaElement;
|
||||
ret = meta?.content ?? "";
|
||||
} else {
|
||||
ret = defaultValue ?? "";
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
export function getClientSideConfig() {
|
||||
if (typeof window === "undefined") {
|
||||
throw Error(
|
||||
"[Client Config] you are importing a browser-only module outside of browser",
|
||||
);
|
||||
}
|
||||
|
||||
const dom = document.getElementById(RUNTIME_CONFIG_DOM);
|
||||
|
||||
if (!dom) {
|
||||
throw Error("[Config] Dont get config before page loading!");
|
||||
}
|
||||
|
||||
try {
|
||||
const fromServerConfig = JSON.parse(dom.innerText) as DangerConfig;
|
||||
const fromBuildConfig = {
|
||||
version: queryMeta("version"),
|
||||
};
|
||||
return {
|
||||
...fromServerConfig,
|
||||
...fromBuildConfig,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("[Config] failed to parse client config");
|
||||
}
|
||||
}
|
@ -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,
|
||||
};
|
||||
};
|
@ -1,12 +1,29 @@
|
||||
import { Analytics } from "@vercel/analytics/react";
|
||||
|
||||
import { Home } from "./components/home";
|
||||
import { getServerSideConfig } from "./config/server";
|
||||
import { RUNTIME_CONFIG_DOM } from "./constant";
|
||||
|
||||
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 default function App() {
|
||||
return (
|
||||
<>
|
||||
<div style={{ display: "none" }} id={RUNTIME_CONFIG_DOM}>
|
||||
{JSON.stringify(DANGER_CONFIG)}
|
||||
</div>
|
||||
<Home />
|
||||
<Analytics />
|
||||
{serverConfig?.isVercel && <Analytics />}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in new issue