Skip to content

Commit

Permalink
fix: 由于网站 URL 填写格式错误导致的老版本升级后容器无限重启#27
Browse files Browse the repository at this point in the history
  • Loading branch information
Mereithhh committed Sep 4, 2022
1 parent 592b5ef commit 4c33704
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 83 deletions.
2 changes: 1 addition & 1 deletion docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
restart: always
environment:
TZ: "Asia/Shanghai"
# 图片资源允许的域名,英文逗号分隔。作者 logo 加载不出来请检查此项。
# 图片资源允许的域名,英文逗号分隔。作者 logo 加载不出来请检查此项。不要带协议!
VAN_BLOG_ALLOW_DOMAINS: "www.mereith.com"
# CDN URL,包含协议,部署到 cdn 的时候用。在开启 cdn 之前请不要设置此项。
# VAN_BLOG_CDN_URL: "https://www.mereith.com"
Expand Down
4 changes: 3 additions & 1 deletion docs/guide/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ services:
restart: always
environment:
TZ: "Asia/Shanghai"
# 图片资源允许的域名,英文逗号分隔。作者 logo 加载不出来请检查此项。
# 图片资源允许的域名,英文逗号分隔。作者 logo 加载不出来请检查此项。不要带协议!
VAN_BLOG_ALLOW_DOMAINS: "www.mereith.com"
# CDN URL,包含协议,部署到 cdn 的时候用。在开启 cdn 之前请不要设置此项。
# VAN_BLOG_CDN_URL: "https://www.mereith.com"
Expand Down Expand Up @@ -104,6 +104,8 @@ services:
sudo docker-compose up -d
```

PS: 请检查 `VAN_BLOG_ALLOW_DOMAINS` 变量是否正确,否则作者头像可能无法正常显示。

浏览器打开 `http://<你的域名>/admin/init` ,并按照提示初始化即可。具体设置项可以参考 [站点配置](/feature/basic/setting.md)

也可以在前台点击右上角管理员按钮即可进入后台初始化页面。
Expand Down
8 changes: 8 additions & 0 deletions docs/guide/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ docker run -d \

