Top Banner
CyberAgent, Inc. All Rights Reserved. goa :: Design-first API Generation 2015 December 4th Date : 2017 March 10 Presenter : Y.Sugiyama
24

goa Design first API Generation

Apr 11, 2017

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: goa Design first API Generation

CyberAgent, Inc. All Rights Reserved.

goa :: Design-first API Generation

2015 December 4th

Date : 2017 March 10 Presenter : Y.Sugiyama

Page 2: goa Design first API Generation

Who are you?01

Page 3: goa Design first API Generation

Introduction Who are you?01

CyberAgent(2011~)• Ameba Pigg

• Java, node.js, MongoDB• Takusuta(Live Streaming)

• Golang(gin), node.js, python, MongoDB,and various on live streaming.• Golang, node.js, a little Scala.

Yoshinori Sugiyama,age:36,github:syama666

Page 4: goa Design first API Generation

What is goa?02

Page 5: goa Design first API Generation

goa has three parts What is goa?02

• a Go DSL for describing the microservice API• a tool for generating code and documentation from design DSL• a library to support the implementation of microservices

Page 6: goa Design first API Generation

design03

Page 7: goa Design first API Generation

DSL examples for API design design03

// GET /v1/kings?page=1&perPage=10&sort=-kingsIdvar _ = Resource("v1_kings", func() {

BasePath("/v1/kings")Action("list", func() {

Routing(GET(""))Description(”get kings list")Param("page", Integer, "e.g.) page=1", func() {

Default(1)})Param("perPage", Integer, "e.g.) perPage=10", func() {

Default(10)})Param("sort", String, "e.g.) sort=kingId or sort=-

kingId", func() {Pattern("^-?[0-9a-zA-Z]+$")Default("-kingId")

})Response(OK, func() {

Media(ResponseKings)})

})})

Page 8: goa Design first API Generation

DSL examples for API design design03

// PUT/v1/kings/1192var _ = Resource("v1_kings", func() {

BasePath("/v1/kings")Action("update", func () {

Routing(PUT("/:kingId"))Description(”update

a king")Params(func() {

Param("kingId", Integer)})Payload(UpdateKing)Response(OK, func()

{

Media(ResponseOneKing)})

})})

Page 9: goa Design first API Generation

DSL examples for API design design03

// base king type dslvar baseKing = Type("baseKing", func() { Attribute("kingId", Integer, func() { Example(1192) }) Attribute("name", String, func() { Example("Louis XIV") }) Attribute("age", Integer, func() { Example(76) })})

Page 10: goa Design first API Generation

DSL examples for API design design03

// Japanese Lord type dslvar japaneseLord = Type("baseKing", func() { Reference(baseKing) Attribute("kingId") Attribute("name") Attribute("age") Attribute("birthPlace", String, func(){ Example(“ ”江戸 ) }) Required( "kingId", "name", "age", "birthPlace", )})

Page 11: goa Design first API Generation

DSL to Swagger design03

Goa DSL to Swagger.json By goagen

Page 12: goa Design first API Generation

API design lifecycle design03

Page 13: goa Design first API Generation

Generating code and docs04

Page 14: goa Design first API Generation

goagen’s sub commands Generating04

• main• main.go and controller scaffolding

• controller• only controller scaffolding

• app• validation, context, etc

• client• golang client

• swagger• swagger json

• js• javascript client

• schema• JSON schema

Page 15: goa Design first API Generation

directory/package structure Generating04

• main.go• controller/• gen/

• app/• contexts.go• controllers.go• hrefs.go• user_types.go• media_types.go

• swagger/• swagger.json• swagger.yml

• design/

Page 16: goa Design first API Generation

directory/package structure Generating04

• main.go• controller/• gen/

• app/• contexts.go• controllers.go• hrefs.go• user_types.go• media_types.go

• swagger/• swagger.json• swagger.yml

• design/

main

app

swagger

Page 17: goa Design first API Generation

directory/package structure Generating04

• main.go• controller/• gen/

• app/• contexts.go• controllers.go• hrefs.go• user_types.go• media_types.go

• swagger/• swagger.json• swagger.yml

• design/

auto-generated, do not modify

Only scaffold, goa do not update

Page 18: goa Design first API Generation

05 Overall view

Page 19: goa Design first API Generation

lib,generated, and yours Overall view05

Page 20: goa Design first API Generation

Components structure Overall view05

Page 21: goa Design first API Generation

API sequence Overall view05

Page 22: goa Design first API Generation

06 digest

Page 23: goa Design first API Generation

cool digest06

• Generating swagger docs.• Generating validation-code, and scaffolding some.

• Output a better code than go-swagger -> https://github.com/go-swagger/go-swagger

Page 24: goa Design first API Generation

not cool digest06

• You need implements logic of convert from your model sturct to goa-generated struct.

• If you want to customize the error response, you have to hack goa - middleware.

https://github.com/goadesign/goa/issues/1076