Skip to content

Commit a29d3ef

Browse files
author
Josh Newman
committed
feat: add config level host
1 parent e75efa6 commit a29d3ef

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ go install github.com/technicallyjosh/protoc-gen-openapi@latest
3535
| `content_type` | The content type to be associated with all operations.<sup>1</sup> | application/json |
3636
| `json_names` | Use the JSON names that Protobuf provides. Otherwise, proto field names are used. | false |
3737
| `json_out` | Create a JSON file instead of the default YAML. | false |
38+
| `host` | The host to be used for all operations.<sup>1</sup> | |
3839

39-
<sup>1</sup> _Can be overridden on a service or method._
40+
<sup>1</sup> _Can be overridden on a file, service, or method._
4041

4142
## Using Buf
4243

internal/generator/generator.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import (
1313

1414
// Config holds the configuration for the generator.
1515
type Config struct {
16-
Version *string
17-
Title *string
16+
ContentType *string
17+
DefaultResponse *string
1818
Description *string
19+
Host *string
1920
Ignore *string
20-
DefaultResponse *string
21-
ContentType *string
22-
UseJSONNames *bool
2321
JSONOutput *bool
22+
Title *string
23+
UseJSONNames *bool
24+
Version *string
2425
}
2526

2627
// Generator is an instance that parses the given folder and its Protobuf files into OAPI.

internal/generator/path.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ func newServer(host string) (*openapi3.Server, error) {
3636
func (g *Generator) addPathsToDoc(doc *openapi3.T, services []*protogen.Service) error {
3737
contentType := *g.config.ContentType
3838

39+
// Default to config defined host.
40+
host := *g.config.Host
41+
3942
for _, service := range services {
40-
var host, pathPrefix string
43+
var pathPrefix string
4144

45+
// Apply/Override file options.
4246
extFile := proto.GetExtension(service.Desc.ParentFile().Options(), oapiv1.E_File)
4347
if extFile != nil && extFile != oapiv1.E_File.InterfaceOf(oapiv1.E_File.Zero()) {
4448
fileOptions := extFile.(*oapiv1.FileOptions)
@@ -51,6 +55,7 @@ func (g *Generator) addPathsToDoc(doc *openapi3.T, services []*protogen.Service)
5155

5256
serviceOptions := new(oapiv1.ServiceOptions)
5357

58+
// Service options.
5459
extService := proto.GetExtension(service.Desc.Options(), oapiv1.E_Service)
5560
if extService != nil && extService != oapiv1.E_Service.InterfaceOf(oapiv1.E_Service.Zero()) {
5661
serviceOptions = extService.(*oapiv1.ServiceOptions)
@@ -91,6 +96,7 @@ func (g *Generator) addPathsToDoc(doc *openapi3.T, services []*protogen.Service)
9196

9297
var methodOptions *oapiv1.MethodOptions
9398

99+
// Method options. If not present, continue to next.
94100
extMethod := proto.GetExtension(method.Desc.Options(), oapiv1.E_Method)
95101
if extMethod != nil && extMethod != oapiv1.E_Method.InterfaceOf(oapiv1.E_Method.Zero()) {
96102
methodOptions = extMethod.(*oapiv1.MethodOptions)

main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ func main() {
1111
var flags flag.FlagSet
1212

1313
conf := generator.Config{
14-
Version: flags.String("version", "0.0.1", "Version of the API."),
15-
Title: flags.String("title", "", "Title of the API"),
14+
ContentType: flags.String("content_type", "application/json", "Default content-type for all paths."),
15+
DefaultResponse: flags.String("default_response", "", "Default response message to use for API responses not defined."),
1616
Description: flags.String("description", "", "Description of the API."),
17+
Host: flags.String("host", "", "Host to be used for all routes."),
1718
Ignore: flags.String("ignore", "", "Packages to ignore."),
18-
DefaultResponse: flags.String("default_response", "", "Default response message to use for API responses not defined."),
19-
ContentType: flags.String("content_type", "application/json", "Default content-type for all paths."),
20-
UseJSONNames: flags.Bool("json_names", false, "Use JSON names instead of the proto names of fields."),
2119
JSONOutput: flags.Bool("json_out", false, "Generate a JSON file instead of YAML."),
20+
Title: flags.String("title", "", "Title of the API"),
21+
UseJSONNames: flags.Bool("json_names", false, "Use JSON names instead of the proto names of fields."),
22+
Version: flags.String("version", "0.0.1", "Version of the API."),
2223
}
2324

2425
opts := protogen.Options{

0 commit comments

Comments
 (0)