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

goaを使ったAPI開発/api-development-with-goa

Cf7c364c6463eb8a1f1bb5c4a2e583a6?s=47 decafe09
March 25, 2017

 goaを使ったAPI開発/api-development-with-goa

Cf7c364c6463eb8a1f1bb5c4a2e583a6?s=128

decafe09

March 25, 2017
Tweet

Transcript

  1. goaΛ࢖ͬͨAPI։ൃ Go Conference 2017 Spring / @decafe09

  2. ࣗݾ঺հ খ໺ࣉ ढ़໵ (@decafe09) • γφδʔϚʔέςΟϯάגࣜձࣾ • Goྺ 2೥

  3. ࠓ೔࿩͢͜ͱ goaΛ࢖ͬͨAPI։ൃͰࠔͬͨ͜ͱͱղܾࡦ

  4. goa https://github.com/goadesign/goa • ϚΠΫϩαʔϏε޲͚ϑϨʔϜϫʔΫ • DSLͰॻ͍ͨAPIσβΠϯ͔Βίʔυ΍ SwaggerΛੜ੒Ͱ͖Δ

  5. ։ൃͷྲྀΕ

  6. design DSL DSLͰAPIσβΠϯΛॻ͘

  7. var _ = Resource("account", func() { DefaultMedia(Account) BasePath("/accounts") Action("show", func()

    { Routing( GET("/:accountID"), ) Description("Retrieve account with given id.") Params(func() { Param("accountID", Integer, "Account ID", func() { Minimum(1) }) }) Response(OK) Response(NotFound) Response(BadRequest, ErrorMedia) }) } DSLͰͷσβΠϯ
  8. goagen ίϚϯυͰίʔυੜ੒ Swagger΋ੜ੒Ͱ͖Δ design API DSL goagen

  9. ίʔυੜ੒ goagen ίϚϯυͰίʔυੜ੒ ެࣜαϯϓϧ https://github.co/goadesign/goa-cellar goagen main -d github.com/goadesign/goa-cellar/design ੜ੒ର৅

    σβΠϯ
  10. func main() { // Create service service := goa.New("cellar") //

    Mount middleware service.Use(middleware.RequestID()) service.Use(middleware.LogRequest(true)) service.Use(middleware.ErrorHandler(service, true)) service.Use(middleware.Recover()) // Mount "account" controller c := NewAccountController(service) app.MountAccountController(service, c) // Start service if err := service.ListenAndServe(":8081"); err != nil { service.LogError("startup", "err", err) } } ੜ੒͞Εͨίʔυ
  11. ੜ੒͞ΕͨίʔυΛϕʔεʹ࣮૷ design API ࣮૷

  12. func main() { // Create service service := goa.New("cellar") //

    Mount middleware service.Use(myMiddleware.RestrictIP()) service.Use(middleware.RequestID()) service.Use(middleware.LogRequest(true)) service.Use(middleware.ErrorHandler(service, true)) service.Use(middleware.Recover()) // Mount "account" controller c := NewAccountController(service) app.MountAccountController(service, c) // Start service if err := service.ListenAndServe(":8081"); err != nil { service.LogError("startup", "err", err) } } service.Use(myMiddleware.RestrictIP()) ϛυϧ΢ΣΞΛ௥Ճ
  13. ಥવͷAPI࢓༷มߋʂ ʢ͋Δ͋Δʣ

  14. DSLΛमਖ਼ͯ͠goagenͰίʔυ࠶ੜ੒ design API goagen ࠶ੜ੒ मਖ਼

  15. ੜ·ΕΔࠩ෼ design API ࠶ੜ੒ API ࣮૷

  16. खಈͰͷࠩ෼Ϛʔδ design API ࠶ੜ੒ API ࣮૷

  17. ΍ͬͯΒΕΔ͔ͬʂ

  18. ։ൃΛ࢝ΊΔͱࠔΔͱ͜Ζ σβΠϯͷมߋͰίʔυΛ࠶ੜ੒͢Δࡍɺɹɹ ࠩ෼Λݟͳ͕ΒखಈϚʔδ͠ͳ͍ͱ͍͚ͳ͍ɻ

  19. ։ൃΛ࢝ΊΔͱࠔΔͱ͜Ζ σβΠϯͷมߋͰίʔυΛ࠶ੜ੒͢Δࡍɺɹɹ ࠩ෼Λݟͳ͕ΒखಈϚʔδ͠ͳ͍ͱ͍͚ͳ͍ɻ ɹɹɹɹɹσβΠϯʹґଘ͠ͳ͍෦෼͕ ɹɹɹɹɹੜ੒͞ΕΔίʔυʹؚ·Ε͍ͯΔ

  20. func main() { // Create service service := goa.New("cellar") //

    Mount middleware service.Use(middleware.RequestID()) service.Use(middleware.LogRequest(true)) service.Use(middleware.ErrorHandler(service, true)) service.Use(middleware.Recover()) // Mount "account" controller c := NewAccountController(service) app.MountAccountController(service, c) // Start service if err := service.ListenAndServe(":8081"); err != nil { service.LogError("startup", "err", err) } } σβΠϯʹґଘ͠ͳ͍
  21. σβΠϯ͔Βࣗ෼ͷ࢖͍΍͍͢Α͏ʹίʔυੜ੒͍ͨ͠

  22. ͦΕpluginͰͰ͖ΔΑ

  23. goa Plugin Generators https://goa.design/extend/generators/ goagenͷpluginͰDSL͔Βࣗ༝ʹίʔυੜ੒ goagen gen -d github.com/goadesign/goa-cellar/design \

    —pkg-path=/go/path/to/genresnames goagen gen plugin
  24. goagen main templateमਖ਼͢Δ͚ͩͰ͍͚Δʂ https://github.com/goadesign/goa/blob/v1.1.0/goagen/gen_main/ generator.go

  25. func main() { // Create service service := goa.New("cellar") //

    Mount middleware service.Use(middleware.RequestID()) service.Use(middleware.LogRequest(true)) service.Use(middleware.ErrorHandler(service, true)) service.Use(middleware.Recover()) // Mount "account" controller c := NewAccountController(service) app.MountAccountController(service, c) // Start service if err := service.ListenAndServe(":8081"); err != nil { service.LogError("startup", "err", err) } } ͜ΕΛ
  26. func main() { // Create service service := goa.New("cellar") //

    Mount middleware mountMiddleware(service) // Mount "account" controller c := NewAccountController(service) app.MountAccountController(service, c) // Start service if err := service.ListenAndServe(":8081"); err != nil { service.LogError("startup", "err", err) } } ͜͏ʂ
  27. ϑΝΠϧΛ෼͚Δ // middleware.go func mountMiddleware(service *goa.Service) { // Put Your

    Code service.Use(middleware.RequestID()) service.Use(middleware.LogRequest(true)) service.Use(middleware.ErrorHandler(service, true)) service.Use(middleware.Recover()) } main.go͔Β੾Γग़ͯ͠ɺผϑΝΠϧʹ͢Δɻ ϛυϧ΢ΣΞ͕૿͑ͯ΋ίʔυੜ੒ͷӨڹΛड͚ͳ͍
  28. ࢓༷มߋʹ଱͑ΒΕΔʂ

  29. ΄͔ʹ΋΍Γํ͸͋Δͱࢥ͍·͕͢

  30. goagenͰࠔͬͨΒ plugin࡞ͬͯΈ·͠ΐ͏

  31. ͓·͚

  32. swrt https://github.com/decafe09/swrt swagger.jsonͷϧʔςΟϯάΛCLIͰ֬ೝɻ $ swrt -f swagger/swagger.json http://localhost:8081 GET /cellar/_ah/health

    GET /cellar/accounts POST /cellar/accounts GET /cellar/accounts/{accountID} PUT /cellar/accounts/{accountID} DELETE /cellar/accounts/{accountID} GET /cellar/accounts/{accountID}/bottles
  33. ΈΜGoಡΜͰ࡞Γ·ͨ͠

  34. goaͷࢀߟαΠτ • ެࣜαΠτ https://goa.design/ja/ ೔ຊޠ൛͋Γ͕ͱ͏͍͟͝·͢ m(_ _)m • goaΛ࢝ΊΑ͏ @ikawaha

    ͞Μ http://ikawaha.hateblo.jp/entry/ 2016/09/11/095129 ͱͯ΋Θ͔Γ΍͍͢Ͱ͢ m(_ _)m
  35. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