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

Make RESTful API with Go

Make RESTful API with Go

Introduce a way to create RESTful API with Go, but without framework. (프레임워크 없이 Go로 RESTful API 만드는 방법 소개)

2c8948aaaa1a7047e0dc3cd869877879?s=128

mingrammer

April 15, 2017
Tweet

Transcript

  1. Make RESTful API with Go MinJae Kwon (@mingrammer) 2017.04.15 GDG

    Incheon Web Seminar (without framework)
  2. Name ӂ޹੤ (MinJae Kwon) Nickname @mingrammer Email k239507@gmail.com Who ই઱؀೟Ү

    ࣗ೐౟ਝয೟җ 3೟֙ / Backend Developer Blog https://mingrammer.com Facebook https://facebook.com/mingrammer Github https://github.com/mingrammer Eng Blog https://medium.com/@mingrammer
  3. 2. Why Go? 4. Implementation of REST API 3. API

    Design & Architecture Contents 5. Review & Next 1. RESTful? REST API?
  4. RESTful? REST API?

  5. RESTful? REST API? REpresentational State Transfer + Application Programming Interface

    REST API?
  6. RESTful? REST API? REST API? HTTP ࢸ҅੄ ੢੼ਸ ୭؀ೠ ࢓ܽ

    API ইఃఫ୊ ೯ਤ Verb ಴അ Representation ੗ਗ Resource (REST API੄ 3ਃࣗ)
  7. RESTful? REST API? Client API Server POST Create GET Retrieve

    PUT/PATCH Update DELETE Delete DB Server REST API? Simple REST Flow (Client - Server)
  8. RESTful? REST API? Why REST API? Stateless Cacheable Uniform Interface

    Fully Client - Server Hierarchical Structure Self Descriptiveness
  9. RESTful? REST API? Yes! Let’s make REST API!

  10. Why Go?

  11. Why Go? 1. ೧ࠁҊ रযࢲ 2. ೐ۨ੐ਕ௼о ೙ࣻח ইש 3.

    ղ੢ ಁః૑о ъ۱ 4. MSAী ੸೤ 5. ࠽٘ ߂ ߓನо ए਑ Because Go is AWESOME
  12. Why Go? Goܳ ҕࠗೞח ੑ੢ীࢲ Go۽ API ೠ ߣ ٜ݅যࠁҊ

    र঻਺ ղ੢ ಁః૑о ъ۱೧ ߹ب੄ ೐ۨ੐ਕ௼ হ੉ب ѐߊ੉ оמ MSA (Micro Service Architecture)ী ੸೤ ࠽٘ ߂ ߓನо औҊ рಞ + + + + పझ౟ ಞೣ ࢿמ જ਺ ޙࢲച ಞೣ …
  13. Why Go? Yes, Go is AWESOME

  14. API Design & Architecture

  15. API Design & Architecture Let’s make REST API for simple

    todo application
  16. API Design & Architecture Client Auth DB Todo APIs Simple

    Architecture
  17. API Design & Architecture Not Yet … Client Auth DB

    Todo APIs Simple Architecture
  18. API Design & Architecture Client DB Todo APIs Public now

    Simple Architecture
  19. API Design & Architecture Models User Project Project Project Task

    Task Task 1 : n 1 : n
  20. API Design & Architecture Models User Project Project Project Task

    Task Task 1 : n 1 : n Not Yet …
  21. API Design & Architecture Models Project Project Project Task Task

    Task 1 : n
  22. API Design & Architecture Models Project Title :: string Archived

    :: bool Task Title :: string Priority :: enum Deadline :: datetime Done :: bool ProjectID :: int 1 : n
  23. API Design & Architecture /projects • GET : Get all

    projects • POST : Create a new project /projects/:title • GET : Get a project • PUT : Update a project • DELETE : Delete a project /projects/:title/archive • PUT : Archive a project • DELETE : Restore a project Projects API Design
  24. API Design & Architecture /projects/:title/tasks • GET : Get all

    tasks of a project • POST : Create a new task in a project /projects/:title/tasks/:id • GET : Get a project of a project • PUT : Update a project of a project • DELETE : Delete a project of a project /projects/:title/tasks/:id/complete • PUT : Archive a project of a project • DELETE : Restore a project of a project Tasks API Design
  25. API Design & Architecture /projects • GET : Get all

    projects • POST : Create a new project /projects/:title • GET : Get a project • PUT : Update a project • DELETE : Delete a project /projects/:title/archive • PUT : Archive a project • DELETE : Restore a project /projects/:title/tasks • GET : Get all tasks of a project • POST : Create a new task in a project /projects/:title/tasks/:id • GET : Get a project of a project • PUT : Update a project of a project • DELETE : Delete a project of a project /projects/:title/tasks/:id/complete • PUT : Archive a project of a project • DELETE : Restore a project of a project API Design
  26. API Design & Architecture ├── app │ ├── app.go │

    ├── handler │ │ ├── common.go │ │ ├── projects.go │ │ └── tasks.go │ └── model │ └── model.go ├── config │ └── config.go └── main.go Application Structure Our API core handlers Common response functions APIs for Project model APIs for Task model Models for our application Configuration Init, route and run the app
  27. Implementation of REST API

  28. Implementation of REST API https://github.com/mingrammer/go-todo-rest-api-example All source code is on

    following repository
  29. Implementation of REST API ├── app │ ├── app.go │

    ├── handler │ │ ├── common.go │ │ ├── projects.go │ │ └── tasks.go │ └── model │ └── model.go ├── config │ └── config.go └── main.go app/app.go Initialize the app Set all routers to app Register all handlers to app Run the app
  30. Implementation of REST API app/app.go app/app.go੄ ੹୓ ҳઑ

  31. Implementation of REST API app/app.go 1. ಁః૑ ࢶ঱ ߂ ೙ਃೠ

    ಁః૑ ੐ನ౟ github.com/gorilla/mux : ղ੢ ಁః૑ࠁ׮ ખ ؊ ಞܻೞѱ ࢎਊೡ ࣻ ੓ח Mux ۄ੉࠳۞ܻ github.com/jinzhu/gorm : Goਊ ORM ۄ੉࠳۞ܻ
  32. Implementation of REST API app/app.go ۄ਋ఠ۽ github.com/gorilla/mux੄ Router ࢎਊ DB۽

    github.com/jinzhu/gorm੄ DB ࢎਊ 2. ۄ਋ఠ৬ DBܳ ыחApp ҳઑ୓ ࢶ঱
  33. Implementation of REST API app/app.go 3. App ୡӝചܳ ਤೠ Initialize

    ೣࣻ ੿੄
  34. Implementation of REST API app/app.go 4. Appী ݽٚ API ۄ਋౴

  35. Implementation of REST API app/app.go 5. HTTP Methodী ٮܲ ۄ਋ఠ

    ېಌ ೣࣻ ੿੄
  36. Implementation of REST API app/app.go 6. Project API ೩ٜ۞ ഐ୹

    ߂ ١۾
  37. Implementation of REST API app/app.go 7. Task API ೩ٜ۞ ഐ୹

    ߂ ١۾
  38. Implementation of REST API app/app.go 8. HTTP࢚ীࢲ API Serverܳ ڸ਋ח

    Run ೣࣻ ੿੄
  39. ├── app │ ├── app.go │ ├── handler │ │

    ├── common.go │ │ ├── projects.go │ │ └── tasks.go │ └── model │ └── model.go ├── config │ └── config.go └── main.go Implementation of REST API Provide shared functions Make response with JSON handler/common.go
  40. handler/common.go੄ ੹୓ ҳઑ Implementation of REST API handler/common.go

  41. Implementation of REST API handler/common.go 1. ಁః૑ ࢶ঱ ߂ ೙ਃೠ

    ಁః૑ ੐ನ౟
  42. 2. JSON ನݘਵ۽ ਽׹ਸ ࢤࢿೞח ೣࣻ ੿੄ Implementation of REST

    API handler/common.go
  43. 3. JSON ನݘਵ۽ ী۞ ਽׹ਸ ࢤࢿೞח ೣࣻ ੿੄ Implementation of

    REST API handler/common.go
  44. ├── app │ ├── app.go │ ├── handler │ │

    ├── common.go │ │ ├── projects.go │ │ └── tasks.go │ └── model │ └── model.go ├── config │ └── config.go └── main.go Implementation of REST API handler/projects.go Provide handlers for Project Perform queries for Project data
  45. handler/projects.go੄ ੹୓ ҳઑ Implementation of REST API handler/projects.go

  46. Implementation of REST API handler/projects.go 1. ಁః૑ ࢶ঱ ߂ ೙ਃೠ

    ಁః૑ ੐ನ౟
  47. 2. /projects ೩ٜ۞ ੘ࢿ Implementation of REST API handler/projects.go GET

    : /projects : Get all projects POST : /projects : Create a new project
  48. 3. /projects/:title ೩ٜ۞ ੘ࢿ Implementation of REST API handler/projects.go GET

    : /projects/:title : Get a project PUT : /projects/:title : Update a project DELETE : /projects/:title : Delete a project
  49. 4. /projects/:title/archive ೩ٜ۞ ੘ࢿ Implementation of REST API handler/projects.go PUT

    : /projects/:title/archive : Archive a project DELETE : /projects/:title/archive : Restore a project
  50. 5. ױੌ ೐۽ં౟ܳ оઉয়ח ௪ܻ ېಌ ੘ࢿ Implementation of REST

    API handler/projects.go
  51. ├── app │ ├── app.go │ ├── handler │ │

    ├── common.go │ │ ├── projects.go │ │ └── tasks.go │ └── model │ └── model.go ├── config │ └── config.go └── main.go Implementation of REST API handler/tasks.go Provide handlers for Task Perform queries for Task data
  52. handler/projects.go੄ ੹୓ ҳઑ Implementation of REST API handler/tasks.go

  53. Implementation of REST API 1. ಁః૑ ࢶ঱ ߂ ೙ਃೠ ಁః૑

    ੐ನ౟ handler/tasks.go
  54. 2. /projects/:title/tasks ೩ٜ۞ ੘ࢿ Implementation of REST API GET :

    /projects/:title/tasks: Get all tasks of a project POST : /projects/:title/tasks: Create a new task in a project handler/tasks.go
  55. 3. /projects/:title/tasks/:id ೩ٜ۞ ੘ࢿ Implementation of REST API GET :

    /projects/:title/tasks/:id : Get a task of a project PUT : /projects/:title/tasks/:id: Update a task of a project DELETE : /projects/:title/tasks/:id : Delete a task of a project handler/tasks.go
  56. 3. /projects/:title/tasks/:id ೩ٜ۞ ੘ࢿ (continue) Implementation of REST API GET

    : /projects/:title/tasks/:id : Get a task of a project PUT : /projects/:title/tasks/:id: Update a task of a project DELETE : /projects/:title/tasks/:id : Delete a task of a project handler/tasks.go
  57. 4. /projects/:title/tasks/:id/complete ೩ٜ۞ ੘ࢿ Implementation of REST API PUT :

    /projects/:title/tasks/:id/complete : Complete a task of a project DELETE : /projects/:title/tasks/:id/complete : Undo a task of a project handler/tasks.go
  58. 5. ױੌ కझ௼ܳ оઉয়ח ௪ܻ ېಌ ੘ࢿ Implementation of REST

    API handler/tasks.go
  59. ├── app │ ├── app.go │ ├── handler │ │

    ├── common.go │ │ ├── projects.go │ │ └── tasks.go │ └── model │ └── model.go ├── config │ └── config.go └── main.go Implementation of REST API model/model.go Define the model structs with Gorm Provide some methods of models Provide a DB migration function
  60. model/model.go੄ ੹୓ ҳઑ Implementation of REST API model/model.go

  61. Implementation of REST API model/model.go 1. ಁః૑ ࢶ঱ ߂ ೙ਃೠ

    ಁః૑ ੐ನ౟
  62. 2. ప੉࠶ ݽ؛ ߂ ݽ؛ ؘ੉ఠ ઑ੘ী ೙ਃೠ ݫࢲ٘ ੿੄

    Implementation of REST API model/model.go
  63. 3. ੿੄ೠ ݽ؛ਸ पઁ ؘ੉ఠ߬੉झী ੸ਊೞח ݃੉Ӓۨ੉࣌ ೣࣻ ੘ࢿ Implementation

    of REST API model/model.go ݃੉Ӓۨ੉࣌җ ೣԋ Foreign Keyܳ ࢎਊೠ Relationshipө૑ ࢤࢿ
  64. ├── app │ ├── app.go │ ├── handler │ │

    ├── common.go │ │ ├── projects.go │ │ └── tasks.go │ └── model │ └── model.go ├── config │ └── config.go └── main.go Implementation of REST API config/config.go Provide a struct for configuration Config values is predefined
  65. 1. Config ҳઑ୓৬ DBConfig ҳઑ୓ ੿੄ Implementation of REST API

    config/config.go DBCofingܳ ࢚ਤ Config ҳઑ୓ী ੐߬٬
  66. 2. Predefined ࢸ੿чਸ о૓ Config ੋझఢझܳ ߈ജೞח ೣࣻ ੘ࢿ Implementation

    of REST API config/config.go
  67. ├── app │ ├── app.go │ ├── handler │ │

    ├── common.go │ │ ├── projects.go │ │ └── tasks.go │ └── model │ └── model.go ├── config │ └── config.go └── main.go Implementation of REST API main.go Main for running the app
  68. 1. Appী Config ੋझఢझܳ ੹׳೧ ୡӝച ೠ ٍ ࢲߡ प೯

    Implementation of REST API main.go
  69. Implementation of REST API Then $ go build $ ./go-todo-rest-api-example

  70. Implementation of REST API http://127.0.0.1:3000/projects/title/tasks/3 Test Example

  71. Implementation of REST API Congraturation!! You have a REST API

    written in Go now!
  72. Review & Next

  73. Review & Next Q. ௏٘о ੉ࢂо? Go ঱য ੗୓۽݅ ࠁݶ,

    ঱যী ࠂ੟ೠ ѐ֛ب ݆੉ হҊ ޙߨ੉ рѾೞݴ ղ ੢ ಁః૑о ೧઱ח ੌ੉ ݆ӝ ٸޙী ੹୓੸ੋ ௏٘੄ оةࢿ੉ ڄয૑૓ ঋ ਵݴ Gofmtۄח ో୓ੋ੉ ੓য ௏٘੄ ੌҙࢿਸ ਬ૑ೡ ࣻ ੓׮ח ੢੼੉ ੓ ਺ ૒੽ ѐߊೠ ҙ੼ীࢲ ࠁ੗ݶ, যו ੿ب ੹୓੸ੋ ҳઑܳ ࢤпೞҊ ࢸ҅৬ ௏ ٘ ѐߊਸ ೮ӝ ٸޙী അ੤ ࢚క۽ח աࢁ૑ ঋ׮Ҋ ࢤпೣ. 
 
 ׮݅, Go۽ REST API ѐߊ੉ ୊਺੉ӝب ೞҊ, ೐ۨ੐ਕ௼١ਸ ࢎਊೞ૑ ঋ ওӝী ୶࢚ചա ௏٘੄ ҳઑച١਷ ই૒ ؏ ૓೯غ঻׮Ҋ ࢤпೞݴ, ৈ੹൤ ѐࢶ੄ ৈ૑ח ҔҔী ࠁ੐. Review
  74. Q. ѐߊ դ੉بח যڃо? ૑ਗغח ղ੢ ಁః૑о ೂࠗೞݴ, খࢲ ঱ә೮٠੉

    ঱যо рѾೞѱ ٸޙী ѐߊ ੘স ੗୓ח ҭ੢൤ ࣻਘ೮਺. դ੉بۄҊ ೠ׮ݶ, য়൤۰ ੹୓੸ੋ ࢸ҅৬ ௏٘ ҳઑച৬ ҙ۲ػ դ੉بܳ ݈ೡ ࣻ ੓חؘ, ੉ח REST API੉ա ਢѐߊ ژח ইఃఫ୛੄ ૑ध੉ա ҃೷ ী ٮۄ ׳ۄ૕ Ѫ э਺ Review & Next Review
  75. Q. ೐ۨ੐ਕ௼ হ੉ ൨ٜ૑ ঋ਷о? ਗېח ઱۽ Pythonਵ۽ ਢѐߊҗ API

    ѐߊਸ ೧৳חؘ, Python੄ ҃਋ ࢎ प ೐ۨ੐ਕ௼ হ੉ ѐߊೞӝо ࢚׼൤ য۰਎ ࣻ ੓ਵݴ Flaskա Django١ ੄ ೐ۨ੐ਕ௼ח Ѣ੄ ೙ࣻ੐ Ӓ۞ա Goীࢲח ঱য ରਗীࢲ ઁҕೞח ղ੢ ಁః૑ੋ net/http۽ب ୽࠙ ೮਺. ౠ൤, Pythonীࢲ੄ Flaskա Djangoীࢲח ੗୓੸ਵ۽ ઁҕೞח ੜ ୶࢚ചػ ೣٜࣻਸ ࢎਊೞח ߑधҗ Goীࢲ੄ ղ੢ ಁః૑ܳ ࢎਊೞח ߑध ੉ Ѣ੄ ࠺त೮ӝ ٸޙী, ٮ۽ ೐ۨ੐ਕ௼੄ ೙ਃࢿ਷ ޅו՝ ޛۿ, ա੄ ҃਋ gorilla/mux৬ gormਸ оઉ׮ ॳӟ ೮૑݅ ੉ח ױࣽ൤ net/http੄ ખ ؊ ಞܻೠ ېಌ ӒܻҊ ORM੉ӝ ٸޙী ೙ਃೠ ҃਋ী݅ ݽ ٕ୊ۢ ॶ ࣻ ੓਺. ૊, ೙ਃೠ ࠗ࠙ী ೧׼ೞח ੘਷ ۄ੉࠳۞ܻٜਸ оઉ׮ ॳݶ غӝ ٸޙী ಽझఖ ೐ۨ੐ਕ௼ח ই૒ө૓ ೙ਃࢿਸ ޅו՝ Review & Next Review
  76. Q. Ӓۧ׮ݶ Auth, Sessions ١ਸ ਤೠ ۄ੉࠳۞ܻח ݆਷о? Review &

    Next ղо ࢎਊ೮؍ gorilla/mux੄ gorillaۄҊ ೞח ਢోఉীب gorilla/sessionsо ੓ਵݴ, jwt-go৬ э਷ ৈ۞ ࣁ࣌੉ա ࠁউ ҙ۲ ۄ੉࠳۞ܻٜ੉ ઓ੤ೞҊ ੓਺. Go۽ ٜ݅য૓ ೐ۨ੐ਕ௼ীࢲب ੗୓੸ਵ۽ ૑ਗೞח ҃਋ب ੓ਵݴ, ೐ۨ੐ਕ௼ ܳ ॳ૑ ঋח׮Ҋ೧ب ਤ৬ э਷ Ѫٜਸ оઉ׮ ॶ ࣻ ੓਺. Goח ޷ٜਝয ׮ܖӝо ೩ٜ۞ ׮ܖחѪҗ ௼ѱ ׮ܳѱ হয য۵૑ ঋӝ ٸޙী ۄ੉࠳۞ܻ݅ оઉ৬ࢲ ਤ੄ ӝמٜਸ ҳഅೞחѱ ࣻਘೡ ࣻ ੓਺ Review
  77. Review & Next Q. ҙܻ ژח ഛ੢ࢿ਷ જ਷о? ਋ࢶ Goܳ

    ੉޷ ॄࠌѢա ॳҊ ੓׮ח о੿ೞীࢲח, ѐੋ੸ਵ۽ח গ೒ܻா ੉࣌ ਬ૑ࠁࣻ৬ ഛ੢ࢿ਷ ݒ਋ ਋ࣻೞ׮Ҋ ࢤпೣ ௼۽झ ஹ౵ੌ੉ ૑ਗغݴ, ױੌ ߄੉ցܻ۽ ஹ౵ੌ੉ غӝ ٸޙী MSAী ҭ ੢൤ ੸೤೧ ഛ੢ࢿ੉ ҭ੢൤ જਵݴ, খࢲ ݈೮٠੉, ਢ ೐ۨ੐ਕ௼ী ؀ೠ ઙ ࣘࢿب ߹۽ হӝ ٸޙী ਗೞח ಁః૑ٜ݅ਸ оઉ׮о ݽٕ୊ۢ ॶ ࣻ ੓য ഛ੢੉ա ழझఠ݃੉૚੉ ಞܻೣ ژೠ go fmtա go test ӒܻҊ go docҗ э਷ ղ੢ ో୓ੋਸ ࢎਊೞݶ ௏ ٘ ੌҙࢿ ਬ૑, పझ౟ ӒܻҊ ޙࢲചب ݒ਋ औҊ ಞܻೞѱ ೡ ࣻ ੓਺ ಁః૑ ҙܻ੄ ҃਋ীח, ই૒ Go ౱ীࢲ ҕध੸ਵ۽ ѐߊೞҊ ੓ח ಁః૑ ݒפ੷о ߬ఋ ߡ੹੉ӝ ٸޙী ઑӘ ইए਍х੉ ੓ӟೞ૑݅ അઓೞח glide ܳ ࢎਊೡ ࣻب ੓Ҋ, റী ҕध ಁః૑ ݒפ੷ ੿ध ߡ੹੉ ܾܻૉ ػ׮ݶ ؊ ਌ ಞܻೡ Ѫਵ۽ ࠁ੐ Review
  78. Review & Next ޷ҳഅػ User৬ Authܳ ୶оೞৈ ࠁউө૑ Ҋ۰ೠ पઁ۽

    ࢎਊ оמೠ REST APIܳ ࢎਊೡ ࣻ ੓ب۾ ѐࢶ ߓನ੄ ਊ੉ࢿਸ ഝਊೠ ߓನ ೐۽ࣁझ ୶о ݽٚ APIী ؀ೠ ਬ׫ పझ౟௏٘ ୶о ખ ؊ RESTfulೞѱ ௏٘ܳ ੘ࢿೡ ࣻ ੓ب۾ ೞח Wrapper ѐߊ Next REST API ޙࢲച (with GoDoc) + ౚషܻ঴ ઁ੘
  79. хࢎ೤פ׮ MinJae Kwon (@mingrammer) 2017.04.15 GDG Incheon Web Seminar Make

    RESTful API with Go