Skip to content

Instantly share code, notes, and snippets.

@ikawaha
Last active February 23, 2016 09:23
Show Gist options
  • Save ikawaha/32fa2f5ab2e2bacf998f to your computer and use it in GitHub Desktop.
Save ikawaha/32fa2f5ab2e2bacf998f to your computer and use it in GitHub Desktop.
goa sample
package main
import (
"fmt"
"cellar/app"
"github.com/goadesign/goa"
)
// BottleController implements thebottle resource.
type BottleController struct {
goa.Controller
}
// NewBottleController creates a bottle controller.
func NewBottleController(service goa.Service) app.BottleController {
return &BottleController{Controller: service.NewController("bottle")}
}
func p(s string) *string {
return &s
}
// Drink runs the drink action.
func (c *BottleController) Drink(ctx *app.DrinkBottleContext) error {
if ctx.BottleID == 0 {
// Emulate a missing record with ID 0
return ctx.NotFound()
}
snaks := []string{}
for _, v := range ctx.Payload {
if v == nil {
continue
}
snaks = append(snaks, v.Name)
}
// Build the resource using the generated data structure
bottle := app.BottleMediaExtended{
ID: &ctx.BottleID,
Name: p(fmt.Sprintf("Bottle #%d", ctx.BottleID)),
Href: p(app.BottleHref(ctx.BottleID)),
Snaks: snaks,
}
// Let the generated code produce the HTTP response using the
// media type described in the design (BottleMedia).
return ctx.OKExtended(&bottle)
}
// Show runs the show action.
func (c *BottleController) Show(ctx *app.ShowBottleContext) error {
if ctx.BottleID == 0 {
// Emulate a missing record with ID 0
return ctx.NotFound()
}
// Build the resource using the generated data structure
bottle := app.BottleMedia{
ID: &ctx.BottleID,
Name: p(fmt.Sprintf("Bottle #%d", ctx.BottleID)),
Href: p(app.BottleHref(ctx.BottleID)),
}
// Let the generated code produce the HTTP response using the
// media type described in the design (BottleMedia).
return ctx.OK(&bottle)
}
package design
import (
. "github.com/goadesign/goa/design"
. "github.com/goadesign/goa/design/apidsl"
)
var _ = API("cellar", func() {
Title("The virtual wine cellar")
Description("A basic example of an API implemented with goa")
Scheme("http")
Host("localhost:8080")
})
var Snack = Type("Snack", func() {
Attribute("name", String, "Snack name")
Required("name")
})
var Snacks = ArrayOf(Snack)
var _ = Resource("bottle", func() {
BasePath("/bottles")
DefaultMedia(BottleMedia)
Action("show", func() {
Description("Retrieve bottle with given id")
Routing(GET("/:bottleID"))
Params(func() {
Param("bottleID", Integer, "Bottle ID")
})
Response(OK)
Response(NotFound)
})
Action("drink", func() {
Description("Drink")
Routing(POST("/:bottleID"))
Params(func() {
Param("bottleID", Integer, "Bottle ID")
})
Payload(Snacks)
Response(OK)
Response(NotFound)
})
})
var BottleMedia = MediaType("application/json", func() {
TypeName("BottleMedia")
Description("A bottle of wine")
Attributes(func() {
Attribute("id", Integer, "Unique bottle ID")
Attribute("href", String, "API href for making requests on the bottle")
Attribute("name", String, "Name of wine")
Attribute("snaks", ArrayOf(String), "List of snaks")
})
View("default", func() {
Attribute("id")
Attribute("href")
Attribute("name")
})
View("extended", func() {
Attribute("id")
Attribute("href")
Attribute("name")
Attribute("snaks")
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment