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

go + swaggerでAPIサーバーを作ってみる

go + swaggerでAPIサーバーを作ってみる

2019年1月22日に開催された【とらのあな主催】オタクが最新技術を追うライトニングトークイベントのライトニングトーク資料です。

6ab47a68ee78e84c34731ce12333deff?s=128

虎の穴ラボ株式会社

January 22, 2019
Tweet

More Decks by 虎の穴ラボ株式会社

Transcript

  1. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. go +

    swaggerAPI ΦλΫ͕࠷৽ٕज़Λ௥͏ϥΠτχϯάτʔΫΠϕϯτճ໨ גࣜձࣾދͷ݀ ދͷ݀ϥϘ ্ాণ׮
  2. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. 1. goAPI

    (Swagger ) 2. Swagger API 3. go-swagger  4.   2
  3.          •

    2017 5  • Fantia   Ruby, JavaScript Java, Kotlin, Go 3
  4. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. Go 

      Web    4
  5. Copyright (C) 2019 Toranoana Inc. All Rights Reserved.  

    5
  6. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. Revel ?

    Echo ? goa ?  6
  7. Copyright (C) 2019 Toranoana Inc. All Rights Reserved.  

           7
  8. Copyright (C) 2019 Toranoana Inc. All Rights Reserved.  

     #  !"   …$  8
  9. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. Swagger go

      9
  10. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. Swagger •

    RESTful API   •  Swagger Editor, Swagger Codegen 10
  11. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. Swagger 11

    Swagger Spec RESTfull API,", -80; !'&+ -8YAMLJSON32? @ Swagger Editer Swagger Spec?@  !'&+ %*#',9< Swagger UI Swagger Spec7$) HTML!'&+ /5,) Swagger Codegen Swagger Spec7$) ,",A(+ ,!=.:4 ,) Swagger Core 6 1>,!Swagger Spec $) /5,)
  12. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. Swagger 12

    Swagger SpecAPI$ () ... • '%" • +'%" etc #  *Swagger Spec$!&  %"
  13. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. go +

    Swagger go-swagger https://github.com/go-swagger/go-swagger 13 Swagger Spec RESTfull API "   YAML JSON$ %  Swagger Editer Swagger Spec $%    !# 
  14. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. go-swagger 

    14 API         go-swagger 
  15. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. % 1.

     API  "$ 2.  ! go-swagger      3. curl  # 15
  16. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. Swagger Editor

      docker  docker pull swaggerapi/swagger-editor docker run -d -p 80:8080 swaggerapi/swagger-editor http://localhost:80/   16
  17. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. Swagger Editor

     (2) YAML  17  
  18. Copyright (C) 2019 Toranoana Inc. All Rights Reserved.  18

  19. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. API 19

  20. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. go-swagger 

    go!"  $ swagger generate server -f ./swagger.yml -A swagger-go-sample 20 -f :    -A :   $ go get -u github.com/go-swagger/go-swagger/cmd/swagger 1. go-swagger   2. go-swagger  
  21. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. go-swagger 

    go  21
  22. Copyright (C) 2019 Toranoana Inc. All Rights Reserved.  

    Handler 22 API  
  23. Copyright (C) 2019 Toranoana Inc. All Rights Reserved.  curl

    23  $ cd cmd/swagger-go-sample-server $ go build -o server $./server Serving swagger go sample at http://127.0.0.1:55187  
  24. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. { “id”:

    0, “name”: ”ueda", "age": 20 } curl  24        $ curl -X POST "http://localhost/" -H "accept: application/json" -H "Content-Type: application/json" -d "{ ¥"name¥": ¥”ueda¥", ¥"age¥": 20}"
  25. Copyright (C) 2019 Toranoana Inc. All Rights Reserved. [{“id”: 0,

    “name”: ”ueda“,”age“:20}]  curl  25       $ curl -X GET "http://localhost/?limit=20" -H "accept: application/json"
  26. Copyright (C) 2019 Toranoana Inc. All Rights Reserved.  •

    GoWeb " go-swagger ) 9/;,82 <  • !#API#%?-;,82 $ A&+6 • 3@( !(5Swagger0:  • '14= $.*7>   26