File tree Expand file tree Collapse file tree 4 files changed +86
-14
lines changed Expand file tree Collapse file tree 4 files changed +86
-14
lines changed Original file line number Diff line number Diff line change @@ -22,7 +22,34 @@ autotest --config-file=${CONFIG_FILE}
22
22
autotest --config-file=${CONFIG_FILE} --env-file=${ENV_FILE}
23
23
```
24
24
## Example
25
- ### 1) start a fake api service
25
+ ### 1) start a fake http api service
26
+ ```
27
+ cd docker-compose
28
+ docker compose up -d
29
+ ```
30
+ #### Add
31
+ ```
32
+ curl -X POST 'http://localhost:8080/api/books' \
33
+ --header 'Content-Type: application/json' \
34
+ --data '{"title": "book3_title", "author": "book3_author"}'
35
+ ```
36
+
37
+ #### Delete
38
+ ```
39
+ curl -X DELETE 'http://localhost:8080/api/books/1'
40
+ ```
41
+
42
+ #### Modify
43
+ ```
44
+ curl -X PUT 'localhost:8080/api/books/3' \
45
+ --header 'Content-Type: application/json' \
46
+ --data '{"title": "book3_title", "author": "book3_author-2"}'
47
+ ```
48
+ #### List
49
+ ```
50
+ curl 'http://localhost:8080/api/books'
51
+ ```
52
+
26
53
27
54
### 2) run automated test cases
28
55
```
Original file line number Diff line number Diff line change @@ -21,7 +21,33 @@ autotest --config-file=${CONFIG_FILE} --env-file=${ENV_FILE}
21
21
```
22
22
23
23
## 示例
24
- ### 1) 启动一个api服务
24
+ ### 1) 启动一个伪造的http api服务
25
+ ```
26
+ cd docker-compose
27
+ docker compose up -d
28
+ ```
29
+ #### 添加
30
+ ```
31
+ curl -X POST 'http://localhost:8080/api/books' \
32
+ --header 'Content-Type: application/json' \
33
+ --data '{"title": "book3_title", "author": "book3_author"}'
34
+ ```
35
+
36
+ #### 删除
37
+ ```
38
+ curl -X DELETE 'http://localhost:8080/api/books/1'
39
+ ```
40
+
41
+ #### 修改
42
+ ```
43
+ curl -X PUT 'localhost:8080/api/books/3' \
44
+ --header 'Content-Type: application/json' \
45
+ --data '{"title": "book3_title", "author": "book3_author-2"}'
46
+ ```
47
+ #### 列表
48
+ ```
49
+ curl 'http://localhost:8080/api/books'
50
+ ```
25
51
26
52
### 2) 运行自动化测试用例
27
53
```
Original file line number Diff line number Diff line change
1
+ version : ' 2'
2
+
3
+ services :
4
+ httpapi :
5
+ image : woshiaotian/httpapi:latest
6
+ ports :
7
+ - ' 8080:8080'
Original file line number Diff line number Diff line change 1
- # build
2
- FROM golang:1.21 as builder
1
+ # 使用更新的 golang 基础镜像
2
+ FROM golang:1.21 AS builder
3
3
4
- RUN mkdir -p /tmp/http_api
5
- COPY go.mod /tmp/http_api/go.mod
6
- COPY go.sum /tmp/http_api/go.sum
7
- COPY main.go /tmp/http_api/main.go
4
+ # 设置工作目录
5
+ WORKDIR /app
8
6
9
- WORKDIR /tmp/http_api
10
- RUN go build -o /bin/httpapi -ldflags "-s -w" /tmp/http_api
7
+ # 复制 Go 模块文件和主文件
8
+ COPY go.mod go.sum ./
9
+ COPY main.go .
11
10
11
+ # 下载依赖
12
+ RUN go mod download
12
13
13
- FROM woshiaotian/simple-base-image:v0.1.8
14
+ # 使用 CGO_DISABLED=0 来构建 Go 二进制文件
15
+ RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o httpapi .
14
16
15
- WORKDIR /data
16
- COPY --from=builder /bin/httpapi /data/httpapi
17
+ # 使用轻量级的 Alpine Linux 基础镜像
18
+ FROM alpine:latest
17
19
18
- CMD ["/data/httpapi" ]
20
+ # 设置工作目录
21
+ WORKDIR /app
22
+
23
+ # 从构建阶段将二进制文件复制到最终镜像中
24
+ COPY --from=builder /app/httpapi .
25
+
26
+ # 暴露应用运行的端口
27
+ EXPOSE 8080
28
+
29
+ # 运行二进制文件
30
+ CMD ["./httpapi" ]
You can’t perform that action at this time.
0 commit comments