Skip to content

Commit

Permalink
Merge pull request #1 from vearne/feat/compose
Browse files Browse the repository at this point in the history
add docker compose file
  • Loading branch information
vearne authored May 6, 2024
2 parents 572aec2 + 8d2f167 commit 0b3c598
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 14 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,34 @@ autotest --config-file=${CONFIG_FILE}
autotest --config-file=${CONFIG_FILE} --env-file=${ENV_FILE}
```
## Example
### 1) start a fake api service
### 1) start a fake http api service
```
cd docker-compose
docker compose up -d
```
#### Add
```
curl -X POST 'http://localhost:8080/api/books' \
--header 'Content-Type: application/json' \
--data '{"title": "book3_title", "author": "book3_author"}'
```

#### Delete
```
curl -X DELETE 'http://localhost:8080/api/books/1'
```

#### Modify
```
curl -X PUT 'localhost:8080/api/books/3' \
--header 'Content-Type: application/json' \
--data '{"title": "book3_title", "author": "book3_author-2"}'
```
#### List
```
curl 'http://localhost:8080/api/books'
```


### 2) run automated test cases
```
Expand Down
28 changes: 27 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,33 @@ autotest --config-file=${CONFIG_FILE} --env-file=${ENV_FILE}
```

## 示例
### 1) 启动一个api服务
### 1) 启动一个伪造的http api服务
```
cd docker-compose
docker compose up -d
```
#### 添加
```
curl -X POST 'http://localhost:8080/api/books' \
--header 'Content-Type: application/json' \
--data '{"title": "book3_title", "author": "book3_author"}'
```

#### 删除
```
curl -X DELETE 'http://localhost:8080/api/books/1'
```

#### 修改
```
curl -X PUT 'localhost:8080/api/books/3' \
--header 'Content-Type: application/json' \
--data '{"title": "book3_title", "author": "book3_author-2"}'
```
#### 列表
```
curl 'http://localhost:8080/api/books'
```

### 2) 运行自动化测试用例
```
Expand Down
7 changes: 7 additions & 0 deletions docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '2'

services:
httpapi:
image: woshiaotian/httpapi:latest
ports:
- '8080:8080'
36 changes: 24 additions & 12 deletions example/http_api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
# build
FROM golang:1.21 as builder
# 使用更新的 golang 基础镜像
FROM golang:1.21 AS builder

RUN mkdir -p /tmp/http_api
COPY go.mod /tmp/http_api/go.mod
COPY go.sum /tmp/http_api/go.sum
COPY main.go /tmp/http_api/main.go
# 设置工作目录
WORKDIR /app

WORKDIR /tmp/http_api
RUN go build -o /bin/httpapi -ldflags "-s -w" /tmp/http_api
# 复制 Go 模块文件和主文件
COPY go.mod go.sum ./
COPY main.go .

# 下载依赖
RUN go mod download

FROM woshiaotian/simple-base-image:v0.1.8
# 使用 CGO_DISABLED=0 来构建 Go 二进制文件
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o httpapi .

WORKDIR /data
COPY --from=builder /bin/httpapi /data/httpapi
# 使用轻量级的 Alpine Linux 基础镜像
FROM alpine:latest

CMD ["/data/httpapi"]
# 设置工作目录
WORKDIR /app

# 从构建阶段将二进制文件复制到最终镜像中
COPY --from=builder /app/httpapi .

# 暴露应用运行的端口
EXPOSE 8080

# 运行二进制文件
CMD ["./httpapi"]

0 comments on commit 0b3c598

Please sign in to comment.