Skip to content

Commit

Permalink
Update gen list, move to buf
Browse files Browse the repository at this point in the history
  • Loading branch information
LeKovr committed Sep 28, 2022
1 parent 3827cc9 commit 5588b0d
Show file tree
Hide file tree
Showing 10 changed files with 447 additions and 90 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Docker

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

on:
push:
branches: [ main ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}

jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64, linux/arm/v7, linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
100 changes: 50 additions & 50 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@


# Docker image versions
ARG alpine=3.11
ARG go=1.14.0
ARG go_ver=v1.18.5-alpine3.16.2
ARG buf_ver=1.7.0

# Docker images
ARG go_img=ghcr.io/dopos/golang-alpine

# Package versions
ARG grpc=1.24.0
ARG gen_gogo=1.3.1
ARG gen_gateway=1.14.3
ARG gen_validator=0.3.0
ARG gen_wsdl=0.8.3
ARG gen_soap=0.4.5
ARG gen_doc=1.3.1
ARG gen_ver=v1.28.1
ARG gen_grpc_ver=v1.2.0
ARG gen_doc_ver=v1.5.1
ARG gen_gateway_ver=v2.11.3
ARG gen_gateway_ts_ver=v1.1.2
ARG gen_validate_ver=v0.6.7
ARG gowrap_ver=v1.2.7
ARG esbuild_ver=v0.15.9

# Build all with golang image
FROM golang:${go}-alpine${alpine} AS builder

# Declare args used for build
ARG grpc
ARG gen_gogo
ARG gen_gateway
ARG gen_validator
ARG gen_wsdl
ARG gen_soap
ARG gen_doc
FROM ${go_img}:${go_ver} as golang

# Speed up build if proxy given
ARG GOPROXY
RUN echo $GOPROXY
ENV CGO_ENABLED=0 GO111MODULE=on

RUN apk --update add git
ARG gen_ver
ARG gen_grpc_ver
ARG gen_doc_ver
ARG gen_gateway_ver
ARG gen_gateway_ts_ver
ARG gen_validate_ver
ARG gowrap_ver
ARG esbuild_ver

WORKDIR /out/include
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@${gen_ver}
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@${gen_grpc_ver}
RUN go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@${gen_doc_ver}
RUN go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@${gen_gateway_ver}
RUN go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@${gen_gateway_ver}
RUN go install github.com/grpc-ecosystem/protoc-gen-grpc-gateway-ts@${gen_gateway_ts_ver}
RUN go install github.com/envoyproxy/protoc-gen-validate@${gen_validate_ver}

# nats-nrpc has no version yet
RUN go get -u github.com/nats-rpc/nrpc/protoc-gen-nrpc
# nrpc.proto used in .proto for nats options
RUN install -m 444 -D /go/src/github.com/nats-rpc/nrpc/nrpc.proto -t github.com/nats-rpc/nrpc
# non protoc generators

ENV GO111MODULE on
RUN go install github.com/hexdigest/gowrap/cmd/gowrap@${gowrap_ver}
RUN go install github.com/evanw/esbuild/cmd/esbuild@${esbuild_ver}

RUN go get -u google.golang.org/grpc@v${grpc}
RUN go get -u github.com/gogo/protobuf/protoc-gen-gogo@v${gen_gogo}
RUN go get -u github.com/gogo/protobuf/protoc-gen-gogofast@v${gen_gogo}
RUN install -m 444 -D $(find /go/pkg/mod/github.com/gogo/protobuf@v*/gogoproto/*.proto) -t github.com/gogo/protobuf/gogoproto
RUN install -m 444 -D $(find /go/pkg/mod/github.com/gogo/protobuf@v*/protobuf/google/protobuf/*.proto) -t github.com/gogo/protobuf/protobuf/google/protobuf
FROM bufbuild/buf:${buf_ver} as buf

RUN go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway@v${gen_gateway}
RUN go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@v${gen_gateway}
RUN install -m 444 -D $(find /go/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v*/third_party/googleapis/google/rpc -name '*.proto') -t google/rpc/
RUN install -m 444 -D $(find /go/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v*/third_party/googleapis/google/api -name '*.proto') -t google/api/
RUN install -m 444 -D $(find /go/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v*/protoc-gen-swagger/options -name '*.proto') -t protoc-gen-swagger/options
ARG gen_gateway_ver

