From cc95e83bed7eb86e95bfcab690ee0df861a95879 Mon Sep 17 00:00:00 2001 From: Zhuoyun Wei Date: Mon, 20 May 2024 23:24:01 -0700 Subject: [PATCH 1/6] feat: aws-lambda compat code --- src/server/aws-lambda/LICENSE | 21 +++++++++++++ src/server/aws-lambda/README.md | 1 + src/server/aws-lambda/index.js | 50 ++++++++++++++++++++++++++++++ src/server/aws-lambda/package.json | 16 ++++++++++ 4 files changed, 88 insertions(+) create mode 100644 src/server/aws-lambda/LICENSE create mode 100644 src/server/aws-lambda/README.md create mode 100644 src/server/aws-lambda/index.js create mode 100644 src/server/aws-lambda/package.json diff --git a/src/server/aws-lambda/LICENSE b/src/server/aws-lambda/LICENSE new file mode 100644 index 000000000..f387b7b62 --- /dev/null +++ b/src/server/aws-lambda/LICENSE @@ -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. diff --git a/src/server/aws-lambda/README.md b/src/server/aws-lambda/README.md new file mode 100644 index 000000000..b1d649c04 --- /dev/null +++ b/src/server/aws-lambda/README.md @@ -0,0 +1 @@ +# AWS Lambda diff --git a/src/server/aws-lambda/index.js b/src/server/aws-lambda/index.js new file mode 100644 index 000000000..4aaa02c11 --- /dev/null +++ b/src/server/aws-lambda/index.js @@ -0,0 +1,50 @@ +const twikoo = require('twikoo-vercel') + +/* +AWS Lambda compat layer for Vercel +https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html +*/ + +exports.handler = async function (event, context) { + process.env.VERCEL_URL = event.requestContext.domainName; + process.env.TWIKOO_IP_HEADERS = JSON.stringify([ + 'headers.requestContext.http.sourceIp' + ]) + const result = { + statusCode: 204, + headers: {}, + body: '' + } + const request = { + method: event.requestContext.http.method, + headers: event.headers, + body: {} + } + try { + if (event.isBase64Encoded) { + request.body = JSON.parse(Buffer.from(event.body, 'base64').toString('utf-8')); + } else { + 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 +} diff --git a/src/server/aws-lambda/package.json b/src/server/aws-lambda/package.json new file mode 100644 index 000000000..f46b9dcfd --- /dev/null +++ b/src/server/aws-lambda/package.json @@ -0,0 +1,16 @@ +{ + "name": "twikoo-aws-lambda", + "version": "1.6.35", + "description": "A simple comment system.", + "author": "imaegoo (https://github.com/imaegoo)", + "license": "MIT", + "main": "index.js", + "repository": { + "type": "git", + "url": "https://github.com/twikoojs/twikoo.git" + }, + "homepage": "https://twikoo.js.org", + "dependencies": { + "twikoo-vercel": "latest" + } +} From 7b39c28699048d8279e675185b0d500fc1426d3e Mon Sep 17 00:00:00 2001 From: Zhuoyun Wei Date: Mon, 20 May 2024 23:45:47 -0700 Subject: [PATCH 2/6] feat: Terraform code to deploy the function --- src/server/aws-lambda/README.md | 2 ++ src/server/aws-lambda/{ => src}/index.js | 0 src/server/aws-lambda/{ => src}/package.json | 0 src/server/aws-lambda/terraform/.gitignore | 4 +++ src/server/aws-lambda/terraform/main.tf | 34 +++++++++++++++++++ .../aws-lambda/terraform/terraform.tfvars | 1 + src/server/aws-lambda/terraform/variables.tf | 9 +++++ 7 files changed, 50 insertions(+) rename src/server/aws-lambda/{ => src}/index.js (100%) rename src/server/aws-lambda/{ => src}/package.json (100%) create mode 100644 src/server/aws-lambda/terraform/.gitignore create mode 100644 src/server/aws-lambda/terraform/main.tf create mode 100644 src/server/aws-lambda/terraform/terraform.tfvars create mode 100644 src/server/aws-lambda/terraform/variables.tf diff --git a/src/server/aws-lambda/README.md b/src/server/aws-lambda/README.md index b1d649c04..b23e1e580 100644 --- a/src/server/aws-lambda/README.md +++ b/src/server/aws-lambda/README.md @@ -1 +1,3 @@ # AWS Lambda + +Deploy Twikoo to [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html). diff --git a/src/server/aws-lambda/index.js b/src/server/aws-lambda/src/index.js similarity index 100% rename from src/server/aws-lambda/index.js rename to src/server/aws-lambda/src/index.js diff --git a/src/server/aws-lambda/package.json b/src/server/aws-lambda/src/package.json similarity index 100% rename from src/server/aws-lambda/package.json rename to src/server/aws-lambda/src/package.json diff --git a/src/server/aws-lambda/terraform/.gitignore b/src/server/aws-lambda/terraform/.gitignore new file mode 100644 index 000000000..eadbc6982 --- /dev/null +++ b/src/server/aws-lambda/terraform/.gitignore @@ -0,0 +1,4 @@ +.terraform* +*.tfstate +*.tfstate.backup +builds/ diff --git a/src/server/aws-lambda/terraform/main.tf b/src/server/aws-lambda/terraform/main.tf new file mode 100644 index 000000000..86db123c4 --- /dev/null +++ b/src/server/aws-lambda/terraform/main.tf @@ -0,0 +1,34 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 4.0" + } + } +} + +provider "aws" { + region = var.region +} + +module "lambda_function" { + source = "terraform-aws-modules/lambda/aws" + version = "7.4.0" + + function_name = "twikoo" + handler = "index.handler" + runtime = "nodejs20.x" + timeout = 10 + + source_path = "../src" + + environment_variables = { + MONGODB_URI = var.mongodb_uri + } + + create_lambda_function_url = true +} + +output "lambda_function_url" { + value = module.lambda_function.lambda_function_url +} diff --git a/src/server/aws-lambda/terraform/terraform.tfvars b/src/server/aws-lambda/terraform/terraform.tfvars new file mode 100644 index 000000000..384addd08 --- /dev/null +++ b/src/server/aws-lambda/terraform/terraform.tfvars @@ -0,0 +1 @@ +mongodb_uri = "mongodb+srv://..." diff --git a/src/server/aws-lambda/terraform/variables.tf b/src/server/aws-lambda/terraform/variables.tf new file mode 100644 index 000000000..7f29dd313 --- /dev/null +++ b/src/server/aws-lambda/terraform/variables.tf @@ -0,0 +1,9 @@ +variable "region" { + description = "AWS region to deploy the function in." + default = "us-west-2" +} + +variable "mongodb_uri" { + description = "MongoDB connection URI" + sensitive = true +} From bb78989a8006658ffa8c81cc2cc526c26b9f9a16 Mon Sep 17 00:00:00 2001 From: Zhuoyun Wei Date: Mon, 20 May 2024 23:49:57 -0700 Subject: [PATCH 3/6] docs: how to use Terraform --- src/server/aws-lambda/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/server/aws-lambda/README.md b/src/server/aws-lambda/README.md index b23e1e580..90fee0fe9 100644 --- a/src/server/aws-lambda/README.md +++ b/src/server/aws-lambda/README.md @@ -1,3 +1,18 @@ # AWS Lambda Deploy Twikoo to [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html). + +## Deploy with Terraform + +```bash +cd terraform + +# Update MONGODB_URI +vim terraform.tfvars + +# Init Terraform modules +terraform init + +# Deploy to AWS +terraform apply +``` From d07ceb3e05acaf0cd5afb3e51365c788287ea151 Mon Sep 17 00:00:00 2001 From: Zhuoyun Wei Date: Mon, 20 May 2024 23:52:33 -0700 Subject: [PATCH 4/6] docs: do not set a dummy value of MONGOODB_URI --- src/server/aws-lambda/README.md | 3 --- src/server/aws-lambda/terraform/terraform.tfvars | 1 - src/server/aws-lambda/terraform/variables.tf | 2 +- 3 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 src/server/aws-lambda/terraform/terraform.tfvars diff --git a/src/server/aws-lambda/README.md b/src/server/aws-lambda/README.md index 90fee0fe9..86c10ea44 100644 --- a/src/server/aws-lambda/README.md +++ b/src/server/aws-lambda/README.md @@ -7,9 +7,6 @@ Deploy Twikoo to [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/urls- ```bash cd terraform -# Update MONGODB_URI -vim terraform.tfvars - # Init Terraform modules terraform init diff --git a/src/server/aws-lambda/terraform/terraform.tfvars b/src/server/aws-lambda/terraform/terraform.tfvars deleted file mode 100644 index 384addd08..000000000 --- a/src/server/aws-lambda/terraform/terraform.tfvars +++ /dev/null @@ -1 +0,0 @@ -mongodb_uri = "mongodb+srv://..." diff --git a/src/server/aws-lambda/terraform/variables.tf b/src/server/aws-lambda/terraform/variables.tf index 7f29dd313..748732cd9 100644 --- a/src/server/aws-lambda/terraform/variables.tf +++ b/src/server/aws-lambda/terraform/variables.tf @@ -4,6 +4,6 @@ variable "region" { } variable "mongodb_uri" { - description = "MongoDB connection URI" + description = "MongoDB connection URI. The value will be passed to the Lambda function as environment variable MONGODB_URI." sensitive = true } From 5647f3acc17dfa3c1e4feffe0230adefdcce5b3f Mon Sep 17 00:00:00 2001 From: Zhuoyun Wei Date: Tue, 21 May 2024 00:21:30 -0700 Subject: [PATCH 5/6] docs: deploy backend on AWS Lambda --- docs/backend.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/backend.md b/docs/backend.md index c4ad146c2..5773e1416 100644 --- a/docs/backend.md +++ b/docs/backend.md @@ -10,6 +10,7 @@ | [Zeabur 部署](#zeabur-部署) | ★☆☆☆☆ | 需要绑定支付宝或信用卡,部署简单,适合中国大陆访问,免费计划环境随时可能会被删除。 | | [Netlify 部署](#netlify-部署) | ★★★★☆ | 有充足的免费额度,中国大陆访问速度不错。 | | [Hugging Face 部署](#hugging-face-部署) | ★★★★☆ | 免费,中国大陆访问速度不错。 | +| [AWS Lambda 部署](#aws-lambda-部署) | ★★★☆☆ | 全球最大的云平台,适合已经使用 AWS 全家桶的用户。 | | [私有部署](#私有部署) | ★★☆☆☆ | 适用于有服务器的用户,需要自行申请 HTTPS 证书。 | | [私有部署 (Docker)](#私有部署-docker) | ★★★☆☆ | 适用于有服务器的用户,需要自行申请 HTTPS 证书。 | @@ -245,6 +246,19 @@ EXPOSE 7860 ![](./static/hugging-6.png) +## AWS Lambda 部署 + +1. 注册 AWS 账号并配置 Terraform CLI。 +2. 参考 `src/server/aws-lambda/terraform` 目录中 Terraform 代码创建 AWS 资源。 +3. 部署完成后,Terraform 会将 `lambda_function_url` 打印在屏幕上,您也可以使用 `terraform output` 获取这一 URL,如: + +``` +$ terraform output +lambda_function_url = "https://axtoiiithbcexamplegq7ozalu0cnkii.lambda-url.us-west-2.on.aws/" +``` + +该 URL 即为您的环境 ID,请记下这一 URL 用于前端配置。 + ## 私有部署 ::: warning 注意 From 95a934f4850a416331e603273231988b30ff5d87 Mon Sep 17 00:00:00 2001 From: Zhuoyun Wei Date: Tue, 21 May 2024 04:51:16 -0700 Subject: [PATCH 6/6] lint: remove extra semicolons --- src/server/aws-lambda/src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/server/aws-lambda/src/index.js b/src/server/aws-lambda/src/index.js index 4aaa02c11..13c68c9f2 100644 --- a/src/server/aws-lambda/src/index.js +++ b/src/server/aws-lambda/src/index.js @@ -6,7 +6,7 @@ https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html */ exports.handler = async function (event, context) { - process.env.VERCEL_URL = event.requestContext.domainName; + process.env.VERCEL_URL = event.requestContext.domainName process.env.TWIKOO_IP_HEADERS = JSON.stringify([ 'headers.requestContext.http.sourceIp' ]) @@ -22,9 +22,9 @@ exports.handler = async function (event, context) { } try { if (event.isBase64Encoded) { - request.body = JSON.parse(Buffer.from(event.body, 'base64').toString('utf-8')); + request.body = JSON.parse(Buffer.from(event.body, 'base64').toString('utf-8')) } else { - request.body = JSON.parse(event.body); + request.body = JSON.parse(event.body) } } catch (e) {} const response = {