Skip to content

Commit

Permalink
add more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
peymanmortazavi committed May 30, 2024
1 parent d265841 commit 1ea047c
Show file tree
Hide file tree
Showing 16 changed files with 215 additions and 35 deletions.
19 changes: 1 addition & 18 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
# This is an example .goreleaser.yml file with some sensible defaults. Make sure to check the documentation at https://goreleaser.com

# The lines below are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/need to use them.
Expand Down Expand Up @@ -68,22 +67,6 @@ archives:
- goos: windows
format: zip

nfpms:
- id: protoc-gen-grpc-api-gateway
package_name: protoc-gen-grpc-api-gateway
vendor: Mesh API
maintainer: Peyman Mortazavi <[email protected]>
description: Protoc plug-ings for generating reverse proxy code translating HTTP to gRPC.
license: GPL-3.0
formats:
- apk
- deb
- rpm
- archlinux
builds:
- "protoc-gen-grpc-api-gateway"
- "protoc-gen-openapiv3"

changelog:
sort: asc
filters:
Expand Down
4 changes: 2 additions & 2 deletions codegen/internal/descriptor/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (g *GatewayFileLoadOptions) addFlags(flags *flag.FlagSet) {
&g.GlobalGatewayConfigFile,
"gateway_config",
g.GlobalGatewayConfigFile,
"(optional) path to the gateway config file that gets loaded first.")
"specifies the path to the global gateway config file that is loaded first. This file can contain bindings for any service.")

flags.StringVar(
&g.FilePattern,
Expand Down Expand Up @@ -91,5 +91,5 @@ func (r *RegistryOptions) AddFlags(flags *flag.FlagSet) {

flag.BoolVar(
&r.AllowDeleteBody, "allow_delete_body", r.AllowDeleteBody,
"unless set, HTTP DELETE methods may not have a body")
"by default, HTTP DELETE methods may not include a body unless explicitly specified.")
}
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ In essence, gRPC Gateway streamlines the exposure of gRPC services to clients th

## Rationale

