Skip to content

Commit b76224b

Browse files
committed
Dockerize and Added tools for dockerizing
1 parent ed765cc commit b76224b

File tree

9 files changed

+265
-12
lines changed

9 files changed

+265
-12
lines changed

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
**/.git
2+
*/
3+
*
4+
!Dockerfile
5+
!konlpy_grpc_gateway/
6+
!tools/

Dockerfile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM golang:latest AS gateway-build
2+
WORKDIR /build
3+
COPY konlpy_grpc_gateway/go.mod konlpy_grpc_gateway/go.sum ./
4+
RUN go mod download
5+
COPY konlpy_grpc_gateway .
6+
RUN go build -o konlpy_grpc_gateway .
7+
8+
FROM python:latest AS swagger-merge
9+
WORKDIR /build
10+
COPY --from=gateway-build /build/_generated/ /build/konlpy_grpc_gateway/_generated/
11+
COPY tools/merge.swagger.py merge.swagger.py
12+
RUN python3 merge.swagger.py
13+
14+
FROM minhoryang/konlpy:v0.5.2 AS konlpy-grpc
15+
LABEL maintainer="Minho Ryang <[email protected]>"
16+
LABEL org.label-schema.schema-version="1.0"
17+
LABEL org.label-schema.name="minhoryang/konlpy-grpc"
18+
LABEL org.label-schema.description="KoNLPy gRPC/HTTP Server"
19+
LABEL org.label-schema.version="v0.1.0"
20+
21+
WORKDIR /app
22+
COPY --from=ochinchina/supervisord:latest /usr/local/bin/supervisord /usr/local/bin/supervisord
23+
COPY --from=gateway-build /build/konlpy_grpc_gateway /app/konlpy_grpc_gateway
24+
COPY --from=swagger-merge /build/konlpy_grpc_gateway/_generated/index.swagger.json /app/_generated/index.swagger.json
25+
COPY tools/supervisor.conf /app/supervisor.conf
26+
27+
# RUN python3 -m pip install konlpy-grpc
28+
RUN python3 -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ konlpy-grpc
29+
30+
CMD ["/usr/local/bin/supervisord", "-c", "supervisor.conf"]
31+
ENTRYPOINT []

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,18 @@ python -m pytest --grpc-real-server=[::]:50051
3434
python -m pytest --konlpy-repo=../konlpy
3535
```
3636

37+
## Release
38+
```bash
39+
rm -rf dist/
40+
poetry publish --build -r test
41+
poetry run twine upload --repository-url https://test.pypi.org/legacy/ dist/*
42+
```
43+
3744
## TODO
3845
- [x] [P0] client.py will be a konlpy-alike module.
3946
- [x] [P0] KoNLPy monkey-patcher
4047
- [x] [P1] Packaging with Poetry `pyproject.toml`.
41-
- [ ] PyPI Register
48+
- [x] PyPI Register
4249
- [ ] Find lowerbound-version of requirements. <!-- poetry debug:resolve -->
4350
- [P1] gRPC Proto Compile
4451
- [P1] In-house tool: `manage.py`
@@ -54,7 +61,7 @@ python -m pytest --konlpy-repo=../konlpy
5461
- [x] [P1] gRPC reflection
5562
- [P1] gRPC heartbeat
5663
- [x] [P1] gRPC Gateway (gRPC to JSON)
57-
- [P2] Dockerize / Register
64+
- [x] [P2] Dockerize / Register
5865
- k8s and istio?
5966
- [P2] CI
6067
- [P3] Button for deploying this to AWS/GCS/Azure now! (and connect by README.)

konlpy_grpc_gateway/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ go get -u github.com/minhoryang/KoNLPy-gRPC/konlpy_grpc_gateway
1212
go install github.com/minhoryang/KoNLPy-gRPC/konlpy_grpc_gateway
1313
$GOPATH/bin/konlpy_grpc_gateway -endpoint "[::]:50051" -swagger_dir _generated
1414
```
15-
It will run at http://localhost:8080
15+
It will run at http://localhost
16+
17+
18+
## Docker Build (gRPC and Gateway)
19+
```bash
20+
DOCKER_BUILDKIT=1 docker build --build-arg BUILDKIT_INLINE_CACHE=1 -t minhoryang/konlpy-grpc:v0.1.0 -f Dockerfile .
21+
```
22+
23+
## Docker Run (gRPC and Gateway)
24+
```bash
25+
docker run -it -p 50051:50051 -p 80:80 minhoryang/konlpy-grpc:v0.1.0
26+
```
1627

