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

Swift サーバーサイド Vaporの始め方

Swift サーバーサイド Vaporの始め方

GEEK GARAGE IOT vol2
https://geekgarage.connpass.com/event/64433/
での発表資料

53e2d354b3299d64a54af680865516d5?s=128

Sato Takeshi

August 30, 2017
Tweet

Transcript

  1. Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017೥8݄29೔ɹ Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 1

  2. ࠤ౻߶࢜ (@hatakenokakashi) MAMORIOגࣜձࣾͱ͍͏མͱ͠෺λά ͷձࣾͰ iOSΤϯδχΞΛ͍ͯ͠·͢ɻ ։ൃ͔ΒAppStore΁ͷϦϦʔε࡞ۀ΋ ͯ͠·͢ɻ ϒϩάɿhttp://blog.personal- factory.com/ Swift

    αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 2
  3. 2015೥12݄ Swift͕ΦʔϓϯιʔεʹͳΓ·ͨ͠ ! " Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 3

  4. αʔόʔαΠυϑϨʔϜϫʔΫ͕ଓʑ ొ৔ Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 4

  5. CodeZinʹͯهࣄΛॻ͖·ͨ͠ http://codezine.jp/article/detail/9622 Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 5

  6. Kituraͷྑ͞ • IBM Cloud Tools ͰGUI͔ΒBluemix΁σϓϩΠ͕Մೳ • ؆ܿͳϧʔςΟϯά Swift αʔόʔαΠυ

    Vaporͷ࢝Ίํ 2017/8/29 6
  7. ౰࣌ͷਏΈ • Swift2ͰϚΠφʔόʔδϣϯ্͕͕Δͱಈ͔ͳ͘ͳΔ • HTTPϦΫΤετϝιου͕CURLͷϥούʔ(Not Swifty) • SQLϞδϡʔϧ͕Kituraʹ͸ͳ͍ Swift αʔόʔαΠυ

    Vaporͷ࢝Ίํ 2017/8/29 7
  8. Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 8

  9. Vapor • χϡʔϤʔΫͷQutheory͕ࣾ։ൃ͍ͯ͠Δ • ΦʔϓϯιʔεͷαʔόʔαΠυϑϨʔϜϫʔΫ • ૣ͍ɺ҆શ • υΩϡϝϯτ͕๛෋ •

    2017೥5݄ʹόʔδϣϯ2͕ϦϦʔε • Vapor Cloud Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 9
  10. Vaporͷ࢝Ίํ Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 10

  11. Vapor toolbox ΛΠϯετʔϧ // Vapor toolboxͷΠϯετʔϧ brew install vapor //

    Πϯετʔϧͷ֬ೝ vapor --help Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 11
  12. ΞϓϦέʔγϣϯͷ࡞੒ vapor new <name> [--template] Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 12

  13. //APIͷςϯϓϨʔτͰ࡞੒(DBϞδϡʔϧͷFluent;͘Ή) vapor new <name> --template=api //WebςϯϓϨʔτͰ࡞੒(ViewϞδϡʔϧͷLeafΛ;͘Ή) vapor new <name> --template=web

    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 13
  14. Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 14

  15. Ϗϧυ&ϥϯ $ vapor new sampleWeb --template=web $ cd sampleWeb/ $

    vapor build $ vapor run Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 15
  16. http://0.0.0.0:8080/ ͰWebαʔόʔ্ཱ͕͕ͪΔ Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 16

  17. XcodeͰ։ൃ͍ͨ͠ʁ Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 17

  18. Ͱ͖·͢ʂ! $ vapor xcode Generating Xcode Project [Done] Select the

    `Run` scheme to run. Open Xcode project? y/n> y Opening Xcode project... Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 18
  19. Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 19

  20. ؆୯ʹσϓϩΠ͍ͨ͠ʁ Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 20

  21. HerokuͰσϓϩΠʂ gitͰίϛοτͯ͠vapor heroku Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 21

  22. $ git add ./ $ git commit -m "init commit"

    [master 8fd2d8e] init commit 1 file changed, 169 insertions(+) create mode 100644 Package.resolved Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 22
  23. $ vapor heroku init Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 23

  24. ৭ʑฉ͔ΕΔͷͰ౴͑Δ Would you like to provide a custom Heroku app

    name? y/n> y Custom app name: > sample-vapor-web Would you like to deploy to other than US region server? y/n> y Region code (us/eu): > us https://sample-vapor-web.herokuapp.com/ | https://git.heroku.com/sample-vapor-web.git Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 24
  25. ͲΜͲΜ౴͑Δ Would you like to provide a custom Heroku buildpack?

    y/n> n Setting buildpack... Are you using a custom Executable name? y/n> n Setting procfile... Committing procfile... Would you like to push to Heroku now? y/n> y This may take a while... Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 25
  26. VaporͷػೳҰཡ Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 26

  27. ϧʔςΟϯά GETϝιου GET /welcome drop.get("welcome") { request in return "Hello"

    } Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 27
  28. Ϩεϙϯε http://0.0.0.0:8080/welcome -> Hello Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 28

  29. ϧʔςΟϯά POSTϝιου POST /form drop.post("form") { request in return "Submitted

    with a POST request" } Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 29
  30. Ϩεϙϯε POST http://0.0.0.0:8080/form -> Submitted with a POST request Swift

    αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 30
  31. ύϥϝʔλʔ GET /users/:id drop.get("users", ":id") { request in guard let

    userId = request.parameters["id"]?.int else { throw Abort.badRequest } return "You requested User #\(userId)" } Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 31
  32. Ϩεϙϯε GET http://0.0.0.0:8080/users/12 -> You requested User #12 Swift αʔόʔαΠυ

    Vaporͷ࢝Ίํ 2017/8/29 32
  33. ΫΤϦʔ GET /users?name="xxx" drop.get("users") { request in guard let name

    = request.query?["name"]?.string else { throw Abort.badRequest } return "You requested User #\(name)" } Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 33
  34. Ϩεϙϯε GET http://0.0.0.0:8080/users?name=sato -> You requested User #sato Swift αʔόʔαΠυ

    Vaporͷ࢝Ίํ 2017/8/29 34
  35. JSON GET /json drop.get("json") { req in var json =

    JSON() try json.set("id", id) try json.set("name", name) try json.set("age", age) return json } Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 35
  36. Ϩεϙϯε GET http://0.0.0.0:8080/json -> {"id":1,"name":"tommy","age":10} Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 36

  37. σϞ Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 37

  38. ·ͱΊ • Server Side Swift Vapor • Vapor ToolsͰϏϧυ&HerokuʹσϓϩΠ •

    ؆ܿͳϝιουͰ։ൃ͠΍͍͢ Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 38
  39. iOSDCͰൃද͠·͢ 8/17 16:50ʙϥΠτχϯάτʔΫ https://iosdc.jp/2017/ Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 39