This project aims to implement features that the [gRPC Gateway](https://grpc-ecosystem.github.io/grpc-gateway) project does not plan to cover. These include bidirectional streaming APIs, dynamic reverse proxy capabilities, and full support for OpenAPI v3.1 specification. These additions enhance the versatility and power of the gRPC API Gateway, providing users with a comprehensive solution for their API gateway needs.

:heart: Inspiration and Gratitude:
This project is deeply inspired by the widely recognized [gRPC Gateway](https://grpc-ecosystem.github.io/grpc-gateway).
Building upon its established and stable implementation has enabled rapid development of similar features,
allowing us to focus on introducing new functionality.
We extend our sincere thanks to the creators of the gRPC Gateway for laying the foundation.

This project aims to implement features that the gRPC Gateway project does not plan to cover. These include bidirectional streaming APIs, dynamic reverse proxy capabilities, and full support for OpenAPI v3.1 specification. These additions enhance the versatility and power of the gRPC API Gateway, providing users with a comprehensive solution for their API gateway needs.

## Key Differences

The differences can be summerized into the following points
Expand Down
20 changes: 16 additions & 4 deletions docs/installation.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
You can directly download the binaries from the [Releases](https://github.com/meshapi/grpc-api-gateway/releases) page.

The link above includes the proto files containing the annotations as well.

Package repositories below also contain the binaries:

#### Arch

This plug-in is not part of the official repository but there is a package in the user repository that can be used:

[protoc-gen-grpc-api-gateway-bin](https://aur.archlinux.org/packages/protoc-gen-grpc-api-gateway-bin)

This can be installed using `makepkg`:

```sh
$ pacman -S grpc-api-gateway-bin
$ git clone https://aur.archlinux.org/protoc-gen-grpc-api-gateway-bin.git
$ cd protoc-gen-grpc-api-gateway-bin && makepkg -si
```

#### Alpine

```sh
$ apk add grpc-api-gateway-bin
```
This is an on-going effort and should be available soon, at the moment, you can download the binaries from
[Releases](https://github.com/meshapi/grpc-api-gateway/releases) page or install from source:

#### Install from source

Expand All @@ -22,3 +30,7 @@ you can install from the source using Go
```sh
$ go install github.com/meshapi/grpc-api-gateway@<version>
```

### Docker

If you wish to install this tool inside docker, you can use `wget` or `curl` to download the binaries for the intended architecture. Take a look at the [Releases](https://github.com/meshapi/grpc-api-gateway/releases).
2 changes: 1 addition & 1 deletion docs/quickstart/http_binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ To do this, we need to define gRPC method to HTTP endpoint bindings.

Generally this can be either via gRPC API Gateway proto extensions or via a configuration file.

The way configurations get loaded can be heavily customized. Refer to [Reference](/reference) to learn more.
The way configurations get loaded can be heavily customized. Refer to [Configuration](/grpc-api-gateway/reference/configuration) to learn more.
The default behavior is that for any proto file `file.proto`, files `file_gateway.yaml`, `file_gateway.yml` and `file_gateway.json` will be tried in order. If any file is available, it will be consumed.

It is a matter of personal preference to choose the method that works best for you and your project.
Expand Down
4 changes: 2 additions & 2 deletions docs/quickstart/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
There is a lot of customization and details to gRPC API Gateway, however this quick step by step guide aims to show
you the quickest way to generate a reverse proxy for your gRPC service.

See [Reference](/reference) for a complete reference of all features and configurations.
See [Reference](/grpc-api-gateway/reference/intro) for a complete reference of all features and configurations.

## Prerequisites

Before we get to coding, ensure that you have installed the gRPC API Gateway protobuf plug-in.
Refer to [Installation](/installation) section for related instructions.
Refer to [Installation](/grpc-api-gateway/installation) section for related instructions.

gRPC API Gateway is a `protoc` plug-in, similar to the Go code generator and the tool
relies on the generated code from both [Go](https://protobuf.dev/reference/go/go-generated/)
Expand Down
2 changes: 1 addition & 1 deletion docs/quickstart/openapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ However, we might like to change a few things in this document.

In this page we will customize our OpenAPI document for this `Echo` service.

Refer to [Reference](/reference) to learn more about all of the customizations.
Refer to [Reference](/grpc-api-gateway/reference/intro) to learn more about all of the customizations.

Similar to defining HTTP bindings, you can opt to use the annotations and directly define options in the proto
files or use configuration files.
Expand Down
3 changes: 0 additions & 3 deletions docs/reference.md

This file was deleted.

155 changes: 155 additions & 0 deletions docs/reference/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Configuration

Significant effort has gone into designing a flexible configuration system with sensible defaults.

Both the _gRPC API Gateway_ and _OpenAPI v3.1_ plug-ins offer extensive customization options through configurations embedded directly in `.proto` files or through separate configuration files in either `YAML` or `JSON` format.

Throughout this documentation, you will find examples provided in both proto annotations and `YAML` configuration file formats.

## Mix and Match

You can mix and match these two methods, setting some configurations in `proto` files and using separate configuration files. However, there are some rules to consider:

!!! note
When both proto files and configuration files set an option:

* __If the value is an _object_ or an _array_ type, the result is a merge of both settings.__

??? example
If `schema.description` is set in a proto file for a message type and `schema.summary` is set in a configuration file for the same message, the result would contain both `summary` and `description`.

* __If the value is a simple type such as _string_, _boolean_ or _number_ the configuration file takes precedence.__

??? example
_For instance_: if `schema.description` is set in a proto file for a message type and a configuration file also sets `schema.description` for the same message, the value from the configuration file is used.

## Using proto annotations

When using proto annotations, you will need to import the proto annotations and types for the `gRPC API Gateway`.

[Buf](https://buf.build/) is a tool that simplifies the development and consumption of the Protobuf APIs.
It manages dependencies and builds proto files efficiently.

All proto files and annotations are available on [buf.build](https://buf.build/meshapi/grpc-api-gateway).

If you decide to use Buf, follow the instructions below or you can visit the `protoc` tab for
instructions on using protoc.

=== "Using Buf"

Let's create a `buf.gen.yaml` file if you do not already have one
with the following content or add `buf.build/meshapi/grpc-api-gateway` in your dependencies if you have an existing one:

```yaml title="buf.yaml" linenums="1"
version: v1
deps:
- "buf.build/meshapi/grpc-api-gateway"
```

Update mods to download the proto files:

```sh
$ buf mod update
```

=== "Using protoc"

You will first need to download the proto files for `gRPC API Gateway`.
File named `grpc_api_gateway_proto.tar.gz` in the [Releases](https://github.com/meshapi/grpc-api-gateway/releases) page contains all the necessary proto files.

From now on, use the `-I` or `--proto-path` option to include these proto files if they reside outside of the proto search path.


In any proto file you wish to use annotations, use the import line below when wanting to use gateway or openapi options:

```proto
import "meshapi/gateway/annotations.proto";
```


## Using configuration files

Each proto file can have a single accompanying configuration file in _YAML_ or _JSON_ format
that gets loaded along with the proto file. As the plug-in processes each proto file, it will use the _config file pattern_
setting to determine a configuration file name and will try three file extensions `.yaml`, `.yml` and `.json` in that order.
If a file exists, the configuration file gets loaded and the search ends.

!!! note
Both proto annotations and configurations files offer the same options in different formats. Thus there is no option
that can be set using one method that cannot be set with the other.

### Search Path

Since _protoc_ compiler does not provide the full path to the proto files, it is __NOT__ possible for the plug-ins to
know the exact path of each proto file and cannot determine which directory to search for the configuration file.
To address this issue, you can set _search paths_ in the command line options for either plug-in to set
the root directory where these configurations live.

Search paths essentially set the root directory that will be used to search for configuration files.
The default value will work for the majority of the use cases. However, if you want to place configuration files
in a separate directory than your proto files or your file structures are more complex, you can use the _search paths_
to direct the plug-ins where they should search.

__Default Value__: default value is always `.` which means the current working directory of the `protoc` compiler.

??? example

Imagine the following structure:

```
project/
proto/
models.proto
```

Assuming `protoc` is callled from the `project` directory:

* If _search path_ is `.` (default value): directory `proto/` will be searched for the relative configuration file.
* If _search path_ is `configs`, directory `configs/proto/` will be searched for the relative configuration file.

### Filename Pattern

We have discussed the search path and the directory that will be used to search for finding a
relative configuration file for proto files. What is the file name? This setting can be used using command line flags in
either plug-in to specify a convention based on the name of the proto file. To remain flexible, this value is a Go template
value.

!!! note
This name must __NOT__ contain the file extension since the tool
itself will append and look for `.yaml`, `yml` and `.json` extensions in that order.

__Default Value__: default value is always `{{ .Path }}_gateway` for both OpenAPI and API Gateway configuration files.

This is a default but it can be changed according to your preferred file organization pattern.

#### Go Template Values

The following values are available to use in the configuration filename pattern.

The value column shows the value for an example proto file `proto/myservice/v1/model.proto`.

| Expression | Description | Value |
| ----------- | ------------------------------------ | ------- |
| `{{.Name}}` | is the base file name (no parent directorry) excluding `.proto` extension | `model` |
| `{{.Path}}` | is the relative path to the file but excluding the `.proto` extension | `proto/myservice/v1/model` |
| `{{.Dir}}` | is the relative path to the parent directory of the related proto file | `proto/myservice/v1` |

!!! example
If _search path_ is `/path/to/configs` and _file pattern_ is `{{.Name}}_gateway`. Associated configuration
file name (omitting file extension) for proto file `cool_service/v1/service.proto` is
`/path/to/configs/cool_service/v1/service_gateway`.

!!! warning
Each `.proto` file can be associated with a single configuration file,
meaning only one configuration will be loaded per .proto file.
However, the same configuration file can be used by multiple `.proto` files if the configuration
file pattern is customized (e.g. using `{{.Dir}}` expression).

It is truly a matter of personal preference which method you would like to use to customize
the gateway and/or the OpenAPI objects. It might be worth noting the following:

* A [JSON schema](https://json.schemastore.org/grpc-api-gateway.json) exists for YAML/JSON files so you benefit from autocompletion
if you have installed the proper YAML/JSON extension.

* With many customization, proto files can get bloated. Separating the proto definitions from the gateway and OpenAPI configurations
can help with the organization of files.
21 changes: 21 additions & 0 deletions docs/reference/grpc/cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Plug-in Options

_gRPC API Gateway_ protoc plug-in has a number of options. Table below lists them all.

| <div style="width:175px">Option</div> | Description | <div style="width:130px">Default</div> |
| --- | --- | --- |
| `allow_delete_body` | By default, HTTP DELETE methods may not include a body unless explicitly specified. | `false` |
| `allow_patch_feature` | Determines whether to use PATCH feature involving update masks (using google.protobuf.FieldMask). | `true` |
| `config_search_path` | The gateway config search path is the directory (relative or absolute) from the current working directory that contains the gateway config files. See [Search Path](/grpc-api-gateway/reference/configuration/#search-path) for more information. | `.` |
| `gateway_config` | Specifies the path to the global gateway config file that is loaded first. This file can contain bindings for any service. | no default |
| `gateway_config_pattern` | The gateway file pattern (excluding the extension) used to load a gateway config file for each proto file. The extensions `.yaml`, `.yml`, and `.json` will be tried in that order. See [Filename Pattern](/grpc-api-gateway/reference/configuration/#filename-pattern) to learn more. | `{{.Path}}_gateway` |
| `generate_local` | __`Experimental`__ Generates code to directly use the server implementation instead of using gRPC clients | `false` |
| `generate_unbound_methods` | Determines whether unannotated RPC methods should be included in the proxy. Methods without explicit HTTP bindings will default to POST and will have the route pattern `/<grpc-service>/<method>`. | `false` |
| `log_file` | If specified, plug-in writes all logs to this file instead. | no default |
| `log_level` | Sets the log level, levels: `warning`, `info`, `trace` and `silent` | `warning` |
| `omit_package_doc` | If true, no package comment will be included in the generated code | `false` |
| `register_func_suffix` | Used to construct names of generated `Register*<Suffix>` methods. | `Handler` |
| `repeated_path_param_separator` | Configures how repeated fields should be split. Allowed values are `csv`, `pipes`, `ssv` and `tsv`. | `csv` |
| `request_context` | Determine whether to use HTTP request's context or not. | `true` |
| `standalone` | Generates a standalone gateway package, which imports the target service package | `false` |
| `warn_on_unbound_methods` | Emits a warning message if an RPC method has no mapping. | `false` |
Empty file added docs/reference/grpc/query.md
Empty file.
Empty file added docs/reference/grpc/route.md
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions docs/reference/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Introduction

This page provides documentation and code examples for utilizing OpenAPI v3.1 and gRPC API Gateway plug-ins.

This project includes two `protoc` plug-ins:

1. **`protoc-gen-grpc-api-gateway`**: This plug-in generates a reverse proxy in Go, leveraging the code generated by the [Go](https://protobuf.dev/reference/go/go-generated/) and [Go gRPC](https://grpc.io/docs/languages/go/quickstart/) plug-ins. The reverse proxy offers an HTTP handler that translates HTTP requests into gRPC.

2. **`protoc-gen-openapiv3`**: This plug-in generates an OpenAPI v3.1 document for the reverse proxy HTTP server. Since OpenAPI v3.1 is designed for RESTful HTTP APIs, this plug-in does not currently support WebSockets, which means some streaming endpoints may be excluded from the generated document.
1 change: 0 additions & 1 deletion examples/internal/proto/openapi_config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# yaml-language-server: $schema=/home/peyman/projects/meshapi/grpc-api-gateway/api/Config.schema.json
openapi:
document:
info:
Expand Down
6 changes: 5 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ nav:
- quickstart/generation.md
- quickstart/http_binding.md
- quickstart/openapi.md
- reference.md
- Reference:
- reference/intro.md
- reference/configuration.md
- Gateway:
- reference/grpc/cli.md

theme:
name: material
Expand Down

0 comments on commit 1ea047c

Please sign in to comment.