Skip to content

Commit

Permalink
feat(公告): 支持远程和本地公告路径
Browse files Browse the repository at this point in the history
- 添加对远程公告地址的支持,通过HTTP请求获取公告内容
- 优化本地公告文件读取逻辑
- 更新README文档,明确公告路径的配置方式
- 调整侧边栏logo的显示逻辑,优先使用自定义logo
  • Loading branch information
kiritoko1029 committed Nov 22, 2024
1 parent f8d1692 commit 2e0d3ed
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
21 changes: 8 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@
* 添加公告功能,可解析markdown,更新公告需重启docker容器。
* 以上功能通过环境变量即可需改
## 环境变量
### (可选) 侧边栏主标题,默认是原标题
`SIDEBAR_TITLE`

### (可选) 一言接口地址
`HITOKOTO_URL`

### (可选) `SIDEBAR_TITLE`
侧边栏主标题,默认是原标题
### `HITOKOTO_URL`
(可选) 一言接口地址
默认是https://v1.hitokoto.cn/
### `HEADER_LOGO_URL`
(可选) 侧边栏logo地址,默认是原图标
### `ANNOUNCEMENT_PATH`
(可选) 公告地址,可为服务器或容器挂载的本地文件(docker允许添加`-v /path/to/local/file:/data:ro`),也可填http地址,公告通知原理是将远程公告和本地公告对比,不一样则显示圆点,点击公告按钮后存储远程公告到本地。

### (可选) 侧边栏logo地址,默认是原图标
`HEADER_LOGO_URL`

### (可选) 公告,
`ANNOUNCEMENT`

支持markdown语法,注意用双引号包裹,避免一些转义问题。后续可能会加读取文本文档实现公告功能,但是现在这样也够用。有新公告会在公告按钮显示红点,点击后即可消除。公告通知原理是将远程公告和本地公告对比,不一样则显示圆点,点击公告按钮后存储远程公告到本地。
### 相关项目
- [ChatGPTNextWeb](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web)
- [one-api](https://github.com/songquanpeng/one-api): 一站式大模型额度管理平台,支持市面上所有主流大语言模型
Expand Down
21 changes: 18 additions & 3 deletions app/api/readFile/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@ function generateHash(input: string) {
async function handler(req: any, res: any) {
// 定义文件路径
if (process.env.ANNOUNCEMENT_PATH) {
const filePath = process.env.ANNOUNCEMENT_PATH;
const data = await fs.readFile(filePath, "utf8");
return NextResponse.json({ content: data, hash: generateHash(data) });
// 使用正则表达式判断是不是URL [a-zA-z]+://[^\s]*
const rule = new RegExp("^[a-zA-z]+://[^s]*");
if (rule.test(process.env.ANNOUNCEMENT_PATH)) {
// 是URL,向这个地址请求以获取文本
const response = await fetch(process.env.ANNOUNCEMENT_PATH);
if (!response.ok) {
return NextResponse.json({
content: "获取云公告失败,请检查公告地址是否可访问",
hash: "",
});
}
const data = await response.text();
return NextResponse.json({ content: data, hash: generateHash(data) });
} else {
const filePath = process.env.ANNOUNCEMENT_PATH;
const data = await fs.readFile(filePath, "utf8");
return NextResponse.json({ content: data, hash: generateHash(data) });
}
}
return NextResponse.json({ content: "", hash: "" });
}
Expand Down
2 changes: 1 addition & 1 deletion app/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export function SideBar(props: { className?: string }) {
<SideBarHeader
title={title}
subTitle=""
logo={headerLogoUrl ?? <ChatGptIcon />}
logo={headerLogoUrl !== "" ? headerLogoUrl : <ChatGptIcon />}
shouldNarrow={shouldNarrow}
>
<div className={styles["sidebar-header-bar"]}>
Expand Down
14 changes: 1 addition & 13 deletions app/icons/bot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2e0d3ed

Please sign in to comment.