|
| 1 | +# Triple OpenAPI Sample |
| 2 | + |
| 3 | +English | [中文](README_CN.md) |
| 4 | + |
| 5 | +This sample demonstrates how to enable OpenAPI documentation for Triple protocol services in Dubbo-go. With OpenAPI enabled, the Triple server automatically generates and serves OpenAPI documentation in both JSON and YAML formats, and provides built-in Swagger UI and ReDoc pages for easy browsing and testing of your RPC services. |
| 6 | + |
| 7 | +## What It Covers |
| 8 | + |
| 9 | +- Enabling OpenAPI via `triple.OpenAPIEnable()` inside `triple.WithOpenAPI()`. |
| 10 | +- Registering multiple proto-based services (including streaming) with OpenAPI support. |
| 11 | +- Registering services with different versions and OpenAPI groups. |
| 12 | +- Registering a non-proto (non-IDL) service alongside proto-based services. |
| 13 | + |
| 14 | +## Contents |
| 15 | + |
| 16 | +- `go-server/cmd/main.go` - The server that enables OpenAPI and registers multiple services. |
| 17 | +- `go-client/cmd/main.go` - The client that calls the greet service (unary, server stream, client stream, bidi stream). |
| 18 | +- `proto/greet/greet.proto` - Protobuf definition with unary and streaming RPCs. |
| 19 | +- `proto/demo/demo.proto` - Protobuf definition for demonstrating versioned service registration. |
| 20 | + |
| 21 | +## How to Run |
| 22 | + |
| 23 | +### Start the Server |
| 24 | + |
| 25 | +```shell |
| 26 | +go run ./go-server/cmd/main.go |
| 27 | +``` |
| 28 | + |
| 29 | +### Verify OpenAPI Documentation |
| 30 | + |
| 31 | +Once the server is running, you can access the OpenAPI documentation via the following URLs: |
| 32 | + |
| 33 | +| URL | Description | |
| 34 | +|-----|-------------| |
| 35 | +| `http://127.0.0.1:20000/dubbo/openapi/swagger-ui/` | Swagger UI page | |
| 36 | +| `http://127.0.0.1:20000/dubbo/openapi/openapi.json` | OpenAPI spec in JSON format | |
| 37 | +| `http://127.0.0.1:20000/dubbo/openapi/openapi.yaml` | OpenAPI spec in YAML format | |
| 38 | +| `http://127.0.0.1:20000/dubbo/openapi/api-docs/default.json` | Per-group OpenAPI spec (JSON) for the `default` group | |
| 39 | +| `http://127.0.0.1:20000/dubbo/openapi/api-docs/default.yaml` | Per-group OpenAPI spec (YAML) for the `default` group | |
| 40 | +| `http://127.0.0.1:20000/dubbo/openapi/redoc/index.html?group=default` | ReDoc documentation for the `default` group | |
| 41 | + |
| 42 | +For the `demo-2.0.0` group, replace `default` with `demo-2.0.0` in the URLs above. |
| 43 | + |
| 44 | +### Run the Client |
| 45 | + |
| 46 | +```shell |
| 47 | +go run ./go-client/cmd/main.go |
| 48 | +``` |
| 49 | + |
| 50 | +## Configuration Reference |
| 51 | + |
| 52 | +### Server-side OpenAPI Configuration |
| 53 | + |
| 54 | +`triple.WithOpenAPI()` is the entry point for OpenAPI configuration. It accepts the following options: |
| 55 | + |
| 56 | +| Option | Description | Default | |
| 57 | +|--------|-------------|---------| |
| 58 | +| `triple.OpenAPIEnable()` | Enable OpenAPI documentation generation. **Required** to activate OpenAPI. | `false` | |
| 59 | +| `triple.OpenAPIInfoTitle(title)` | Title of the OpenAPI document. | `"Dubbo-go OpenAPI"` | |
| 60 | +| `triple.OpenAPIInfoDescription(desc)` | Description of the OpenAPI document. | `"Dubbo-go OpenAPI"` | |
| 61 | +| `triple.OpenAPIInfoVersion(version)` | Version of the OpenAPI document. | `"1.0.0"` | |
| 62 | +| `triple.OpenAPIPath(path)` | Base URL path for serving OpenAPI endpoints. | `"/dubbo/openapi"` | |
| 63 | +| `triple.OpenAPIDefaultConsumesMediaTypes(types...)` | Default request content types. | `["application/json"]` | |
| 64 | +| `triple.OpenAPIDefaultProducesMediaTypes(types...)` | Default response content types. | `["application/json"]` | |
| 65 | +| `triple.OpenAPIDefaultHttpStatusCodes(codes...)` | Default HTTP status codes for responses. | `["200", "400", "500"]` | |
| 66 | +| `triple.OpenAPISettings(settings)` | Additional key-value settings. | `{}` | |
| 67 | + |
| 68 | +Example: |
| 69 | + |
| 70 | +```go |
| 71 | +srv, err := server.NewServer( |
| 72 | + server.WithServerProtocol( |
| 73 | + protocol.WithTriple( |
| 74 | + triple.WithOpenAPI( |
| 75 | + triple.OpenAPIEnable(), |
| 76 | + triple.OpenAPIInfoTitle("OpenAPI Service"), |
| 77 | + triple.OpenAPIInfoDescription("A service with OpenAPI documentation"), |
| 78 | + triple.OpenAPIInfoVersion("1.0.0"), |
| 79 | + ), |
| 80 | + ), |
| 81 | + protocol.WithPort(20000), |
| 82 | + ), |
| 83 | +) |
| 84 | +``` |
| 85 | + |
| 86 | +### Service-level OpenAPI Group |
| 87 | + |
| 88 | +When registering a service, you can assign it to a specific OpenAPI group via `server.WithOpenAPIGroup()`: |
| 89 | + |
| 90 | +```go |
| 91 | +demo.RegisterGreetServiceHandler(srv, &DemoTripleServerV2{}, |
| 92 | + server.WithOpenAPIGroup("demo-2.0.0"), |
| 93 | + server.WithVersion("2.0.0"), |
| 94 | +) |
| 95 | +``` |
| 96 | + |
| 97 | +Services without an explicit group fall into the `default` group. |
| 98 | + |
| 99 | +### Services Registered in This Sample |
| 100 | + |
| 101 | +| Service | Description | |
| 102 | +|---------|-------------| |
| 103 | +| `greet.GreetService` | Unary + streaming RPCs (server stream, client stream, bidi stream) | |
| 104 | +| `demo.GreetService` (v1.0.0) | Unary RPC with version `1.0.0` | |
| 105 | +| `demo.GreetService` (v2.0.0) | Same interface with version `2.0.0`, registered under OpenAPI group `demo-2.0.0` | |
| 106 | +| `com.example.UserService` | Non-proto (non-IDL) service, registered without protobuf | |
0 commit comments