Skip to content

Commit

Permalink
feat: 优化私有部署方式
Browse files Browse the repository at this point in the history
  • Loading branch information
imaegoo committed Jul 29, 2022
1 parent 87f5961 commit 197865d
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 15 deletions.
1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ charset = utf-8
[{*.js,*.json,*.vue,*.html,*.css}]
indent_style = space
indent_size = 2

8 changes: 8 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ jobs:
run: cd src/server/vercel && yarn publish --tag beta
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
- if: "github.event.release.prerelease"
run: cd src/server/self-hosted && yarn publish --tag beta
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
- if: "!github.event.release.prerelease"
run: yarn publish
env:
Expand All @@ -43,3 +47,7 @@ jobs:
run: cd src/server/vercel && yarn publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
- if: "!github.event.release.prerelease"
run: cd src/server/self-hosted && yarn publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
2 changes: 1 addition & 1 deletion docs/.vuepress/theme/layouts/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<!-- Twikoo -->
<div id="twikoo"></div>
<component :is="'script'" src="https://cdn.staticfile.org/twikoo/1.6.0-beta.1/twikoo.all.min.js" ref="twikooJs"></component>
<component :is="'script'" src="https://cdn.staticfile.org/twikoo/1.6.0-beta.2/twikoo.all.min.js" ref="twikooJs"></component>
</div>
</template>
</ParentLayout>
Expand Down
34 changes: 33 additions & 1 deletion docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Twikoo 分为云函数和前端两部分,部署时请注意保持二者版本一致。

