From 020182b4e4bf1a87cad6d4f0929292d7e4c08d9a Mon Sep 17 00:00:00 2001 From: LuminousDream Date: Mon, 29 Jan 2024 20:51:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E4=BA=91=E5=87=BD?= =?UTF-8?q?=E6=95=B0=20Deta=20=E9=83=A8=E7=BD=B2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/deta/README.md | 17 +++++++++++++++ src/server/deta/Spacefile | 12 +++++++++++ src/server/deta/index.js | 40 ++++++++++++++++++++++++++++++++++++ src/server/deta/package.json | 1 + 4 files changed, 70 insertions(+) create mode 100644 src/server/deta/README.md create mode 100644 src/server/deta/Spacefile create mode 100644 src/server/deta/index.js create mode 100644 src/server/deta/package.json diff --git a/src/server/deta/README.md b/src/server/deta/README.md new file mode 100644 index 000000000..fac6aed4b --- /dev/null +++ b/src/server/deta/README.md @@ -0,0 +1,17 @@ +# Deta + +本目录存储 Deta Space serverless functions 代码 + +## 开发帮助文档 + +https://deta.space/docs/en/build/new-apps + +https://deta.space/docs/en/build/reference/cli + +https://deta.space/docs/en/build/reference/spacefile + +https://deta.space/docs/en/build/quick-starts/node + +https://docs.mongodb.com/drivers/node/quick-start/ + +http://mongodb.github.io/node-mongodb-native/3.6/api/ \ No newline at end of file diff --git a/src/server/deta/Spacefile b/src/server/deta/Spacefile new file mode 100644 index 000000000..345d71897 --- /dev/null +++ b/src/server/deta/Spacefile @@ -0,0 +1,12 @@ +# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0 +v: 0 +micros: + - name: twikoo-deta + src: ./ + engine: nodejs16 + public: true + presets: + env: + - name: MONGODB_URI + description: Twikoo 评论系统 MongoDB 数据库 URI + default: "" \ No newline at end of file diff --git a/src/server/deta/index.js b/src/server/deta/index.js new file mode 100644 index 000000000..063840ea8 --- /dev/null +++ b/src/server/deta/index.js @@ -0,0 +1,40 @@ +/** + * Deta 兼容 Vercel 函数实现 + * 复用 Twikoo Vercel 函数代码 + */ + +const twikoo = require('twikoo-vercel') +const express = require('express') +const app = express() + +// Tip: Deta 本身无法获取评论者 IP,需要使用 Cloudflare CDN 才能获取评论者 IP。 +// Docs: https://deta.space/docs/en/build/guides/accessing-client-ip-address +process.env.TWIKOO_IP_HEADERS = JSON.stringify(['headers.cf-connecting-ip']) + +app.use(async function (req, res) { + const buffers = []; + req.on('data', (chunk) => { + buffers.push(chunk); + }); + req.on('end', async () => { + try { + req.body = JSON.parse(Buffer.concat(buffers).toString()); + } catch (e) { + req.body = {}; + } + res.status = function (code) { + res.statusCode = code; + return this; + }; + res.json = function (json) { + if (!res.headersSent) { + res.setHeader('Content-Type', 'application/json'); + res.status(200).send(JSON.stringify(json)); + } + return this; + }; + return await twikoo(req, res); + }); +}); + +app.listen(8080); diff --git a/src/server/deta/package.json b/src/server/deta/package.json new file mode 100644 index 000000000..ff86e6fd4 --- /dev/null +++ b/src/server/deta/package.json @@ -0,0 +1 @@ +{ "dependencies": { "twikoo-vercel": "latest","express": "latest" } } \ No newline at end of file From 9655328362c9f0d133b132e876158b7b0bc102eb Mon Sep 17 00:00:00 2001 From: LuminousDream Date: Tue, 30 Jan 2024 16:01:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9C=A8=20yarn?= =?UTF-8?q?=20lint=20=E7=9A=84=E4=BB=A3=E7=A0=81=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: LuminousDream --- src/server/deta/index.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/server/deta/index.js b/src/server/deta/index.js index 063840ea8..bd257c997 100644 --- a/src/server/deta/index.js +++ b/src/server/deta/index.js @@ -12,29 +12,29 @@ const app = express() process.env.TWIKOO_IP_HEADERS = JSON.stringify(['headers.cf-connecting-ip']) app.use(async function (req, res) { - const buffers = []; + const buffers = [] req.on('data', (chunk) => { - buffers.push(chunk); - }); + buffers.push(chunk) + }) req.on('end', async () => { try { - req.body = JSON.parse(Buffer.concat(buffers).toString()); + req.body = JSON.parse(Buffer.concat(buffers).toString()) } catch (e) { - req.body = {}; + req.body = {} } res.status = function (code) { - res.statusCode = code; - return this; - }; + res.statusCode = code + return this + } res.json = function (json) { if (!res.headersSent) { - res.setHeader('Content-Type', 'application/json'); - res.status(200).send(JSON.stringify(json)); + res.setHeader('Content-Type', 'application/json') + res.status(200).send(JSON.stringify(json)) } - return this; - }; - return await twikoo(req, res); - }); -}); + return this + } + return await twikoo(req, res) + }) +}) -app.listen(8080); +app.listen(8080)