zeromicro/goctl-swagger

[Improvement] Generate Single Swagger file from multiple microservice's *.api

a0v0 opened this issue · 0 comments

a0v0 commented

Maintaining swagger of multiple microservices is a tedious task. goctl-swagger should generated a single swagger file by introspecting api definition of each service.

I have two microservices user and email in app folder in the root directory of the project.

app/user/api/user.api looks like this

type (
	CreateAccountResponse {
		Id string `json:"id"`
	}
)

service user {
	@doc(
		summary: "Create a new user account",
	)
	@handler createAccount
	post /user/account returns (CreateAccountResponse)
	
}

app/email/api/email.api looks like this

type (
	CreateEmailResponse {
		Id string `json:"id"`
	}
)

service email {
	@doc(
		summary: "Create a new email",
	)
	@handler createEmail
	post /email returns (CreateEmailResponse)
	
}

go-swagger should find all the *.api files (in my case user.api and email.api) and merge them into one merged.api with the content as shown below

info(
	title: "type title here"
	desc: "type desc here"
	author: "type author here"
	email: "type email here"
	version: "type version here"
)

type (
    // extracted from user.api
	CreateAccountResponse {
		Id string `json:"id"`
	}

    // extracted from email.api
    CreateEmailResponse {
		Id string `json:"id"`
	}
)

service organization_name {
     // extracted from user.api
	@doc(
		summary: "Create a new user account",
	)
	@handler createAccount
	post /user/account returns (CreateAccountResponse)

	 // extracted from email.api
   @doc(
		summary: "Create a new email",
	)
	@handler createEmail
	post /email returns (CreateEmailResponse)


}

go-swagger can easily generate single swagger file from this merged.api file.

This is rather a simple task by dumping everything inside type ( <contentToBeDumped> ) and service svcname { <contentToBeDumped> } to a new merged.api file. But I don't know how. I was able to find all the files with .api extension with the below command but dont know what to do after that.
find app -type f -name "*.api"

can somewhen take a look at this.