Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Goa Design

Avatar for ikawaha ikawaha
December 30, 2023

Goa Design

Avatar for ikawaha

ikawaha

December 30, 2023
Tweet

More Decks by ikawaha

Other Decks in Programming

Transcript

  1. API 色々書ける。けど、実は必須ではないので書かなくともよい。 package design import ( . "goa.design/goa/v3/dsl" ) //

    API describes the global properties of the API server. var _ = API("calc", func() { Title("Calculator Service") Description("HTTP service for multiplying numbers, a goa teaser") // Server describes a single process listening for client requests. The DSL // defines the set of services that the server hosts as well as hosts details. Server("calc", func() { Description("calc hosts the Calculator Service.") // List the services hosted by this server. Services("calc") // List the Hosts and their transport URLs. Host("development", func() { Description("Development hosts.") // Transport specific URLs, supported schemes are: // 'http', 'https', 'grpc' and 'grpcs' with the respective default // ports: 80, 443, 8080, 8443. URI("http://localhost:8000/calc") URI("grpc://localhost:8080") }) Host("production", func() { Description("Production hosts.") // URIs can be parameterized using {param} notation. URI("https://{version}.goa.design/calc") URI("grpcs://{version}.goa.design") // Variable describes a URI variable. Variable("version", String, "API version", func() { // URL parameters must have a default value and/or an // enum validation. Default("v1") }) }) }) })
  2. サービス // Service describes a service var _ = Service("calc",

    func() { Description("The calc service performs operations on numbers") // Method describes a service method (endpoint) Method("multiply", func() { // Payload describes the method payload. // Here the payload is an object that consists of two fields. Payload(func() { // Attribute describes an object field Attribute("a", Int, "Left operand", func() { Meta("rpc:tag", "1") }) Field(2, "b", Int, "Right operand") Required("a", "b") }) // Result describes the method result. // Here the result is a simple integer value. Result(Int) // HTTP describes the HTTP transport mapping. HTTP(func() { // Requests to the service consist of HTTP GET requests. // The payload fields are encoded as path parameters. GET("/multiply/{a}/{b}") // Responses use a "200 OK" HTTP status. // The result is encoded in the response body. Response(StatusOK) }) // GRPC describes the gRPC transport mapping. GRPC(func() { // Responses use a "OK" gRPC code. // The result is encoded in the response message. Response(CodeOK) }) }) // Serve the file gen/http/openapi3.json for requests sent to // /openapi.json. Files("/openapi.json", "openapi3.json") })
  3. コード生成 ※ 指定するのはファイルパスでなく、パッケージ名であることに注意 gen と example の2つのコマンドがある。 gen ユーザーがタッチしないコードの生成 (毎回破壊&生成)

    example ユーザーが実装すべきコードの生成(存在する場合は上書きしない) goa gen github.com/ikawaha/basic/design goa example github.com/ikawaha/basic/design 注: gonew でコピーしてきた場合は ./cmd 下と calc.go を削除してから実行する
  4. 生成されるファイルの構成 サービスごとにサービスのひな形が生成される . ├── calc.go ← サービス(ビジネスロジック)のひな形 ├── cmd │

    ├── calc/ ← サーバの参考実装 │ └── calc-cli/ ← 生成されたクライアント ├── design │ └── design.go ← 今回のデザイン └── gen ← デザインで生成されたもの(生成の度に破壊されるので手は入れない) ├── calc/ ├── grpc/ └── http/
  5. サービスのひな形(1) package calcapi import ( "context" "log" calc "github.com/ikawaha/basic/gen/calc" )

    // calc service example implementation. // The example methods log the requests and return zero values. type calcsrvc struct { logger *log.Logger } // NewCalc returns the calc service implementation. func NewCalc(logger *log.Logger) calc.Service { return &calcsrvc{logger} } // Multiply implements multiply. func (s *calcsrvc) Multiply(ctx context.Context, p *calc.MultiplyPayload) (res int, err error) { s.logger.Print("calc.multiply") return } ※ return p.A * p.B, nil としてやればかけ算の結果が返る
  6. バリデーション Attribute("name", String, func() { MinLength(8) MaxLength(31) }) バリデーション 対象

    説明 MinLength / MaxLength 文字列 文字列長の最小/最大を指定できる Minimum/Maximum 数値 最小値/最大値を指定できる Enum 文字列/数値 値を列挙して指定できる 定型フォーマット 文字列 別記したフォーマットを指定できる
  7. 定型バリデーション(1) フォーマット 概要 例 FormatDate RFC3339 の日付形 式 1977-06-28 FormatDateTime

    RFC3339 の日時形 式 2014-06-10T07:31:34Z FormatRFC1123 RFC1123 形式の日時 Sun, 30 Jan 1972 16:17:03 UTC FormatUUID RFC4122 の UUID形 式 76FB876C-96AC-91E7-BD21- B0C2988DDF65
  8. 定型バリデーション(3) フォーマット 概要 例 FormatIPv4, FormatIPv6, FormatIP RFC2373 の IPv4,

    IPv6 形式のアドレス もしくはそのいずれ か 8.8.8.8, 2001:cafe:109f:34d2:d755:da28:b5dc:8f23 FormatURI RFC3986 の URI形 式 github.com/goadesign/goa
  9. 定型バリデーション(4) フォーマット 概要 例 FormatMAC IEEE 802 形式の MAC-48, EUI-48

    or EUI-64 MACアドレス AD-85-EC-C3-95- 9B FormatCIDR RFC4632 もしくは RFC4291 の CIDR 記法の IP アドレス 192.168.100.14/24 FormatRegexp RE2 で定義される正規表現 (ab)