## 参考
[【AI Shift Advent Calendar 2021】OpenAPI SpecificationをGoで使ってみます \| 株式会社AI Shift](https://www.ai-shift.co.jp/techblog/2347)
## インストール
```console
# パッケージ
go get github.com/deepmap/oapi-codegen
# コマンド
go install github.com/deepmap/oapi-codegen/cmd/
[email protected]
```
## YAMLの作成
適当に作る。
```yaml:openapi.yaml
openapi: "3.0.0"
info:
version: 1.0.0
title: test
servers:
- url: http://localhost
paths:
/ping:
get:
responses:
'200':
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Ping'
components:
schemas:
Ping:
type: object
required:
- ping
properties:
ping:
type: string
```
## コード生成
```console
oapi-codegen -generate "types,server" openapi.yaml > openapi/openapi.go
```
```go:openapi/openapi.go
// Package Openapi provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen version v1.10.1 DO NOT EDIT.
package Openapi
import (
"github.com/labstack/echo/v4"
)
// Ping defines model for Ping.
type Ping struct {
Ping string `json:"ping"`
}
// ServerInterface represents all server handlers.
type ServerInterface interface {
// (GET /ping)
GetPing(ctx echo.Context) error
}
// ServerInterfaceWrapper converts echo contexts to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
}
// GetPing converts echo context to params.
func (w *ServerInterfaceWrapper) GetPing(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshalled arguments
err = w.Handler.GetPing(ctx)
return err
}
// This is a simple interface which specifies echo.Route addition functions which
// are present on both echo.Echo and echo.Group, since we want to allow using
// either of them for path registration
type EchoRouter interface {
CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
}
// RegisterHandlers adds each server route to the EchoRouter.
func RegisterHandlers(router EchoRouter, si ServerInterface) {
RegisterHandlersWithBaseURL(router, si, "")
}
// Registers handlers, and prepends BaseURL to the paths, so that the paths
// can be served under a prefix.
func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL string) {
wrapper := ServerInterfaceWrapper{
Handler: si,
}
router.GET(baseURL+"/ping", wrapper.GetPing)
}
```
## 呼び出し
main関数にて。
```go
import (
Openapi "sample/openapi"
"github.com/labstack/echo/v4"
"net/http"
)
// 自動生成されたServerInterfaceを満たす構造体の素をつくる
type Server struct{}
// ServerInterfaceのメソッドを実装していく
func (h Server) GetPing(ctx echo.Context) error {
// return値の第2引数に自動生成されたresponse型を使う
return ctx.JSON(http.StatusOK, &Openapi.Ping{
Ping: "pong",
})
}
func main() {
instance := echo.New()
server := api.Server{}
// 自動生成されたハンドラ登録関数にServerInterfaceを満たすserverを渡す
Openapi.RegisterHandlers(instance, server)
instance.Logger.Fatal(instance.Start(":1323"))
}
```