Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: 添加云函数 Deta 部署功能 #659

Merged
merged 2 commits into from Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/server/deta/README.md
Original file line number Diff line number Diff line change
@@ -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/
12 changes: 12 additions & 0 deletions src/server/deta/Spacefile
Original file line number Diff line number Diff line change
@@ -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: ""
40 changes: 40 additions & 0 deletions src/server/deta/index.js
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions src/server/deta/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "dependencies": { "twikoo-vercel": "latest","express": "latest" } }
Loading