Skip to content

Commit 5856637

Browse files
committed
add serverless of version-control
1 parent 07c32ed commit 5856637

File tree

8 files changed

+760
-640
lines changed

8 files changed

+760
-640
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 快速构建 koa-starter
2+
3+
**中文** | [English](./README_EN.md)
4+
5+
## 简介
6+
7+
koa-starter 模板使用 Tencent SCF 组件及其触发器能力,方便的在腾讯云创建,配置和管理一个 koa-starter 应用。
8+
9+
## 快速开始
10+
11+
### 1. 安装
12+
13+
```bash
14+
# 安装 Serverless Framework
15+
npm install -g serverless
16+
```
17+
18+
### 2. 创建
19+
20+
通过如下命令直接下载该例子:
21+
22+
```bash
23+
serverless init koa-starter --name example
24+
cd example
25+
```
26+
27+
### 3. 部署
28+
29+
`serverless.yml` 文件所在的项目根目录,运行以下指令,将会弹出二维码,直接扫码授权进行部署:
30+
31+
```bash
32+
serverless deploy
33+
```
34+
35+
> **说明**:如果鉴权失败,请参考 [权限配置](https://cloud.tencent.com/document/product/1154/43006) 进行授权。
36+
37+
### 4. 查看状态
38+
39+
执行以下命令,查看您部署的项目信息:
40+
41+
```bash
42+
serverless info
43+
```
44+
45+
### 5. 移除
46+
47+
可以通过以下命令移除 koa-starter 应用
48+
49+
```bash
50+
serverless remove
51+
```
52+
53+
### 账号配置(可选)
54+
55+
serverless 默认支持扫描二维码登录,用户扫描二维码后会自动生成一个 `.env` 文件并将密钥存入其中.
56+
如您希望配置持久的环境变量/秘钥信息,也可以本地创建 `.env` 文件,
57+
把从[API 密钥管理](https://console.cloud.tencent.com/cam/capi)中获取的 `SecretId``SecretKey` 填入其中.
58+
59+
> 如果没有腾讯云账号,可以在此[注册新账号](https://cloud.tencent.com/register)
60+
61+
```bash
62+
# 腾讯云的配置信息
63+
touch .env
64+
```
65+
66+
```
67+
# .env file
68+
TENCENT_SECRET_ID=123
69+
TENCENT_SECRET_KEY=123
70+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Quickly create and deploy koa-starter application
2+
3+
[中文](./README.md) | **English**
4+
5+
## Introduction
6+
7+
Easily deploy koa-starter applications to Tencent Cloud's serverless infrastructure using this Serverless Framework Component.
8+
Your application will auto-scale, never charge you for idle time, and require little-to-zero administration.
9+
10+
## Quick Start
11+
12+
### 1. Install
13+
14+
```bash
15+
# Install Serverless Framework
16+
npm install -g serverless
17+
```
18+
19+
### 2. Initialize
20+
21+
Initializing the koa-starter template by running this following command:
22+
23+
```bash
24+
serverless init koa-starter --name example
25+
cd example
26+
```
27+
28+
### 3. Deploy
29+
30+
You can use following command to deploy the APP.
31+
32+
```bash
33+
cd koa-starter
34+
serverless deploy
35+
```
36+
37+
This command will walk you through signing up a Tencent Cloud Account to deploy the APP.
38+
39+
### 4. Monitor
40+
41+
Anytime you need to know more about your running express instance, you can run `serverless info` to view the most critical info.
42+
This is especially helpful when you want to know the outputs of your instances so that you can reference them in another instance.
43+
You will also see a url where you'll be able to view more info about your instance on the Serverless Dashboard.
44+
45+
It also shows you the status of your instance, when it was last deployed, and how many times it was deployed.
46+
To dig even deeper, you can pass the --debug flag to view the state of your component instance in case the deployment failed for any reason.
47+
48+
```bash
49+
serverless info
50+
```
51+
52+
### 5. Remove
53+
54+
If you wanna tear down your entire infrastructure that was created during deployment,
55+
just run `serverless remove` and serverless will remove all the data it needs from the built-in state storage system to delete only the relevant cloud resources that it created.
56+
57+
```bash
58+
serverless remove
59+
```
60+
61+
### Setting up credentials (Optional)
62+
63+
By default, you are able to login your Tencent Cloud account by scanning QR code and an `.env` file with credentials is auto generated.
64+
The credentials will be expired after 2 hours.
65+
If you would like to use persistent credentials,
66+
you can [create an API Key here](https://console.cloud.tencent.com/cam/capi) and add the `SecretId` and `SecretKey` into the `.env` file
67+
68+
> If you don's have a Tencent Cloud Account, you can register [here](https://cloud.tencent.com/register)
69+
70+
```bash
71+
# Add your Tencent credentials here
72+
touch .env
73+
```
74+
75+
76+
```
77+
# .env file
78+
TENCENT_SECRET_ID=123
79+
TENCENT_SECRET_KEY=123
80+
```
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
'use strict';
2+
3+
const { Pool } = require('pg');
4+
5+
function ApiError(code, msg) {
6+
const e = new Error(msg);
7+
e.code = code;
8+
return e;
9+
}
10+
11+
let pgPool;
12+
13+
module.exports = {
14+
async getPool() {
15+
if (!pgPool) {
16+
pgPool = new Pool({
17+
connectionString: 'postgresql://tencentdb_8ygfbnqf:ib4%7C0)DS6%24CldO1@10.0.0.7:5432/tencentdb_8ygfbnqf\n',
18+
});
19+
// init table
20+
await pgPool.query(`CREATE TABLE IF NOT EXISTS versions (
21+
ID SERIAL PRIMARY KEY,
22+
VERSION TEXT NOT NULL,
23+
URL CHAR(50) NOT NULL
24+
);`);
25+
return pgPool;
26+
} else {
27+
return pgPool;
28+
}
29+
},
30+
async getVersionList() {
31+
const pool = await this.getPool();
32+
const client = await pool.connect();
33+
const { rows } = await client.query({
34+
text: 'select * from versions',
35+
});
36+
await client.end();
37+
return rows;
38+
},
39+
async addVersion(versionInfo) {
40+
const pool = await this.getPool();
41+
const { version, url } = versionInfo;
42+
const deleteWhiteOfVersion = version.trim()
43+
const deleteWhiteOfUrl = url.trim()
44+
const existVersion = await this.getVersion(deleteWhiteOfVersion);
45+
if (existVersion) {
46+
throw new ApiError(1000, `Version ${deleteWhiteOfVersion} exist.`);
47+
}
48+
const client = await pool.connect();
49+
const { rowCount } = await client.query({
50+
text: 'INSERT INTO versions(version, url) VALUES($1, $2)',
51+
values: [deleteWhiteOfVersion, deleteWhiteOfUrl],
52+
});
53+
await client.end();
54+
return rowCount === 1;
55+
},
56+
async getVersion(version) {
57+
try {
58+
const pool = await this.getPool();
59+
const client = await pool.connect();
60+
const { rows } = await client.query({
61+
text: 'SELECT * FROM versions WHERE version = $1',
62+
values: [version],
63+
});
64+
await client.end();
65+
if (rows.length > 0) {
66+
return rows;
67+
}
68+
return false;
69+
} catch (e) {
70+
throw new ApiError(1001, e);
71+
}
72+
},
73+
async getUrlByVersion(version){
74+
try{
75+
const pool = await this.getPool();
76+
const client = await pool.connect();
77+
const { rows } = await client.query({
78+
text: 'SELECT URL FROM versions WHERE version = $1',
79+
values: [version],
80+
});
81+
await client.end();
82+
return rows;
83+
}catch (e) {
84+
throw new ApiError(1001, e);
85+
}
86+
},
87+
async deleteVersion(version) {
88+
const pool = await this.getPool();
89+
const client = await pool.connect();
90+
const { rows } = await client.query({
91+
text: 'DELETE FROM versions WHERE version = $1',
92+
values: [version],
93+
});
94+
await client.end();
95+
return rows;
96+
},
97+
};
98+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Serverless Component - Express.js</title>
7+
<style lang="css">
8+
h1 {
9+
text-align: center;
10+
width: 600px;
11+
margin: 300px auto;
12+
}
13+
</style>
14+
</head>
15+
<body>
16+
<h1>
17+
Welcome to Koa.js application created by
18+
<a href="https://serverlesscloud.cn/">Serverless Framework.</a>
19+
</h1>
20+
</body>
21+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "koa-starter",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "sls.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "yugasun",
10+
"license": "MIT",
11+
"devDependencies": {
12+
"@types/koa-router": "^7.4.1",
13+
"koa": "^2.11.0",
14+
"koa-bodyparser": "^4.3.0",
15+
"koa-router": "^8.0.8",
16+
"koa-sendfile": "^2.0.1",
17+
"pg": "^7.14.0"
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
component: koa
2+
name: dapp-version-control-api
3+
app: dapp-version-control-db
4+
5+
inputs:
6+
entryFile: sls.js #以您实际入口文件名为准
7+
src:
8+
src: ./
9+
exclude:
10+
- .env
11+
region: ap-guangzhou
12+
functionName: dapp-version-api # 云函数名称
13+
runtime: Nodejs10.15
14+
serviceName: dappVersionService # api网关服务名称
15+
apigatewayConf:
16+
protocols:
17+
- http
18+
- https
19+
environment: release
20+
enableCORS: true # 允许跨域

packages/version-control-api/sls.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const Koa = require('koa')
2+
const KoaRouter = require('koa-router')
3+
const sendFile = require('koa-sendfile')
4+
const path = require('path')
5+
6+
const app = new Koa()
7+
const router = new KoaRouter()
8+
9+
// Routes
10+
router.get(`/*`, async (ctx) => {
11+
await sendFile(ctx, path.join(__dirname, 'index.html'))
12+
})
13+
14+
app.use(router.allowedMethods()).use(router.routes());
15+
16+
// don't forget to export!
17+
module.exports = app

0 commit comments

Comments
 (0)