Skip to content

Commit

Permalink
build: Optimize environment judgment logic
Browse files Browse the repository at this point in the history
doc: Add screenshot image
  • Loading branch information
Amery2010 committed May 3, 2024
1 parent a2c8fe6 commit c3eb476
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

# production
/build
/release

# misc
.DS_Store
Expand Down
6 changes: 0 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

ENV NEXT_PUBLIC_ENABLE_PROTECT="1"
ENV ACCESS_PASSWORD=""
ENV GEMINI_API_KEY=""
ENV GEMINI_API_BASE_URL=""
ENV HEAD_SCRIPTS=""

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
Expand Down
63 changes: 36 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Deploy your private Gemini application for free with one click, supporting Gemin

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Amery2010/TalkWithGemini)

![cover](./docs/images/cover.png)
![cover](./docs/images/screenshot.jpg)

> [!NOTE]
>
Expand Down Expand Up @@ -213,29 +213,24 @@ pnpm dev
> ⚠️ Note: Most of the time, the docker version will lag behind the latest version by 1 to 2 days, so the "update exists" prompt will continue to appear after deployment, which is normal.
```shell
docker pull xiangfa/talk-with-gemini
docker pull xiangfa/talk-with-gemini:latest

docker run -d -p 3000:3000 \
-e OPENAI_API_KEY=sk-xxxx \
-e CODE=your-password \
xiangfa/talk-with-gemini
docker run -d -p 3000:3000 xiangfa/talk-with-gemini
```

You can start service behind a proxy:
You can also specify additional environment variables:

```shell
docker run -d -p 3000:3000 \
-e OPENAI_API_KEY=sk-xxxx \
-e CODE=your-password \
-e PROXY_URL=http://localhost:7890 \
-e GEMINI_API_KEY=AIza... \
-e ACCESS_PASSWORD=your-password \
-e GEMINI_API_BASE_URL=http://localhost:7890 \
xiangfa/talk-with-gemini
```

If your proxy needs password, use:
If you need to specify other environment variables, please add `-e key=value` to the above command to specify it.

```shell
-e PROXY_URL="http://127.0.0.1:7890 user pass"
```
## 部署

### 容器部署(推荐)

Expand All @@ -244,32 +239,46 @@ If your proxy needs password, use:
> ⚠️ 注意:docker 版本在大多数时间都会落后最新的版本 1 到 2 天,所以部署后会持续出现“存在更新”的提示,属于正常现象。
```shell
docker pull xiangfa/talk-with-gemini
docker pull xiangfa/talk-with-gemini:latest

docker run -d -p 3000:3000 \
-e OPENAI_API_KEY=sk-xxxx \
-e CODE=页面访问密码 \
xiangfa/talk-with-gemini
docker run -d -p 3000:3000 xiangfa/talk-with-gemini
```

您也可以指定 proxy
您也可以指定额外的环境变量

```shell
docker run -d -p 3000:3000 \
-e OPENAI_API_KEY=sk-xxxx \
-e CODE=页面访问密码 \
--net=host \
-e PROXY_URL=http://127.0.0.1:7890 \
-e GEMINI_API_KEY=AIza... \
-e ACCESS_PASSWORD=页面访问密码 \
-e GEMINI_API_BASE_URL=http://127.0.0.1:7890 \
xiangfa/talk-with-gemini
```

如果您的本地代理需要账号密码,可以使用:
如果您需要指定其他环境变量,请自行在上述命令中增加 `-e 环境变量=环境变量值` 来指定。

### static deployment

You can also build a static page version directly, and then upload all files in the `out` directory to any website service that supports static pages.

```shell
pnpm build:export
```

If you deploy the project in a subdirectory and encounter resource loading failures when accessing, please modify the `nextConfig.basePath` in the `next.config.js` file to the specific directory of your subdirectory `nextConfig.basePath = '/path/project'.

**Static deployment does not support setting environment variables**

### 静态部署

您也可以直接构建静态页面版本,然后将 `out` 目录下的所有文件上传到任何支持静态页面的网站服务。

```shell
-e PROXY_URL="http://127.0.0.1:7890 user password"
pnpm build:export
```

如果您需要指定其他环境变量,请自行在上述命令中增加 `-e 环境变量=环境变量值` 来指定。
如果您将项目部署在子目录下,在访问时会遇到资源加载失败的情况,请修改 `next.config.js` 文件中 `nextConfig.basePath` 为您子目录的具体目录 `nextConfig.basePath = '/路径/项目名称'`

**静态部署不支持设置环境变量**

## LICENSE

Expand Down
25 changes: 16 additions & 9 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import topics from '@/constant/topics'
import { customAlphabet } from 'nanoid'
import { findLast, isFunction, groupBy, pick } from 'lodash-es'

const buildMode = process.env.NEXT_PUBLIC_BUILD_MODE as string
const nanoid = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz', 8)

export default function Home() {
Expand Down Expand Up @@ -179,9 +180,13 @@ export default function Home() {
if (response.status < 400 && response.body) {
onResponse(response.body)
} else {
const { message, code } = await response.json()
if (isFunction(onError)) {
onError(message, code)
if (response.headers.get('Content-Type') === 'text/html') {
setSetingOpen(true)
} else {
const { message, code } = await response.json()
if (isFunction(onError)) {
onError(message, code)
}
}
}
}
Expand Down Expand Up @@ -318,12 +323,14 @@ export default function Home() {
}, [])

const checkAccessStatus = useCallback(() => {
const { password, apiKey } = useSettingStore.getState()
if (password !== '' || apiKey !== '') {
return true
} else {
const { isProtected, password, apiKey } = useSettingStore.getState()
const isProtectedMode = isProtected && (password === '' || apiKey === '')
const isStaticMode = buildMode === 'export' && apiKey === ''
if (isProtectedMode || isStaticMode) {
setSetingOpen(true)
return false
} else {
return true
}
}, [])

Expand Down Expand Up @@ -579,9 +586,9 @@ export default function Home() {
<div className="text-sm leading-6">
<div className="animate-pulse text-lg text-white">{statusText}</div>
{status === 'talking' ? (
<pre className="text-center text-red-300">{subtitle}</pre>
<div className="whitespace-pre-wrap text-center text-red-300">{subtitle}</div>
) : (
<div className="text-center text-green-300">{content}</div>
<div className="whitespace-pre-wrap text-center text-green-300">{content}</div>
)}
</div>
<div className="flex items-center justify-center pt-2">
Expand Down
Binary file added docs/images/screenshot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
const nextConfig = {
transpilePackages: ['crypto-js'],
}
if (process.env.NEXT_BUILD_MODE === 'export') {
if (process.env.NEXT_PUBLIC_BUILD_MODE === 'export') {
nextConfig.output = 'export'
// Only used for static deployment, the default deployment directory is the root directory
nextConfig.basePath = ''
} else if (process.env.NEXT_BUILD_MODE === 'standalone') {
} else if (process.env.NEXT_PUBLIC_BUILD_MODE === 'standalone') {
nextConfig.output = 'standalone'
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"scripts": {
"dev": "next dev",
"build": "next build",
"build:docker": "cross-env NEXT_BUILD_MODE=standalone next build",
"build:export": "cross-env NEXT_BUILD_MODE=export next build",
"build:docker": "cross-env NEXT_PUBLIC_BUILD_MODE=standalone next build",
"build:export": "cross-env NEXT_PUBLIC_BUILD_MODE=export next build",
"start": "next start",
"lint": "next lint"
},
Expand Down

0 comments on commit c3eb476

Please sign in to comment.