Skip to content

Commit fa9372a

Browse files
committed
fix todolist
1 parent 880e078 commit fa9372a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+139
-308
lines changed

example/todolist-app/publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Type: Application
22
Name: todolist-app
3-
Version: 0.1.1
3+
Version: 0.1.2
44
Provider:
55
- 阿里云
66
Description: 一款基于Node.JS的网页TodoList应用

example/todolist-app/src/README.md

Lines changed: 7 additions & 288 deletions
Original file line numberDiff line numberDiff line change
@@ -1,298 +1,17 @@
1-
# TodoList应用案例
1+
# todoList
22

3-
一款基于Node.JS的网页TodoList应用。
3+
## 准备
44

5-
- 基础介绍:
6-
- 框架:Express
7-
- 运行时:Node.js运行时 (当然,也可以选择Custom运行时,可以参考[Express的Custom案例](https://github.com/devsapp/start-web-framework/tree/master/web-framework/nodejs/express/src)
8-
- 使用流程
9-
- 初始化项目:`s init todolist-app`
10-
- 进入项目:`cd todolist-app`
11-
- 部署项目:`s deploy`
12-
13-
更多使用内容:
14-
- [本地构建](#本地构建)
15-
- [调试](#调试)
16-
- [端云联调](#端云联调)
17-
- [本地调试](#本地调试)
18-
- [部署](#部署)
19-
- [查看日志](#查看日志)
20-
- [对服务进行访问](#对服务进行访问)
21-
- [可观测性](#可观测性)
22-
- [发布](#发布)
23-
- [CICD](#CICD)
24-
- [删除](#删除)
25-
26-
> 权限与Yaml配置可以参考 [FC Yaml 规范文档](https://github.com/devsapp/fc/blob/jiangyu-docs/docs/zh/yaml.md)
27-
28-
### 本地构建
29-
30-
本案例为 nodejs runtime,因此构建过程实际上是进行了安装依赖的操作,可以使用如下三种方式进行构建:
31-
32-
```bash
33-
# 方式一,基于 npm 原生指令进行构建
34-
$ npm install
35-
36-
# 方式二,基于 Serverless Devs fc 组件,依赖于本机的 nodejs 环境进行构建,构建产物保存在 .s 目录下
37-
$ s build
38-
39-
# 方式三,基于 Serverless Devs fc 组件,需要安装 docker,依赖函数计算官方镜像的 nodejs 环境进行构建,构建产物保存在 .s 目录下
40-
$ s build --use-docker
41-
```
42-
43-
### 调试
44-
45-
调试目前只支持本地,有两种类型:
46-
47-
1. 本地调试: 本地启动函数容器,调用请求由本地发起,无法访问 vpc 内网以及一些云服务的内网地址。
48-
2. 端云联调: 本地启动函数容器,调用请求来自线上,可以访问 vpc 内网以及一些云服务的内网地址。
49-
50-
#### 端云联调
51-
52-
端云联调需要如下几种前置条件:
53-
54-
1. 开通[阿里云镜像服务](https://cr.console.aliyun.com/)
55-
2. 开通[阿里云日志服务](https://sls.console.aliyun.com/)
56-
3. 安装 docker
57-
58-
可以通过 proxied 系列指令,快速进行端云联调操作:
59-
60-
```bash
61-
# 设置环境变量 DISABLE_BIND_MOUNT_TMP_DIR,用于关闭本地函数容器挂载本机目录到 /tmp 的能力
62-
63-
# For Windows
64-
$ set DISABLE_BIND_MOUNT_TMP_DIR=true
65-
# For Macos
66-
$ export DISABLE_BIND_MOUNT_TMP_DIR=true
67-
68-
# 启动准备环境和辅助资源
69-
$ s proxied setup --config vscode --debug-port 3000
70-
```
71-
72-
`setup` 执行完后会阻塞住,此时需要重新打开一个终端执行调用请求:
73-
74-
```bash
75-
$ s proxied invoke
76-
```
77-
78-
此时本地函数就会被调用,而调用的返回结果只是一段 html 内容,此时不会执行 html 中发出的请求。这是因为端云联调的调用请求得到的 html 内容只能以文本形式返回,并不会执行 html 中请求静态页面的内容。
79-
80-
`setup` 时我们已经增加了 `--config vscode --debug-port 3000` 参数,这两个参数用于 vscode 断点调试,因此我们可以通过如下步骤进行断点调试。
81-
82-
1. 给源代码打上断点
83-
2. 启动调试器
84-
![img](https://img.alicdn.com/imgextra/i1/O1CN01sE914X1IBdKZMlqgN_!!6000000000855-2-tps-3572-2238.png)
85-
3. 在终端执行 `s proxied invoke` 指令
86-
87-
在端云联调完成后,需要执行 `s proxied clean` 指令来清理本地环境以及辅助资源。
88-
89-
#### 本地调试
90-
91-
本示例是 http 函数,因此使用 `local start` 指令来进行本地调试,若是 event 函数,需要使用 `local invoke`
92-
指令来进行调试,详情请参考[这里](https://github.com/devsapp/fc/blob/main/docs/Usage/local.md)
93-
94-
```bash
95-
# 设置环境变量 DISABLE_BIND_MOUNT_TMP_DIR,用于关闭本地函数容器挂载本机目录到 /tmp 的能力
96-
97-
# For Windows
98-
$ set DISABLE_BIND_MOUNT_TMP_DIR=true
99-
# For Macos
100-
$ export DISABLE_BIND_MOUNT_TMP_DIR=true
101-
102-
# 启动本地调试
103-
$ s local start
104-
[2021-07-23T11:25:43.197] [INFO ] [S-CLI] - Start ...
105-
[2021-07-23T11:25:45.473] [INFO ] [FC-LOCAL-INVOKE] - Using trigger for start: name: http-trigger
106-
type: http
107-
config:
108-
authType: anonymous
109-
methods:
110-
- GET
111-
- POST
112-
113-
[2021-07-23T11:25:45.477] [INFO ] [FC-LOCAL-INVOKE] - HttpTrigger http-trigger of todo-list-service/todo-list was registered
114-
url: http://localhost:7901/2016-08-15/proxy/todo-list-service/todo-list/
115-
methods: GET,POST
116-
authType: anonymous
117-
118-
Tips:you can also use these commands to run/debug custom domain resources:
119-
120-
Start with customDomain:
121-
* s local start auto
122-
123-
Debug with customDomain:
124-
* s local start -d 3000 auto
125-
126-
127-
Tips for next step
128-
======================
129-
* Deploy Resources: s deploy
130-
TodoList:
131-
status: succeed
132-
133-
function compute app listening on port 7901!
134-
```
135-
136-
此时访问上述 `url: http://localhost:7901/2016-08-15/proxy/todo-list-service/todo-list/` 字段即可访问本地启动的函数服务。
137-
138-
若要进行断点调试,需要给 `local start` 指令增加如下参数:
139-
140-
```bash
141-
$ s local start --config vscode --debug-port 3000
142-
```
143-
144-
断点调试模式下,可以通过如下步骤进行断点调试。
145-
146-
1. 给源代码打上断点
147-
2. 启动调试器
148-
![img](https://img.alicdn.com/imgextra/i1/O1CN01sE914X1IBdKZMlqgN_!!6000000000855-2-tps-3572-2238.png)
149-
3. 访问上述 `url: http://localhost:7901/2016-08-15/proxy/todo-list-service/todo-list/`
150-
151-
### 部署
152-
153-
接下来我们可以进行部署操作。
5+
1. 安装 serverless-devs
1546

1557
```bash
156-
$ s deploy
8+
curl -o- -L http://cli.so/install.sh | bash
1579
```
15810

159-
若发现配置有问题,可以修改对应的配置,然后利用 `deploy` 指令只部署修改后的配置,例如我们这里可以修改 s.yml 中函数的 `description` ,然后只部署函数配置:
11+
ref: [https://github.com/Serverless-Devs/docs/blob/master/docs/en/tool/install.md](https://github.com/Serverless-Devs/docs/blob/master/docs/en/tool/install.md)
16012

161-
```bash
162-
$ s deploy function --type config
163-
```
164-
165-
部署完成后,我们需要保存返回的 `url` 字段,供后续调用使用。
166-
167-
此外,我们可以通过 `info` 指令来获取部署后的资源信息。
168-
169-
```bash
170-
# 查看部署后的资源信息
171-
$ s info
172-
```
173-
174-
### 查看日志
175-
176-
利用 `logs` 指令能实时查看函数生成的日志,但是得在 s.yml 中的 `service` 下配置 `logConfig` 字段。本示例中已经配置了 `logConfig: auto`,这会在账号下自动生成一个 logproject 和 logstore,因此需要有对应的权限。
177-
178-
```bash
179-
# 实时查看日志
180-
$ s logs -t
181-
182-
# 查看某一时间段内的日志
183-
$ s logs -s 2021-07-22T12:00:00+08:00 -e 2021-07-23T10:00:00+08:00
184-
```
185-
186-
### 对服务进行访问
187-
188-
服务部署完成后,可以直接通过 `s invoke` 对线上服务发起调用,查看服务是否运行正常。也可以对线上服务进行压力测试:
189-
190-
```bash
191-
# 调用函数
192-
$ s invoke
193-
194-
# 压力测试
195-
$ s cli fc stress start --num-user 6 --spawn-rate 10 --run-time 30 --function-type http --url xxx --region cn-hangzhou
196-
```
197-
198-
压力测试后会返回简单的压测结果信息,若要查看详细的汇报信息,请打开生成的 html 文件:
199-
200-
```bash
201-
Html report flie: /Users/zqf/.s/cache/fc-stress/html/url#2021-07-23T09-59-41.html
202-
Execute 'open /Users/zqf/.s/cache/fc-stress/html/url#2021-07-23T09-59-41.html' on macos for html report with browser.
203-
Average: 17
204-
Fails: 0
205-
Failures/s: 0
206-
Max: 10111
207-
Method: GET
208-
Min: 7
209-
Name: /
210-
RPS: 333
211-
Requests: 9989
212-
p50: 13
213-
p60: 14
214-
p70: 16
215-
p90: 21
216-
p95: 25
217-
p99: 43
218-
```
219-
220-
### 可观测性
221-
222-
通过 `metrics` 指令来查看函数的运行指标:
13+
## 部署函数到云端
22314

22415
```bash
225-
$ s metrics
226-
[2021-07-23T10:21:29.023] [INFO ] [S-CLI] - Start ...
227-
[2021-07-23T10:21:30.444] [INFO ] [FC-METRICS] - Creating serivce: Metrics start...
228-
End of method: metrics
229-
[2021-07-23T10:21:30.625] [INFO ] [FC-METRICS] - Getting domain: http://localhost:3000, 请用浏览器访问Uri地址进行查看
16+
s deploy
23017
```
231-
232-
`s metrics` 指令会返回上述 url,访问该 url 就可以查看函数的指标信息。
233-
234-
### 发布
235-
236-
上述测试完成后,可以进行发布操作:
237-
238-
```bash
239-
# 发布版本
240-
$ s version publish --description 'this is a version 1'
241-
242-
# 发布别名
243-
$ s alias publish --alias-name testAlias --version 1
244-
```
245-
246-
发布完成后,可以查看已经发布的版本/别名。
247-
248-
```bash
249-
# 查看已经发布的版本信息
250-
$ s version list --table
251-
s
252-
# 查看已经发布的别名
253-
$ s alias list --table
254-
```
255-
256-
### CICD
257-
258-
若要想要将上述流程在自动化,可以选择 cicd 组件来生成 cicd 模版,目前支持生成 Github Action 模版和 Gitee Go 模版。以 Github Action 为例。
259-
260-
```bash
261-
# 初始化 Github Action 模版
262-
$ s cli cicd github
263-
```
264-
265-
上述指令会在当前项目下生成 .github/workflows/serverless-devs.yml 文件,文件内容:
266-
267-
```yaml
268-
name: Serverless Devs Project CI/CD
269-
270-
on:
271-
push:
272-
branches: [ main ]
273-
274-
jobs:
275-
serverless-devs-cd:
276-
runs-on: ubuntu-latest
277-
steps:
278-
- uses: actions/checkout@v2
279-
- uses: actions/setup-node@v2
280-
with:
281-
node-version: 12
282-
registry-url: https://registry.npmjs.org/
283-
- run: npm install
284-
- run: npm install -g @serverless-devs/s
285-
# 默认密钥配置指令是阿里云密钥配置指令,更多可以参考:
286-
# 如何通过Github Action使用Serverless Devs做CI/CD:http://short.devsapp.cn/cicd/github/action/usage
287-
# Serverless Devs的官网是通过Serverless Devs部署的: http://short.devsapp.cn/cicd/github/action/practice
288-
- run: s config add --AccountID ${{secrets.AccountID}} --AccessKeyID ${{secrets.AccessKeyID}} --AccessKeySecret ${{secrets.AccessKeySecret}} -a default
289-
- run: s deploy
290-
```
291-
292-
此时 push 到 main 分支的操作会触法上述流程,目前流程主要只会执行 `s deploy` 操作,若需要进行其他操作,需要手工添加 `- run: ${command}` 即可。
293-
294-
注:需要将阿里云密钥信息设置在对应 repo 中的 `Secrets` 中,包括: AccountID、AccessKeyID 和 AccessKeySecret 三个变量。
295-
296-
### 删除
297-
298-
最后,我们可以通过 `s remove service` 指令来删除上述部署的资源。

example/todolist-app/src/code/.fcignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

example/todolist-app/src/code/.gitignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

example/todolist-app/src/code/bootstrap

Lines changed: 0 additions & 2 deletions
This file was deleted.
File renamed without changes.

example/todolist-app/src/code/public/.gitignore renamed to example/todolist-app/src/public/.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
# testing
99
/coverage
1010

11-
# production
12-
# /build
13-
1411
# misc
1512
.DS_Store
1613
.env.local
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"files": {
3+
"main.css": "./static/css/main.b0fa5bce.css",
4+
"main.js": "./static/js/main.101af830.js",
5+
"main.js.map": "./static/js/main.101af830.js.map",
6+
"index.html": "./index.html",
7+
"precache-manifest.63d1998835c0736d4d045c7f44b46968.js": "./precache-manifest.63d1998835c0736d4d045c7f44b46968.js",
8+
"service-worker.js": "./service-worker.js",
9+
"static/css/main.b0fa5bce.css.map": "./static/css/main.b0fa5bce.css.map"
10+
},
11+
"entrypoints": [
12+
"static/css/main.b0fa5bce.css",
13+
"static/js/main.101af830.js"
14+
]
15+
}

0 commit comments

Comments
 (0)