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

    2017೥5݄ʹόʔδϣϯ2͕ϦϦʔε • Vapor Cloud Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 9
  2. Vapor toolbox ΛΠϯετʔϧ // Vapor toolboxͷΠϯετʔϧ brew install vapor //

    Πϯετʔϧͷ֬ೝ vapor --help Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 11
  3. Ϗϧυ&ϥϯ $ vapor new sampleWeb --template=web $ cd sampleWeb/ $

    vapor build $ vapor run Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 15
  4. Ͱ͖·͢ʂ! $ 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
  5. $ 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
  6. ৭ʑฉ͔ΕΔͷͰ౴͑Δ 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
  7. ͲΜͲΜ౴͑Δ 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
  8. ϧʔςΟϯά POSTϝιου POST /form drop.post("form") { request in return "Submitted

    with a POST request" } Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 29
  9. ύϥϝʔλʔ 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
  10. ΫΤϦʔ 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
  11. 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
  12. ·ͱΊ • Server Side Swift Vapor • Vapor ToolsͰϏϧυ&HerokuʹσϓϩΠ •

    ؆ܿͳϝιουͰ։ൃ͠΍͍͢ Swift αʔόʔαΠυ Vaporͷ࢝Ίํ 2017/8/29 38