Skip to content

Commit

Permalink
feat: add twikoo-netlify package
Browse files Browse the repository at this point in the history
  • Loading branch information
imaegoo committed Sep 8, 2023
1 parent 604ffd3 commit d1972ba
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/server/netlify/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020-present iMaeGoo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions src/server/netlify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Twikoo 云函数

## 部署

请查看 [Netlify 部署](https://twikoo.js.org/backend.html#netlify-部署)
47 changes: 47 additions & 0 deletions src/server/netlify/netlify/functions/twikoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const twikoo = require('twikoo-vercel')

/**
* Netlify 函数兼容 Vercel 函数实现
* 复用 Twikoo Vercel 函数代码
* Netlify functions doc:
* https://docs.netlify.com/functions/create/?fn-language=js
*/
exports.handler = async function (event, context) {
process.env.VERCEL_URL = event.rawUrl
process.env.TWIKOO_IP_HEADERS = JSON.stringify([
'headers.x-nf-client-connection-ip'
])
const result = {
statusCode: 204,
headers: {},
body: ''
}
const request = {
method: event.httpMethod,
headers: event.headers,
body: {}
}
try {
request.body = JSON.parse(event.body)
} catch (e) {}
const response = {
status: function (code) {
result.statusCode = code
return this
},
json: function (json) {
result.headers['Content-Type'] = 'application/json'
result.body = JSON.stringify(json)
return this
},
end: function () {
return this
},
setHeader: function (k, v) {
result.headers[k] = v
return this
}
}
await twikoo(request, response)
return result
}
16 changes: 16 additions & 0 deletions src/server/netlify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "twikoo-netlify",
"version": "1.6.19",
"description": "A simple comment system.",
"author": "imaegoo <[email protected]> (https://github.com/imaegoo)",
"license": "MIT",
"main": "netlify/functions/twikoo.js",
"repository": {
"type": "git",
"url": "https://github.com/twikoojs/twikoo.git"
},
"homepage": "https://twikoo.js.org",
"dependencies": {
"twikoo-vercel": "latest"
}
}

0 comments on commit d1972ba

Please sign in to comment.