20211026のGoに関する記事は1件です。

openapi-generator-cli (go-server)

こちらで行ったことと同じことを go-server で行いました。 openapi-generator-cli (python-flask) hello.yaml は同じです。 Go のサーバーを作成 npx @openapitools/openapi-generator-cli generate \ -g go-server \ -i ./hello.yaml \ -o ./src \ --api-package=api \ --model-package=model \ --additional-properties=withSeparateModelsAndApi=true コードの修正 go/api_hello_service.go /* * Hello * * ユーザ名を与えると挨拶を返してくれるAPI * * API version: 1.0.0 * Generated by: OpenAPI Generator (https://openapi-generator.tech) */ package openapi import ( "context" // "net/http" // "errors" ) // HelloApiService is a service that implements the logic for the HelloApiServicer // This service should implement the business logic for every endpoint for the HelloApi API. // Include any external packages or services that will be required by this service. type HelloApiService struct { } // NewHelloApiService creates a default api service func NewHelloApiService() HelloApiServicer { return &HelloApiService{} } // GetHello - func (s *HelloApiService) GetHello(ctx context.Context, userName string) (ImplResponse, error) { // TODO - update GetHello with the required logic for this service method. // Add api_hello_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. //TODO: Uncomment the next line to return response Response(200, InlineResponse200{}) or use other options such as http.Ok ... // return Response(200, InlineResponse200{}), nil return Response(200, InlineResponse200{"Hello, " + userName + "!" }), nil // return Response(200, InlineResponse200{}), userName //TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ... //return Response(400, nil),nil //TODO: Uncomment the next line to return response Response(500, {}) or use other options such as http.Ok ... //return Response(500, nil),nil // return Response(http.StatusNotImplemented, nil), errors.New("GetHello method not implemented") } サーバーの実行 go run main.go クライアントでアクセス $ http http://0.0.0.0:8080/hello?userName=Scott HTTP/1.1 200 OK Content-Length: 30 Content-Type: application/json; charset=UTF-8 Date: Tue, 26 Oct 2021 02:53:45 GMT { "HelloUser": "Hello, Scott!" }
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む