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/
での発表資料

Sato Takeshi

August 30, 2017
Tweet

More Decks by Sato Takeshi

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  9. Vapor
    • χϡʔϤʔΫͷQutheory͕ࣾ։ൃ͍ͯ͠Δ
    • ΦʔϓϯιʔεͷαʔόʔαΠυϑϨʔϜϫʔΫ
    • ૣ͍ɺ҆શ
    • υΩϡϝϯτ͕๛෋
    • 2017೥5݄ʹόʔδϣϯ2͕ϦϦʔε
    • Vapor Cloud
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 9

    View full-size slide

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

    View full-size slide

  11. Vapor toolbox ΛΠϯετʔϧ
    // Vapor toolboxͷΠϯετʔϧ
    brew install vapor
    // Πϯετʔϧͷ֬ೝ
    vapor --help
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 11

    View full-size slide

  12. ΞϓϦέʔγϣϯͷ࡞੒
    vapor new [--template]
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 12

    View full-size slide

  13. //APIͷςϯϓϨʔτͰ࡞੒(DBϞδϡʔϧͷFluent;͘Ή)
    vapor new --template=api
    //WebςϯϓϨʔτͰ࡞੒(ViewϞδϡʔϧͷLeafΛ;͘Ή)
    vapor new --template=web
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 13

    View full-size slide

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

    View full-size slide

  15. Ϗϧυ&ϥϯ
    $ vapor new sampleWeb --template=web
    $ cd sampleWeb/
    $ vapor build
    $ vapor run
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 15

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  23. $ vapor heroku init
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 23

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  26. VaporͷػೳҰཡ
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 26

    View full-size slide

  27. ϧʔςΟϯά GETϝιου
    GET /welcome
    drop.get("welcome") { request in
    return "Hello"
    }
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 27

    View full-size slide

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

    View full-size slide

  29. ϧʔςΟϯά POSTϝιου
    POST /form
    drop.post("form") { request in
    return "Submitted with a POST request"
    }
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 29

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  32. Ϩεϙϯε
    GET http://0.0.0.0:8080/users/12
    -> You requested User #12
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 32

    View full-size slide

  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

    View full-size slide

  34. Ϩεϙϯε
    GET http://0.0.0.0:8080/users?name=sato
    -> You requested User #sato
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 34

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  38. ·ͱΊ
    • Server Side Swift Vapor
    • Vapor ToolsͰϏϧυ&HerokuʹσϓϩΠ
    • ؆ܿͳϝιουͰ։ൃ͠΍͍͢
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 38

    View full-size slide

  39. iOSDCͰൃද͠·͢
    8/17 16:50ʙϥΠτχϯάτʔΫ
    https://iosdc.jp/2017/
    Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 39

    View full-size slide