1728
## Thanks
1829
https://github.com/grpc-ecosystem/grpc-gateway

konlpy_grpc_gateway/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func main() {
2020

2121
ctx := context.Background()
2222
opts := gateway.Options{
23-
Addr: ":8080",
23+
Addr: ":80",
2424
GRPCServer: gateway.Endpoint{
2525
Network: *network,
2626
Addr: *endpoint,

poetry.lock

Lines changed: 171 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ homepage = "https://github.com/minhoryang/KoNLPy-gRPC"
1111
repository = "https://github.com/minhoryang/KoNLPy-gRPC"
1212
include = [
1313
"konlpy_grpc/_generated/*.py",
14+
"konlpy_grpc_gateway/README.md",
1415
]
1516
exclude = [
1617
"konlpy_grpc/_generated/.git*",
18+
"konlpy_grpc_gateway/*",
1719
]
1820

1921
[tool.poetry.dependencies]
@@ -22,8 +24,8 @@ grpcio-tools = "*"
2224
grpcio-reflection = "*"
2325
konlpy = [
2426
{ version = "^0.5.2", optional = true },
25-
{ git = "https://github.com/minhoryang/konlpy", branch = "pruned/tags_only", optional = true, develop = true },
26-
{ path = "../konlpy/", optional = true, develop = true },
27+
# { git = "https://github.com/minhoryang/konlpy", branch = "pruned/tags_only", optional = true, develop = true },
28+
# { path = "../konlpy/", optional = true, develop = true },
2729
]
2830
python = "^3.6"
2931

@@ -37,6 +39,7 @@ poetry = "^0.12"
3739
pytest = "*"
3840
pytest-cov = "*"
3941
pytest-grpc = "*"
42+
twine = "*"
4043

4144
[tool.poetry.extras]
4245
server = ["konlpy"]

tools/merge.swagger.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import glob
2+
import json
3+
import pathlib
4+
5+
ROOT = pathlib.Path.cwd() / "konlpy_grpc_gateway" / "_generated"
6+
RESULT = {}
7+
RESULT["swagger"] = "2.0"
8+
RESULT["info"] = {}
9+
RESULT["info"]["title"] = "KoNLPy Service"
10+
RESULT["info"]["version"] = "0.1.0"
11+
RESULT["consumes"] = ["application/json"]
12+
RESULT["produces"] = ["application/json"]
13+
RESULT["paths"] = {}
14+
RESULT["definitions"] = {}
15+
for swagger_file in ROOT.glob("*.swagger.json"):
16+
operation_id_prefix = swagger_file.name.split(".")[0]
17+
swagger_json = json.loads(swagger_file.read_bytes())
18+
for path in swagger_json["paths"].values():
19+
for method in path.values():
20+
method["operationId"] = f"{operation_id_prefix}{method['operationId']}"
21+
RESULT["paths"].update(swagger_json["paths"])
22+
RESULT["definitions"].update(swagger_json["definitions"])
23+
24+
(ROOT / "index.swagger.json").write_text(json.dumps(RESULT))

tools/supervisor.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[program:grpc]
2+
command = python3 -m konlpy_grpc server
3+
4+
[program:http]
5+
command = ./konlpy_grpc_gateway -alsologtostderr -endpoint localhost:50051 -swagger_dir _generated
6+
depends_on = grpc

0 commit comments

Comments
 (0)