RUN go get -u github.com/mwitkow/go-proto-validators/protoc-gen-govalidators@v${gen_validator}
RUN install -m 444 -D $(find /go/pkg/mod/github.com/mwitkow/go-proto-validators@v* -name '*.proto') -t github.com/mwitkow/go-proto-validators
COPY --from=golang /go/bin /go/bin
RUN ls -l /go/bin

RUN go get -u github.com/UNO-SOFT/soap-proxy/protoc-gen-wsdl@v${gen_wsdl}
RUN go get -u github.com/UNO-SOFT/grpcer/protoc-gen-grpcer@v${gen_soap}
WORKDIR /app
# sample .proto for buf mod update
COPY --from=golang /go/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@${gen_gateway_ver}/examples/internal/proto/examplepb/echo_service.proto ./proto/service.proto

RUN go get -u github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@v${gen_doc}
#RUN apk add --no-cache libstdc++

FROM alpine:$alpine
# Prepare buf cache

RUN apk --update --no-cache add protobuf libxml2-utils
COPY buf.* ./
RUN buf mod update
COPY template.tmpl .
COPY proto.config.swagger.yaml .

COPY --from=builder /go/bin/protoc* /usr/local/bin/
COPY --from=builder /out/include /usr/local/include
ENV PATH="/go/bin:${PATH}"
RUN buf --debug generate --template buf.gen.yaml --path proto

ENTRYPOINT ["protoc", "-I/usr/local/include", "-I/usr/local/include/github.com/gogo/protobuf/protobuf"]
ENTRYPOINT ["/usr/local/bin/buf"]
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

BUF_IMG ?= buf

docker-buf:
docker run -v `pwd`:/mnt/pwd -w /mnt/pwd $(BUF_IMG) generate --template buf.gen.yaml --path proto

buf:
docker run --rm -it -v `pwd`:/mnt/pwd -w /mnt/pwd $(BUF_IMG) $(CMD)

bufe:
docker run --rm -it --entrypoint /bin/sh -v `pwd`:/mnt/pwd -w /mnt/pwd $(BUF_IMG) $(CMD)

docker:
docker build --tag $(BUF_IMG) --file Dockerfile .

# buf update dependencies
buf-build:
buf mod update
buf build

swagger-ui:
docker-compose -f docker-compose.yaml up
84 changes: 44 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,61 @@
# dockerfile-protoc-go
Processing .proto for golang project via docker image
# gogens
Set of golang generators for GRPC projects

## Plugins
## Generators

This image has protobuf package from alpine distributive and the following plugins:
### Packages used via [buf.build](https://buf.build/)

