install buf

install protoc-gen-openapiv2 and add the Go /bin to PATH env variable:

sudo go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest \
&& export PATH=$PATH:/Users/sargx/go/bin

Run:

protoc --openapiv2_out . --openapiv2_opt logtostderr=true --openapiv2_opt json_names_for_fields=false ./khonsuapis/horus/khonsu/v1/khonsu.proto

The swagger json file will be placed in the same folder of the .proto file.


You can also achieve the same results using buf directly:

https://buf.build/docs/installation

run where buf.gen.yaml is located:

buf generate

buf.gen.yaml

version: v1
managed:
  enabled: true
  go_package_prefix:
    default: github.com/ditioas/horus-protorepo-go
    except:
      - buf.build/googleapis/googleapis
      - buf.build/gogo/protobuf
      - buf.build/grpc-ecosystem/grpc-gateway
  java_multiple_files: true
plugins:
  - plugin: buf.build/protocolbuffers/go
    out: pb/go
    opt: paths=source_relative
  - plugin: buf.build/bufbuild/connect-go
    out: pb/go
    opt: paths=source_relative
  - plugin: buf.build/apple/swift
    opt: Visibility=Public
    out: pb/swift
  - plugin: buf.build/bufbuild/connect-swift
    opt: Visibility=Public
    out: pb/swift
  - plugin: buf.build/protocolbuffers/java
    out: pb/java
  - plugin: buf.build/bufbuild/connect-kotlin
    out: pb/java
    opt:
      - generateCallbackMethods=true
      - generateCoroutineMethods=true
  - plugin: openapiv2
    out: pb/swagger
    opt:
      - generate_unbound_methods=true
  - plugin: go-grpc
    out: pb/go
    opt:
      - paths=source_relative
  - plugin: grpc-gateway
    out: pb/go

https://buf.build/docs/installation


🌱 Back to Garden