GOA UNIVERSITY Sub P.O. Goa University, Taleigao Plateau, Goa ...
goa Design first API Generation
-
Upload
yoshinori-sugiyama -
Category
Software
-
view
881 -
download
1
Transcript of goa Design first API Generation
![Page 1: goa Design first API Generation](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/1.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/2.jpg)
Who are you?01
![Page 3: goa Design first API Generation](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/4.jpg)
What is goa?02
![Page 5: goa Design first API Generation](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/6.jpg)
design03
![Page 7: goa Design first API Generation](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/11.jpg)
DSL to Swagger design03
Goa DSL to Swagger.json By goagen
![Page 12: goa Design first API Generation](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/12.jpg)
API design lifecycle design03
![Page 13: goa Design first API Generation](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/13.jpg)
Generating code and docs04
![Page 14: goa Design first API Generation](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/14.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/15.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/16.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/18.jpg)
05 Overall view
![Page 19: goa Design first API Generation](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/19.jpg)
lib,generated, and yours Overall view05
![Page 20: goa Design first API Generation](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/20.jpg)
Components structure Overall view05
![Page 21: goa Design first API Generation](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/21.jpg)
API sequence Overall view05
![Page 22: goa Design first API Generation](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/22.jpg)
06 digest
![Page 23: goa Design first API Generation](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/23.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062903/58d12b121a28abe3298b5003/html5/thumbnails/24.jpg)
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