* [grpc](https://google.golang.org/grpc)
* [gogo/protobuf](https://github.com/gogo/protobuf)
* [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway)
* [go-proto-validators](https://github.com/mwitkow/go-proto-validators)
* [nrpc](https://github.com/nats-rpc/)
* [soap-proxy](https://github.com/UNO-SOFT/soap-proxy)
* [grpcer](https://github.com/UNO-SOFT/grpcer)
* [proto docs](https://github.com/pseudomuto/protoc-gen-doc)
* [protobuf](https://google.golang.org/protobuf)
* [google grpc](https://google.golang.org/grpc)
* [protoc-gen-doc](https://github.com/pseudomuto/protoc-gen-doc)
* protoc-gen-grpc-gateway and protoc-gen-openapiv2 from [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway)


* [protoc-gen-grpc-gateway-ts](https://github.com/grpc-ecosystem/protoc-gen-grpc-gateway-ts)
* [protoc-gen-validate](https://github.com/envoyproxy/protoc-gen-validate)

### non protoc generators

* [gowrap](https://github.com/hexdigest/gowrap)
* [esbuild](https://github.com/evanw/esbuild)

## Usage

For ./proto/messages.proto run command
### Generate GRPC code

For ./proto/service.proto run command

```
docker run -ti --rm \
-w $PWD \
-v $PWD:$PWD \
tenderpro/protoc-go -I=./proto \
--gogofast_out=plugins=grpc:./proto/ \
--grpc-gateway_out=logtostderr=true:./proto/ \
--swagger_out=logtostderr=true:./assets/ \
--grpcer_out=soap:soap \
--wsdl_out=cmd/webserver/ \
--nrpc_out=. \
--doc_out=docs \
--doc_opt=markdown,messages.md \
messages.proto
docker run --rm -v `pwd`:/mnt/pwd -w /mnt/pwd gogens generate --template buf.gen.yaml --path proto
```
which will generate:
Result:

* proto/README.md - markdown docs for .proto
* zgen/go/proto - golang code
** service_grpc.pb.go - gRPC service
** service.pb.go
** service.pb.gw.go - JSON service
** service.pb.validate.go - API validator
* zgen/ts - typescript code
** proto/service.pb.ts - TS client
** fetch.pb.ts - fetch lib

### Generate JS client

* proto/messages.pb.go - gRPC service
* proto/messages.pb.gw.go - JSON service
* assets/messages.swagger.json - openapi definition
* soap/messages.grpcer.go - SOAP service
* cmd/webserver/messages.wsdl - WSDL file for SOAP service
* cmd/webserver/messages.wsdl.go - WSDL for SOAP service as golang variable
* messages.nrpc.go - NATS service (TODO: this file is obsolete and needs update)
* docs/messages.md - markdown docs for .proto, there are [several formats](https://github.com/pseudomuto/protoc-gen-doc#invoking-the-plugin) available
For generated zgen/ts files run

## Plans
```
docker run --rm -v `pwd`:/mnt/pwd -w /mnt/pwd gogens --entrypoint /opt/go/esbuild \
service.pb.ts --bundle \
--outfile=/mnt/pwd/static/js/api.js --global-name=AppAPI
```

The following plugins might be added in future:
Result:

* [protoc-gen-gotemplate](https://github.com/moul/protoc-gen-gotemplate)
* [envoyproxy/protoc-gen-validate](https://github.com/envoyproxy/protoc-gen-validate)
* [buf.build](https://buf.build/)
* static/js/api.js - service JS client for use in browser

## Thanks

* https://github.com/higebu/docker-protoc-go
* https://github.com/TheThingsIndustries/docker-protobuf
* https://github.com/eugene-bert/docker-buf
63 changes: 63 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
version: v1
plugins:
# ##############################################################################
# protoc-gen-go
# https://pkg.go.dev/google.golang.org/protobuf/cmd/protoc-gen-go
# #############################################################################
- name: go
out: .
opt:
- paths=source_relative
# ##############################################################################
# protoc-gen-go-grpc
# https://pkg.go.dev/google.golang.org/grpc/cmd/protoc-gen-go-grpc#section-readme
# #############################################################################
- name: go-grpc
out: .
opt:
- paths=source_relative
##############################################################################
# protoc-gen-validate (PGV)
# https://github.com/envoyproxy/protoc-gen-validate
# #############################################################################
- name: validate
out: .
opt:
- lang=go
- paths=source_relative
# ##############################################################################
# gRPC-Gateway grpc-gateway & openapi
# https://github.com/grpc-ecosystem/grpc-gateway
# https://buf.build/grpc-ecosystem/plugins/openapiv2
# https://buf.build/grpc-ecosystem/plugins/grpc-gateway
# #############################################################################
- name: grpc-gateway
out: .
opt:
- paths=source_relative
- name: openapiv2
out: .
opt:
- output_format=json
- allow_merge=true
- merge_file_name=book
- openapi_configuration=proto.config.swagger.yaml
strategy: all
# ##############################################################################
# protoc-gen-doc
# https://github.com/pseudomuto/protoc-gen-doc
# #############################################################################
- name: doc
out: docs/generated/
opt: template.tmpl,service.md
strategy: all
# ##############################################################################
# protoc-gen-grpc-gateway-ts
# https://github.com/grpc-ecosystem/protoc-gen-grpc-gateway-ts
# #############################################################################
- name: grpc-gateway-ts
out: static/js
strategy: all
opt:
- ts_import_roots=.
- ts_import_root_aliases=base
15 changes: 15 additions & 0 deletions buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated by buf. DO NOT EDIT.
version: v1
deps:
- remote: buf.build
owner: envoyproxy
repository: protoc-gen-validate
commit: 45685e052c7e406b9fbd441fc7a568a5
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 8d7204855ec14631a499bd7393ce1970
- remote: buf.build
owner: grpc-ecosystem
repository: grpc-gateway
commit: bc28b723cd774c32b6fbc77621518765
3 changes: 3 additions & 0 deletions buf.work.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: v1
directories:
- proto
Loading

0 comments on commit 5588b0d

Please sign in to comment.