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

Perl MongersのためのServer Side Swift入門

isaoeka
December 20, 2016

Perl MongersのためのServer Side Swift入門

YAPC::Hokkaido 2016 前夜祭で発表させていただきました。
http://yapcjapan.org/2016hokkaido/

ツッコミ等あれば twitter : @isaoekaまで

isaoeka

December 20, 2016
Tweet

More Decks by isaoeka

Other Decks in Programming

Transcript

  1. 2 自己紹介 ▪ Տ໺ޭ ▪ ΤンδχΞ!.PCJMF'BDUPSZ *OD ▪ Twitter :

    @isaoeka ▪ ೘ࢎ஋͕ߴいͰ͢ ▪ ࠷ۙ͸ΞイΧπΛݟͯい·͢
  2. 7 オープンソースになったSwift (3.0 ~ ) ▪ ίΞライϒラϦ ▪ Foundation ▪

    GDC ▪ XCTest CͰ࣮૷(CoreFoundation) SwiftͰCoreFoundationΛݺͿ(Foundation) し͔しFoundation͸、ະ։ൃɾ࣮૷なՕॴ͕࢒ͬ ͯいΔ
  3. 8 Swift Web Server Gateway Interface ▪ Nest (nestproject/Nest) ▪

    Currasow ▪ Http4swift ▪ NestBox ▪ Open Swift (open-swift/S4) ▪ C7: Core Components ▪ S4: Server Side ▪ D5: Database
  4. 9 Swift Web Server Gateway Interface ▪ Nest (nestproject/Nest) ▪

    ։ൃ͕ࢭ·ͬͯいΔ ▪ Open Swift (open-swift/S4) ▪ ։ൃଧͪ੾Γ CoreFoundationͷ։ൃ͕ਐΈ、ॆ࣮し ͖ͯͨ͜ͱʹΑΓ、InterfaceΛఆٛ͢ Δඞཁ΋な͘なͬͨ(?) ֤8"'Ͱ(original core + Foundation) Λఆٛし࣮૷しͯいΔ
  5. ▪ apple/swift-package-manager ▪ ඪ४ͷライϒラϦ؅ཧπʔϧ ▪ ਽ܗ࡞੒swift package init 10 Swift

    Package Manager import PackageDescription let package = Package( name: "isucon6-swift", dependencies: [ .Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 1), .Package(url: "https://github.com/vapor/mysql-provider.git", majorVersion: 1) ] ) Package.swif
  6. 15 Perl Swift ▪ PSGI/Plack ▪ Carton ▪ search.cpan.org
 metacpan.org

    ▪ Vim/Emacs ▪ original + Foundation ▪ Swift Package Manager ▪ IBM Swift Package Catalog ▪ IDE (XcodeなͲ)
  7. import PerfectLib import PerfectHTTP import PerfectHTTPServer // Create HTTP server.

    let server = HTTPServer() // Register your own routes and handlers var routes = Routes() routes.add(method: .get, uri: "/", handler: { request, response in response.setHeader(.contentType, value: "text/html") response.appendBody(string: "<html><title>Hello, world!</ title><body>Hello, world!</body></html>") response.completed() } ) // Add the routes to the server. server.addRoutes(routes) // Set a listen port of 8181 server.serverPort = 8181 do { // Launch the HTTP server. try server.start() } catch PerfectError.networkError(let err, let msg) { print("Network error thrown: \(err) \(msg)") } 23 Perfect
  8. 24 Vapor ▪ vapor/vapor ▪ Laravel Inspired ▪ Full Stack

    ▪ セοτΞοϓ͕؆୯ ▪ ਓؾ͕ߴい ▪ ܕ҆શなϧʔςΟンά͕Մೳ
  9. import Vapor let drop = Droplet() drop.get("hello") { req in

    return "Hello, world." } drop.run() 25 Vapor
  10. import Vapor let drop = Droplet() drop.get("hello") { req in

    return "Hello, world." } drop.run() 26 Vapor ৠؾΛimport
  11. import Vapor let drop = Droplet() drop.get("hello") { req in

    return "Hello, world." } drop.run() 27 Vapor ৠؾΛimport ణΛ࡞Δ
  12. // My first Kitura Swift code! import Kitura let router

    = Router() router.get("/") { request, response, next in response.send("Hello, Server Side Swift") next() } Kitura.addHTTPServer(onPort: 8090, with: router) Kitura.run() 29 Kitura
  13. 30 Zewo ▪ Zewo/Zewo ▪ Modular Web Framework ▪ Full

    Stack ▪ ίンϙʔωンτ͕7BQPSͱޓ׵ੑ͕͋Γ、੾Γସ͑Δ͜ͱ ͕Մೳ
  14. import HTTPServer let log = LogMiddleware() let router = BasicRouter

    { route in route.get("/hello") { request in return Response(body: "Hello, world!") } } let server = try Server(port: 8080, middleware: [log], responder: router) try server.start() 31 Zewo
  15. 37 Vaporプロジェクトの作成 ▪ vapor new {project_name} ▪ cd {project_name} ▪

    “vapor/slack-bot” ͔ΒίʔυΛഈआ ▪ vapor xcode ▪ vapor build ▪ vapor run
  16. 38