![](https://www.mereith.com/static/img/5efb32214a31c1003df5eeba217a5586.clipboard-2022-09-03.png)

## 容器无限重启

有时候作者抽风或者由于奇怪的问题可能会发生容器无限重启现象,遇到这种问题您可以先参考下面进行版本回滚。(如果您不知道版本,可以去看更新日志)

有能力的同学可以记录一下无限重启的容器日志,提一个 issue 或者联系我,十分感谢!

PS:因为目前只有我一个人开发,所以很多情况测试覆盖不到。如给您造成困扰,十分抱歉!

## 如何回滚

您可以通过指定镜像的版本号来实现,比如您想回滚到 `v0.29.0`,那您可以修改编排中的:
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/baota.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ services:
restart: always
environment:
TZ: "Asia/Shanghai"
# 图片资源允许的域名,英文逗号分隔。作者 logo 加载不出来请检查此项。
# 图片资源允许的域名,英文逗号分隔。作者 logo 加载不出来请检查此项。不要带协议!
VAN_BLOG_ALLOW_DOMAINS: "www.mereith.com"
# CDN URL,包含协议,部署到 cdn 的时候用。在开启 cdn 之前请不要设置此项。
# VAN_BLOG_CDN_URL: "https://www.mereith.com"
Expand Down
3 changes: 2 additions & 1 deletion packages/admin/src/components/SiteInfoForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ export default function (props: {
name="baseUrl"
rules={[{ required: true, message: '这是必填项' }]}
label="网站 Url"
placeholder={'请输入网站 Url'}
placeholder={'请输入包含访问协议的完整 URL'}
tooltip={'请输入包含访问协议的完整 URL,此 URL 会被用来生成前后台/RSS的相关数据。'}
required={true}
/>
)}
Expand Down
27 changes: 26 additions & 1 deletion packages/admin/src/pages/InitPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,32 @@ const InitPage = () => {
placeholder={'请输入登录密码'}
></ProFormText.Password>
</StepsForm.StepForm>
<StepsForm.StepForm name="step2" title={'基本配置'} formRef={formRef1}>
<StepsForm.StepForm
name="step2"
title={'基本配置'}
formRef={formRef1}
onFinish={async (values) => {
let ok = true;
try {
new URL(values.baseUrl);
} catch (err) {
ok = false;
}
if (!ok) {
Modal.warn({
title: '网站 URL 不合法!',
content: (
<div>
<p>请输入包含完整协议的 URL</p>
<p>例: https://blog-demo.mereith.com</p>
</div>
),
});
return false;
}
return true;
}}
>
<Alert
type="info"
message="默认的上传图片会到内置图床,如需配置 oss 图床,可在初始化后去设置页更改。初始化页面所有配置都可在初始化后进入后台修改。"
Expand Down
18 changes: 18 additions & 0 deletions packages/admin/src/pages/SystemConfig/tabs/SiteInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ export default function () {
}}
syncToInitialValues={true}
onFinish={async (data) => {
let ok = true;
try {
new URL(data.baseUrl);
} catch (err) {
ok = false;
}
if (!ok) {
Modal.warn({
title: '网站 URL 不合法!',
content: (
<div>
<p>请输入包含完整协议的 URL</p>
<p>例: https://blog-demo.mereith.com</p>
</div>
),
});
return;
}
if (location.hostname == 'blog-demo.mereith.com') {
Modal.info({ title: '演示站禁止修改站点配置!' });
return;
Expand Down
160 changes: 83 additions & 77 deletions packages/server/src/provider/rss/rss.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fs from 'fs';
import path from 'path';
import { config } from 'src/config';
import { MarkdownProvider } from '../markdown/markdown.provider';
import { washUrl } from 'src/utils/washUrl';

@Injectable()
export class RssProvider {
Expand All @@ -21,65 +22,66 @@ export class RssProvider {

async generateRssFeed(info?: string) {
this.logger.log(info + '重新生成 RSS 订阅');
let articles = await this.articleProvider.getAll('public', false, false);
articles = articles.map((a: any) => {
const article = a?._doc || a;
if (article.private) {
return { ...article, content: '此文章已加密' };
} else {
return article;
try {
let articles = await this.articleProvider.getAll('public', false, false);
articles = articles.map((a: any) => {
const article = a?._doc || a;
if (article.private) {
return { ...article, content: '此文章已加密' };
} else {
return article;
}
});
const meta = await this.metaProvider.getAll();
const walineSetting = await this.settingProvider.getWalineSetting();
let email = process.env.EMAIL;
if (walineSetting && walineSetting?.authorEmail) {
email = walineSetting?.authorEmail;
}
});
const meta = await this.metaProvider.getAll();
const walineSetting = await this.settingProvider.getWalineSetting();
let email = process.env.EMAIL;
if (walineSetting && walineSetting?.authorEmail) {
email = walineSetting?.authorEmail;
}
walineSetting?.authorEmail;
const author = {
name: meta.siteInfo.author,
email,
link: meta.siteInfo.baseUrl,
};
const siteUrl = new URL(meta.siteInfo.baseUrl).toString();
const favicon =
meta.siteInfo.favicon ||
meta.siteInfo.siteLogo ||
meta.siteInfo.authorLogo ||
`${siteUrl}logo.svg`;
const siteLogo =
meta.siteInfo.siteLogo ||
meta.siteInfo.authorLogo ||
meta.siteInfo.favicon ||
`${siteUrl}logo.svg`;
const date = new Date();
const feed = new Feed({
title: meta.siteInfo.siteName,
description: meta.siteInfo.siteDesc,
id: siteUrl,
link: siteUrl,
language: ' zh-cn',
image: siteLogo,
favicon: favicon,
copyright: `All rights reserved ${date.getFullYear()}, ${
meta.siteInfo.author
}`,
updated: date,
generator: 'Feed for VanBlog',
feedLinks: {
rss2: `${siteUrl}rss/feed.xml`, // xml format
json: `${siteUrl}rss/feed.json`, // json fromat
},
author,
});
for (const article of articles) {
const url = `${siteUrl}post/${article.id}`;
const category = {
name: article.category,
domain: `${siteUrl}/category/${article.category}`,
walineSetting?.authorEmail;
const author = {
name: meta.siteInfo.author,
email,
link: meta.siteInfo.baseUrl,
};
const html = `<div class="markdown-body rss">
const siteUrl = washUrl(meta.siteInfo.baseUrl);
const favicon =
meta.siteInfo.favicon ||
meta.siteInfo.siteLogo ||
meta.siteInfo.authorLogo ||
`${siteUrl}logo.svg`;
const siteLogo =
meta.siteInfo.siteLogo ||
meta.siteInfo.authorLogo ||
meta.siteInfo.favicon ||
`${siteUrl}logo.svg`;
const date = new Date();
const feed = new Feed({
title: meta.siteInfo.siteName,
description: meta.siteInfo.siteDesc,
id: siteUrl,
link: siteUrl,
language: ' zh-cn',
image: siteLogo,
favicon: favicon,
copyright: `All rights reserved ${date.getFullYear()}, ${
meta.siteInfo.author
}`,
updated: date,
generator: 'Feed for VanBlog',
feedLinks: {
rss2: `${siteUrl}rss/feed.xml`, // xml format
json: `${siteUrl}rss/feed.json`, // json fromat
},
author,
});
for (const article of articles) {
const url = `${siteUrl}post/${article.id}`;
const category = {
name: article.category,
domain: `${siteUrl}/category/${article.category}`,
};
const html = `<div class="markdown-body rss">
<link rel="stylesheet" href="${siteUrl}markdown.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/katex.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.6.0/build/styles/default.min.css">
Expand All @@ -89,26 +91,30 @@ export class RssProvider {
/<div class="mermaid">/g,
`<div class="mermaid" style="background: #f3f3f3; padding: 8px;"> <p>Mermaid 图表 RSS 暂无法显示,具体请查看原文</p>`,
)}</div>`;
feed.addItem({
title: article.title,
id: url,
link: url,
description: this.markdownProvider.renderMarkdown(
this.markdownProvider.getDescription(article.content),
),
category: [category],
content: html,
author: [author],
contributor: [author],
date: new Date(article.createdAt),
published: new Date(article.updatedAt || article.createdAt),
});
}
const rssPath = path.join(config.staticPath, 'rss');
feed.addItem({
title: article.title,
id: url,
link: url,
description: this.markdownProvider.renderMarkdown(
this.markdownProvider.getDescription(article.content),
),
category: [category],
content: html,
author: [author],
contributor: [author],
date: new Date(article.createdAt),
published: new Date(article.updatedAt || article.createdAt),
});
}
const rssPath = path.join(config.staticPath, 'rss');

fs.mkdirSync(rssPath, { recursive: true });
fs.writeFileSync(path.join(rssPath, 'feed.json'), feed.json1());
fs.writeFileSync(path.join(rssPath, 'feed.xml'), feed.rss2());
fs.writeFileSync(path.join(rssPath, 'atom.xml'), feed.atom1());
fs.mkdirSync(rssPath, { recursive: true });
fs.writeFileSync(path.join(rssPath, 'feed.json'), feed.json1());
fs.writeFileSync(path.join(rssPath, 'feed.xml'), feed.rss2());
fs.writeFileSync(path.join(rssPath, 'atom.xml'), feed.atom1());
} catch (err) {
this.logger.error('生成订阅源失败!');
this.logger.error(JSON.stringify(err, null, 2));
}
}
}
2 changes: 1 addition & 1 deletion packages/server/src/utils/checkFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export const checkOrCreate = (p: string) => {
fs.readdirSync(p);
} catch (err) {
console.log(`${p}不存在,创建。`);
fs.mkdirSync(p);
fs.mkdirSync(p, { recursive: true });
}
};
14 changes: 14 additions & 0 deletions packages/server/src/utils/washUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function washUrl(s: string) {
// 先判断一下带不带协议
let url = s;
if (!s.includes('http')) {
url = `https://${s}`;
}
// 带反斜杠的
try {
const u = new URL(url);
return u.toString();
} catch (err) {
return url;
}
}

0 comments on commit 4c33704

Please sign in to comment.