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 만드는 방법 소개)

mingrammer

April 15, 2017
Tweet

More Decks by mingrammer

Other Decks in Programming

Transcript

  1. Name ӂ޹੤ (MinJae Kwon) Nickname @mingrammer Email [email protected] Who ই઱؀೟Ү

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

    Design & Architecture Contents 5. Review & Next 1. RESTful? REST API?
  3. RESTful? REST API? REST API? HTTP ࢸ҅੄ ੢੼ਸ ୭؀ೠ ࢓ܽ

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

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

    Fully Client - Server Hierarchical Structure Self Descriptiveness
  6. Why Go? 1. ೧ࠁҊ रযࢲ 2. ೐ۨ੐ਕ௼о ೙ࣻח ইש 3.

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

    र঻਺ ղ੢ ಁః૑о ъ۱೧ ߹ب੄ ೐ۨ੐ਕ௼ হ੉ب ѐߊ੉ оמ MSA (Micro Service Architecture)ী ੸೤ ࠽٘ ߂ ߓನо औҊ рಞ + + + + పझ౟ ಞೣ ࢿמ જ਺ ޙࢲച ಞೣ …
  8. API Design & Architecture Models Project Title :: string Archived

    :: bool Task Title :: string Priority :: enum Deadline :: datetime Done :: bool ProjectID :: int 1 : n
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. Implementation of REST API app/app.go 1. ಁః૑ ࢶ঱ ߂ ೙ਃೠ

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

    github.com/jinzhu/gorm੄ DB ࢎਊ 2. ۄ਋ఠ৬ DBܳ ыחApp ҳઑ୓ ࢶ঱
  16. ├── 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
  17. ├── 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
  18. 2. /projects ೩ٜ۞ ੘ࢿ Implementation of REST API handler/projects.go GET

    : /projects : Get all projects POST : /projects : Create a new project
  19. 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
  20. 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
  21. ├── 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. ├── 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
  27. 3. ੿੄ೠ ݽ؛ਸ पઁ ؘ੉ఠ߬੉झী ੸ਊೞח ݃੉Ӓۨ੉࣌ ೣࣻ ੘ࢿ Implementation

    of REST API model/model.go ݃੉Ӓۨ੉࣌җ ೣԋ Foreign Keyܳ ࢎਊೠ Relationshipө૑ ࢤࢿ
  28. ├── 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
  29. 1. Config ҳઑ୓৬ DBConfig ҳઑ୓ ੿੄ Implementation of REST API

    config/config.go DBCofingܳ ࢚ਤ Config ҳઑ୓ী ੐߬٬
  30. ├── 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
  31. Review & Next Q. ௏٘о ੉ࢂо? Go ঱য ੗୓۽݅ ࠁݶ,

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

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

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

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

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

    ࢎਊ оמೠ REST APIܳ ࢎਊೡ ࣻ ੓ب۾ ѐࢶ ߓನ੄ ਊ੉ࢿਸ ഝਊೠ ߓನ ೐۽ࣁझ ୶о ݽٚ APIী ؀ೠ ਬ׫ పझ౟௏٘ ୶о ખ ؊ RESTfulೞѱ ௏٘ܳ ੘ࢿೡ ࣻ ੓ب۾ ೞח Wrapper ѐߊ Next REST API ޙࢲച (with GoDoc) + ౚషܻ঴ ઁ੘