Goaを使ってAPIサーバ開発してみた / Develop API server by Goa

B932b972f9334d4d333224468f3f84d9?s=47 yyh-gl
May 15, 2020

Goaを使ってAPIサーバ開発してみた / Develop API server by Goa

DMM.go #2 登壇資料

サンプルコード
https://github.com/yyh-gl/goa-tutorial

B932b972f9334d4d333224468f3f84d9?s=128

yyh-gl

May 15, 2020
Tweet

Transcript

  1. (PBΛ࢖ͬͯ"1*αʔό։ൃͯ͠Έͨ

  2. ࣗݾ঺հ 5XJUUFSɿ!ZZI@HMʢIPO%ʣ (JU)VCɿZZIHM ϓϥοτϑΥʔϜࣄۀຊ෦
 όοΫΤϯυΤϯδχΞ (PΞʔΩςΫνϟ%%% ΄Μͩ

  3. 3 ʙ(PBʹࢸͬͨܦҢʙ

  4. >> ͍·ͷϓϩδΣΫτ w৽نͰωΠςΟϒΞϓϦͷόοΫΤϯυ։ൃ w "1*ͷ֎෦ެ։ɺࣾ಺ެ։͸ͳ͍ wʢҰԠʣΫϥΠΞϯτͱαʔόαΠυͰνʔϜ͕
 ෼͔Ε͍ͯΔ w ΫϥΠΞϯτͷํ͕গ͠ૣΊʹ։ൃ͕ελʔτ w

    3&45GVM"1*Λ࢖༻ 4
  5. >> ϑϨʔϜϫʔΫબఆ wબఆʹ͓͚ΔΩʔϫʔυʮ·ͣυΩϡϝϯτʯ w υΩϡϝϯτɿ"1*ͷ*0ఆٛ 5 ར༻ऀ͸ l·ͣυΩϡϝϯτzΛݟΔ ར༻ऀ͔Β͢Ε͹ lυΩϡϝϯτz͕શͯ

  6. >> υΩϡϝϯτେࣄ 6 ΋ͪΖΜ౰ͨΓલͰ͢ ͦ͏ɺ౰ͨΓલͳΜͰ͢ Ͱ΋ɺਓ͸ϛεΛ͢Δੜ͖෺Ͱ͢

  7. >> ͜Ε·ͰͷͭΒ͍ܦݧʜ 7 υΩϡϝϯτʹͳ͍ ϑΟʔϧυ υΩϡϝϯτͱҧ͏ܕ υΩϡϝϯτʹͳ͍ ύϥϝʔλཁٻ ༨෼ͳ֬ೝ͕࣌ؒൃੜ

  8. >> ͭ·Γʜ 8 υΩϡϝϯτͱίʔυ͸ဃ཭͢Δ

  9. >> ͭ·Γʜ 9 υΩϡϝϯτͱίʔυ͸ဃ཭͢Δ खͰ࡞Δ ͕ͬͪ͜ਖ਼͍͠

  10. >> Ͱ͋Ε͹ɺ wख࡞ۀ͸ഉআ w͋Δ৘ใݯ͔Βੜ੒ w ίʔυˠυΩϡϝϯτ w υΩϡϝϯτˠίʔυ w தؒݴޠˠίʔυͱυΩϡϝϯτ

    10
  11. >> υΩϡϝϯτΛେࣄʹͨ͠ཧ༝͸·ͩ͋Δ w࣮૷લʹυΩϡϝϯτΛϨϏϡʔ͢Δ͜ͱͰ
 ࣮૷ʹೖ͔ͬͯΒͷख໭ΓΛݮΒ͢ w"1*͕Ͱ͖͍ͯͳ͍͜ͱʹΑΔ։ൃ଴ͪΛͳ͘͢ w ΫϥΠΞϯτ͸υΩϡϝϯτΛ΋ͱʹ࣮૷ w υΩϡϝϯτ͕͋Ε͹ϞοΫ΋࡞ΕΔʂ 11

  12. >> ϑϨʔϜϫʔΫબఆཁ݅ w࣮૷ΑΓ΋લʹ·ͣυΩϡϝϯτΛ࡞ΕΔ w͋Δ৘ใݯ͔ΒυΩϡϝϯτʢͱίʔυʣΛ
 ੜ੒Ͱ͖Δ 12 ઈରʹυΩϡϝϯτʹίʔυʂ

  13. >> ީิ w0QFO"1*ʢPQFOBQJZNM㲈υΩϡϝϯτʣ w HPTXBHHFS w PQFOBQJZNMʢTXBHHFSZNMʣ͔Β(PͷίʔυΛੜ੒ w TXBH w

    (Pίʔυ಺ͷίϝϯτ͔ΒPQFOBQJZNMΛੜ੒ w (PB w %4-͔ΒPQFOBQJZNMͱ(PͷίʔυΛੜ੒ w1SPUPDPM#V⒎FST w H31$ H31$6* w *%-͔ΒυΩϡϝϯτͱ(PͷίʔυΛੜ੒ 13
  14. >> ީิ w0QFO"1*ʢPQFOBQJZNM㲈υΩϡϝϯτʣ w HPTXBHHFS w PQFOBQJZNMʢTXBHHFSZNMʣ͔Β(PͷίʔυΛੜ੒ w TXBH w

    (Pίʔυ಺ͷίϝϯτ͔ΒPQFOBQJZNMΛੜ੒ w (PB w %4-͔ΒPQFOBQJZNMͱ(PͷίʔυΛੜ੒ w1SPUPDPM#V⒎FST w H31$ H31$6* w *%-͔ΒυΩϡϝϯτͱ(PͷίʔυΛੜ੒ 14 ZBNMΛॻ͘ͷ͸ͭΒ͍
  15. >> ީิ w0QFO"1*ʢPQFOBQJZNM㲈υΩϡϝϯτʣ w HPTXBHHFS w PQFOBQJZNMʢTXBHHFSZNMʣ͔Β(PͷίʔυΛੜ੒ w TXBH w

    (Pίʔυ಺ͷίϝϯτ͔ΒPQFOBQJZNMΛੜ੒ w (PB w %4-͔ΒPQFOBQJZNMͱ(PͷίʔυΛੜ੒ w1SPUPDPM#V⒎FST w H31$ H31$6* w *%-͔ΒυΩϡϝϯτͱ(PͷίʔυΛੜ੒ 15 ɾ୯ମͰ͸υΩϡϝϯτ͔Βͷ
 ίʔυੜ੒͕Ͱ͖ͳ͍ ɾίϝϯτ͕๲େʹͳΔ
  16. >> ީิ w0QFO"1*ʢPQFOBQJZNM㲈υΩϡϝϯτʣ w HPTXBHHFS w PQFOBQJZNMʢTXBHHFSZNMʣ͔Β(PͷίʔυΛੜ੒ w TXBH w

    (Pίʔυ಺ͷίϝϯτ͔ΒPQFOBQJZNMΛੜ੒ w (PB w %4-͔ΒPQFOBQJZNMͱ(PͷίʔυΛੜ੒ w1SPUPDPM#V⒎FST w H31$ H31$6* w *%-͔ΒυΩϡϝϯτͱ(PͷίʔυΛੜ੒ 16 ɾQSPUPͷ಺༰ΛυΩϡϝϯτԽͰ͖Δʂ ɾQSPUP͔ΒίʔυΛੜ੒Ͱ͖Δʂ ɾ࠷ߴʂͰ΋31$ɻɻɻ
  17. >> ީิ w0QFO"1*ʢPQFOBQJZNM㲈υΩϡϝϯτʣ w HPTXBHHFS w PQFOBQJZNMʢTXBHHFSZNMʣ͔Β(PͷίʔυΛੜ੒ w TXBH w

    (Pίʔυ಺ͷίϝϯτ͔ΒPQFOBQJZNMΛੜ੒ w (PB w %4-͔ΒPQFOBQJZNMͱ(PͷίʔυΛੜ੒ w1SPUPDPM#V⒎FST w H31$ H31$6* w *%-͔ΒυΩϡϝϯτͱ(PͷίʔυΛੜ੒ 17 ɾ%4-͔ΒυΩϡϝϯτΛੜ੒Ͱ͖Δʂ ɾ%4-͔ΒίʔυΛੜ੒Ͱ͖Δʂ ɾ%4-ࣗମ(PͳͷͰֶशίετ͸গͳΊ ͍͍Ͷʂ 5-
  18. 18 ʙ(PBʹ͍ͭͯ΋͏গ͠ৄ͘͠ʙ

  19. >> (PB w%4-Λهड़͢Δ͜ͱͰ
 "1*ͷ*OQVUͱ0VUQVUΛఆٛ w%4-Λ΋ͱʹ w PQFOBQJZNMΛੜ੒Մೳ w (PͷίʔυΛੜ੒Մೳ w%4-ࣗମ͸(PͳͷͰɺ0QFO"1*ʹؔ͢Δ஌͕ࣝ


    ͋Ε͹ॻ͘͜ͱࣗମ͸؆୯ 19
  20. >> (PB w%4-Λهड़͢Δ͜ͱͰ
 "1*ͷ*/165ͱ065165Λఆٛ w%4-Λ΋ͱʹ w PQFOBQJZNMΛੜ੒Մೳ w (PͷίʔυΛੜ੒Մೳ w%4-ࣗମ͸(PͳͷͰɺ0QFO"1*ʹؔ͢Δ஌͕ࣝ


    ͋Ε͹ॻ͘͜ͱࣗମ͸؆୯ 20 https://goa.design/
  21. >> (PBͰੜ੒͞Εͨίʔυ wੜ੒͞ΕΔ(Pͷίʔυ͸͋͘·Ͱ
 ϓϨθϯςʔγϣϯ૚૬౰ͷίʔυ w ϏδωεϩδοΫʹ͸Ұ੾հೖ͠ͳ͍ 21 1SFTFOUBUJPO૚ 6TF$BTFʢ"QQMJDBUJPOʣ૚ %PNBJO૚

    *OGSBTUSVDUVSF૚
  22. >> (PBͷ஫ҙ఺ w࢖༻Ͱ͖Δͷ͸0QFO"1*WͷΈ w ݱࡏWରԠʹ޲͚ͯ࡞ۀதʁ 22

  23. 23 ʙ࣮ࡍͲ͏։ൃ͍ͯ͠Δ͔ʙ

  24. >> σβΠϯ࡞੒ 24

  25. >> υΩϡϝϯτͱίʔυͷੜ੒ wσβΠϯ͔Β
 PQFOBQJZNM͓Αͼ
 ίʔυΛੜ੒ 25

  26. >> wPQFOBQJZNMΛ4XBHHFS6*Λ࢖ͬͯ
 ϒϥ΢βͰӾཡͰ͖ΔΑ͏ʹ͢Δ w ΈΜͳͰϨϏϡʔ ϨϏϡʔ 26 EPDLFSDPNQPTFZNM

  27. >> wPQFOBQJZNMΛ4XBHHFS6*Λ࢖ͬͯ
 ϒϥ΢βͰӾཡͰ͖ΔΑ͏ʹ͢Δ w ΈΜͳͰϨϏϡʔ ϨϏϡʔ 27

  28. >> wPQFOBQJZNMΛ4XBHHFS6*Λ࢖ͬͯ
 ϒϥ΢βͰӾཡͰ͖ΔΑ͏ʹ͢Δ w ΈΜͳͰϨϏϡʔ ϨϏϡʔ 28 υΩϡϝϯτڞ༗ํ๏͸ޙ΄Ͳ

  29. >> ຊ࣮૷ wੜ੒͞ΕͨίʔυΛϏδωεϩδοΫͱඥ෇͚Δ 29 1SFTFOUBJPO૚ͷίʔυ

  30. 30 ʙଞʹ޻෉͍ͯ͠Δ͜ͱʙ

  31. >> ࣗಈੜ੒ͨ͠ϑΝΠϧͷ؅ཧ w(JUͰͷ؅ཧ͸͍ͯ͠ͳ͍ w ͩΕ͕ੜ੒ͯ͠΋ಉ͡ϑΝΠϧ͕Ͱ͖͕͋Δ w 13ϨϏϡʔ࣌ʹ༨෼ͳࠩ෼Λग़ͨ͘͠ͳ͍ w ੜ੒ʹ͔͔Δ࣌ؒ͸਺ඵఔ౓ w

    $*࣌΋͍͔ͪΒੜ੒ w ݱঢ়ɺ$*ʹΊͪΌͪ͘Ό͔͔࣌ؒΔɺͱ͔͸ͳ͍ 31
  32. >> υΩϡϝϯτͷڞ༗ํ๏ w 4ͰϗεςΟϯά w EFWFMPQϒϥϯν͸ৗʹӾཡՄೳ w ϒϥϯν໊ͷϓϦϑΟοΫεΛʮEFTJHOʯʹ͢Δͱ
 $*ʹͯPQFOBQJZNM͕ࣗಈͰ4ʹΞοϓϩʔυ͞ΕΔ 32

  33. >> υΩϡϝϯτͷڞ༗ํ๏ w 4ͰϗεςΟϯά w EFWFMPQϒϥϯν͸ৗʹӾཡՄೳ w ϒϥϯν໊ͷϓϦϑΟοΫεΛʮEFTJHOʯʹ͢Δͱ
 $*ʹͯPQFOBQJZNM͕ࣗಈͰ4ʹΞοϓϩʔυ͞ΕΔ 33

    ͜ͷ෦෼Λ࡞ۀ͍ͯͨ͠ϒϥϯν໊ʹ͢Ε͹ ͦͷϒϥϯνͷυΩϡϝϯτΛӾཡՄೳ
  34. >> ϨεϙϯεΛΧελϚΠζ wΦϦδφϧͷΤϥʔϨεϙϯε༻ϋϯυϥʔ͓Αͼ
 ϑΥʔϚολΛࠩ͠ࠐΈՄೳ 34 HFOIUUQVTFSTFSWFSTFSWFSHP \ lOBNFNJTTJOH@QBZMPBE  lJE4BQ*0#O(

     NFTTBHFNJTTJOHSFRVJSFEQBZMPBEz  lUFNQPSBSZGBMTF  lUJNFPVUGBMTF  lGBVMUzGBMTF ^
  35. >> ϨεϙϯεΛΧελϚΠζ wΦϦδφϧͷΤϥʔϨεϙϯε༻ϋϯυϥʔ͓Αͼ
 ϑΥʔϚολΛࠩ͠ࠐΈՄೳ 35 HFOIUUQVTFSTFSWFSTFSWFSHP

  36. >> ϨεϙϯεΛΧελϚΠζ wΦϦδφϧͷΤϥʔϨεϙϯε༻ϋϯυϥʔ͓Αͼ
 ϑΥʔϚολΛࠩ͠ࠐΈՄೳ 36 HFOIUUQVTFSTFSWFSTFSWFSHP \ DPEF  NFTTBHFαʔό಺ͰΤϥʔ͕ൃੜ͠·ͨ͠

    ^
  37. >> HPMJOUΛ९क w(PBެࣜͷαϯϓϧίʔυʹԊͬͯ։ൃ͍ͯ͠Δͱ
 υοτΠϯϙʔτ͍ͯ͠ΔͨΊHPMJOUʹౖΒΕΔ 37 αϯϓϧίʔυ

  38. >> HPMJOUΛ९क w(PBެࣜͷαϯϓϧίʔυʹԊͬͯ։ൃ͍ͯ͠Δͱ
 υοτΠϯϙʔτ͍ͯ͠ΔͨΊHPMJOUʹౖΒΕΔ 38 υοτΠϯϙʔτ͍ͯ͠ͳ͍όʔδϣϯ

  39. >> αϯϓϧίʔυ HPMJOUΛ९क w(PBެࣜͷαϯϓϧίʔυʹԊͬͯ։ൃ͍ͯ͠Δͱ
 υοτΠϯϙʔτ͍ͯ͠ΔͨΊHPMJOUʹౖΒΕΔ 39 HPMJOU EFTJHOVTFSHPTIPVMEOPUVTFEPUJNQPSUT

  40. >> HPMJOUΛ९क w(PBެࣜͷαϯϓϧίʔυʹԊͬͯ։ൃ͍ͯ͠Δͱ
 υοτΠϯϙʔτ͍ͯ͠ΔͨΊHPMJOUʹౖΒΕΔ 40 ๻ͨͪͷίʔυ

  41. >> HPMJOUΛ९क w(PBެࣜͷαϯϓϧίʔυʹԊͬͯ։ൃ͍ͯ͠Δͱ
 υοτΠϯϙʔτ͍ͯ͠ΔͨΊHPMJOUʹౖΒΕΔ 41 ๻ͨͪͷίʔυ ਖ਼௚ΊΜͲ͍͘͞

  42. >> ·ͱΊ w(PBͰυΩϡϝϯτத৺ͷ։ൃΛ࣮ݱ w ίʔυʹυΩϡϝϯτ wυΩϡϝϯτͷϨϏϡʔͰख໭ΓΛ࡟ݮ w ʴΫϥΠΞϯτͱαʔόαΠυؒͰͷೝࣝ߹Θͤ w"1*։ൃ஗ΕʹΑΔɺΫϥΠΞϯταΠυͰͷ
 ଴ͪ࣌ؒΛ࡟ݮ

    42
  43. >> ༨ஊ w͍ͭͰ΋H31$ʹ৐Γ׵͑Δ४උ͸Ͱ͖͍ͯΔ 43 (PB͸QSFTFOUBUJPO૚಺ʹ͔͠ݱΕͳ͍ QSFTFOUBUJPO૚͚ͩ ม͑Ε͹0,

  44. >> (PB wੜ੒͞ΕΔ(Pͷίʔυ͸͋͘·Ͱ
 ϓϨθϯςʔγϣϯ૚૬౰ͷίʔυ w ϏδωεϩδοΫʹ͸Ұ੾հೖ͠ͳ͍ 44 1SFTFOUBUJPO૚ %PNBJO૚ *OGSBTUSVDUVSF૚

    6TF$BTFʢ"QQMJDBUJPOʣ૚
  45. >> 1SFTFOUBUJPO૚ (PB wੜ੒͞ΕΔ(Pͷίʔυ͸͋͘·Ͱ
 ϓϨθϯςʔγϣϯ૚૬౰ͷίʔυ w ϏδωεϩδοΫʹ͸Ұ੾հೖ͠ͳ͍ 45 (PB %PNBJO૚

    *OGSBTUSVDUVSF૚ 6TF$BTFʢ"QQMJDBUJPOʣ૚
  46. >> 1SFTFOUBUJPO૚ (PB wੜ੒͞ΕΔ(Pͷίʔυ͸͋͘·Ͱ
 ϓϨθϯςʔγϣϯ૚૬౰ͷίʔυ w ϏδωεϩδοΫʹ͸Ұ੾հೖ͠ͳ͍ 46 H31$ %PNBJO૚

    *OGSBTUSVDUVSF૚ 6TF$BTFʢ"QQMJDBUJPOʣ૚
  47. >> wੜ੒͞ΕΔ(Pͷίʔυ͸͋͘·Ͱ
 ϓϨθϯςʔγϣϯ૚૬౰ͷίʔυ w ϏδωεϩδοΫʹ͸Ұ੾հೖ͠ͳ͍ 1SFTFOUBUJPO૚ (PB 47 H31$ %PNBJO૚

    *OGSBTUSVDUVSF૚ 6TF$BTFʢ"QQMJDBUJPOʣ૚ (PB