design.goファイル分割

公式ドキュメント通りのファイル構成というかdesign.goの書き方にするとdesign.goが肥大化するため、ファイルを分割しました。

大きく3つに分割してます。

  • design
package design // The convention consists of naming the design
// package "design"
import (
    . "github.com/goadesign/goa/design/apidsl"
)

var _ = API("area", func() { // API defines the microservice endpoint and
    Title("area API")                   // other global properties. There should be one
    Description("A simple goa service") // and exactly one API definition appearing in
    Scheme("http")                      // the design.
    Host("localhost:8080")
})
  • resource
package design // The convention consists of naming the design

import (
    . "github.com/goadesign/goa/design" // Use . imports to enable the DSL
    . "github.com/goadesign/goa/design/apidsl"
)

var _ = Resource("point", func() { // Resources group related API endpoints
    BasePath("/points")      // together. They map to REST resources for REST
    DefaultMedia(PointMedia) // services.

    Action("show", func() { // Actions define a single API endpoint together
        Description("Get point by id") // with its path, parameters (both path
        Routing(GET("/:pointId"))      // parameters and querystring values) and payload
        Params(func() {                // (shape of the request body).
            Param("pointId", Integer, "Bottle ID")
        })
        Response(OK, PointMedia) // Responses define the shape and status code
        Response(NotFound)       // of HTTP responses.
    })
    Action("list", func() {
        Description("point list")
        Routing(GET("/"))
        Response(OK, CollectionOf(PointMedia)) // Responses define the shape and status code
        Response(NotFound)                     // of HTTP responses.
    })
})
  • media
package design // The convention consists of naming the design
// package "design"
import (
    . "github.com/goadesign/goa/design" // Use . imports to enable the DSL
    . "github.com/goadesign/goa/design/apidsl"
)

var PointMedia = MediaType("application/vnd.point.area+json", func() {
    Description("A station of mine")
    Attributes(func() { // Attributes define the media type shape.
        Attribute("id", Integer, "Unique station ID")
        Attribute("name", String, "Name of station")
        Required("id", "name")
    })
    View("default", func() { // View defines a rendering of the media type.
        Attribute("id") // Media types may have multiple views and must
        Attribute("name")
    })
})

これでファイルの役割がすっきりして見通しやすくなりました。 goagenはもちろん

goagen bootstrap -d github.com/hryktrd/goaTest/design

で通ります。

完全なサンプルはこちらのリポジトリに置いてあります。

github.com