* [云函数部署](#云函数部署)有 4 种方式,[一键部署](#一键部署)[手动部署](#手动部署)[命令行部署](#命令行部署)[Vercel 部署](#vercel-部署)
* [云函数部署](#云函数部署)有 4 种方式,[一键部署](#一键部署)[手动部署](#手动部署)[命令行部署](#命令行部署)[Vercel 部署](#vercel-部署)[私有部署](#私有部署)
* [前端部署](#前端部署)有 2 种方式,如果您的网站主题支持 Twikoo,您只需在配置文件中指定 Twikoo 即可;如果您的网站主题不支持 Twikoo,您需要修改源码手动引入 Twikoo 的 js 文件并初始化。

## 云函数部署
Expand All @@ -13,6 +13,7 @@ Twikoo 分为云函数和前端两部分,部署时请注意保持二者版本
| [手动部署](#手动部署) | [ 建议 ] 手动部署到腾讯云云开发环境,在中国大陆访问速度较快。由于基础版 1 已从 0 元涨价至 6.9 元 / 月,需要付费购买环境才能部署。 |
| [命令行部署](#命令行部署) | [ 不建议 ] 仅针对有 Node.js 经验的开发者。 |
| [Vercel 部署](#vercel-部署) | [ 建议 ] 适用于想要免费部署的用户,在中国大陆访问速度较慢。 |
| [私有部署](#私有部署) | [ 建议 ] 适用于有服务器的用户,需要自行申请 HTTPS 证书。 |

### 一键部署

Expand Down Expand Up @@ -102,6 +103,32 @@ Vercel 部署的环境需配合 1.4.0 以上版本的 twikoo.js 使用
8. 进入 Overview,点击 Domains 下方的链接,如果环境配置正确,可以看到 “Twikoo 云函数运行正常” 的提示
9. Vercel Domains(包含 `https://` 前缀,例如 `https://xxx.vercel.app`)即为您的环境 id

### 私有部署

::: warning 注意
私有部署的环境需配合 1.6.0 或以上版本的 twikoo.js 使用
:::

1. 服务端下载安装 [Node.js](https://nodejs.org/zh-cn/)
2. 安装 Twikoo server: `npm i -g tkserver`
3. 根据需要配置环境变量

| 名称 | 描述 | 默认值 |
| ---- | ---- | ---- |
| `TWIKOO_DATA` | 数据库存储路径 | `./data` |
| `TWIKOO_PORT` | 端口号 | `8080` |
| `TWIKOO_THROTTLE` | IP 请求限流,当同一 IP 短时间内请求次数超过阈值将对该 IP 返回错误 | `250` |

4. 启动 Twikoo server: `tkserver`
5. 访问 `http://服务端IP:8080`
6. 若能正常访问,服务端地址(包含 `http://` 和端口号,例如 `http://12.34.56.78:8080`)即为您的环境 id

::: info 其他事项
1. Linux 服务器可以用 `nohup tkserver >> tkserver.log 2>&1 &` 命令后台启动
2. 强烈建议配置前置 nginx 服务器并配置 https 证书
3. 数据在服务器上,请注意定期备份数据
:::

## 前端部署

### 在 Hexo 中使用
Expand Down Expand Up @@ -249,6 +276,11 @@ yarn deploy -e 您的环境id
4.`"twikoo-vercel": "x.x.x"` 其中的版本号修改为最新版本号。点击 Commit changes
5. 部署会自动触发,可以回到 [Vercel 仪表板](https://vercel.com/dashboard),查看部署状态

### 针对私有部署的更新方式

1. 在服务器上执行 `npm i -g tkserver@latest`
2. 重新启动 `tkserver`

### 自动更新

考虑到可用性和安全性问题,Twikoo 没有实现自动更新,也没有计划实现自动更新。如果您希望实现自动更新,可以参考 MHuiG 基于 Github 工作流的 [twikoo-update](https://github.com/MHuiG/twikoo-update) 的实现方式。
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twikoo",
"version": "1.6.0-beta.1",
"version": "1.6.0-beta.2",
"description": "A simple comment system.",
"keywords": [
"twikoojs",
Expand Down
2 changes: 1 addition & 1 deletion src/client/version.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const version = '1.6.0-beta.1'
const version = '1.6.0-beta.2'

export { version }
2 changes: 1 addition & 1 deletion src/server/function/twikoo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const requestTimes = {}
// 云函数入口点 / entry point
exports.main = async (event, context) => {
console.log('请求 IP:', auth.getClientIP())
console.log('请求方法:', event.event)
console.log('请求函数:', event.event)
console.log('请求参数:', event)
let res = {}
try {
Expand Down
2 changes: 1 addition & 1 deletion src/server/function/twikoo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twikoo-func",
"version": "1.6.0-beta.1",
"version": "1.6.0-beta.2",
"description": "A simple comment system.",
"author": "imaegoo <[email protected]> (https://github.com/imaegoo)",
"license": "MIT",
Expand Down
21 changes: 21 additions & 0 deletions src/server/self-hosted/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Twikoo 私有部署服务端

## 安装

```
npm i -g tkserver
```

## 启动

```
tkserver
```

## 环境变量

| 名称 | 描述 | 默认值 |
| ---- | ---- | ---- |
| `TWIKOO_DATA` | 数据库存储路径 | `./data` |
| `TWIKOO_PORT` | 端口号 | `8080` |
| `TWIKOO_THROTTLE` | IP 请求限流,当同一 IP 短时间内请求次数超过阈值将对该 IP 返回错误 | `250` |
Empty file.
2 changes: 1 addition & 1 deletion src/server/self-hosted/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = async (request, response) => {
let accessToken
const event = request.body || {}
console.log('请求 IP:', getIp(request))
console.log('请求方法:', event.event)
console.log('请求函数:', event.event)
console.log('请求参数:', event)
let res = {}
try {
Expand Down
18 changes: 14 additions & 4 deletions src/server/self-hosted/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
{
"name": "twikoo-server",
"version": "1.6.0-beta.1",
"name": "tkserver",
"version": "1.6.0-beta.2",
"description": "A simple comment system.",
"keywords": [
"twikoo",
"twikoojs",
"comment",
"comment-system"
],
"author": "imaegoo <[email protected]> (https://github.com/imaegoo)",
"license": "MIT",
"main": "server.js",
"bin": {
"twikoo": "server.js"
"tkserver": "server.js"
},
"repository": {
"type": "git",
"url": "https://github.com/imaegoo/twikoo.git"
},
"homepage": "https://twikoo.js.org",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"lokijs": "^1.5.12",
"twikoo-func": "1.6.0-beta.1",
"twikoo-func": "1.6.0-beta.2",
"uuid": "^8.3.2"
}
}
12 changes: 12 additions & 0 deletions src/server/self-hosted/server.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const http = require('http')

const dataDir = path.resolve(process.cwd(), process.env.TWIKOO_DATA || './data')
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir)
}
console.log(`Twikoo database stored at ${dataDir}`)

const twikoo = require('./index')
const server = http.createServer()

Expand All @@ -10,6 +21,7 @@ server.on('request', async function (request, response) {
}
request.body = JSON.parse(Buffer.concat(buffers).toString())
} catch (e) {
console.error(e.message)
request.body = {}
}
response.status = function (code) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/vercel/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const requestTimes = {}
module.exports = async (request, response) => {
const event = request.body || {}
console.log('请求 IP:', request.headers['x-real-ip'])
console.log('请求方法:', event.event)
console.log('请求函数:', event.event)
console.log('请求参数:', event)
let res = {}
try {
Expand Down
4 changes: 2 additions & 2 deletions src/server/vercel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twikoo-vercel",
"version": "1.6.0-beta.1",
"version": "1.6.0-beta.2",
"description": "A simple comment system.",
"author": "imaegoo <[email protected]> (https://github.com/imaegoo)",
"license": "MIT",
Expand All @@ -12,7 +12,7 @@
"homepage": "https://twikoo.js.org",
"dependencies": {
"mongodb": "^3.6.3",
"twikoo-func": "1.6.0-beta.1",
"twikoo-func": "1.6.0-beta.2",
"uuid": "^8.3.2"
}
}

0 comments on commit 197865d

Please sign in to comment.