ã¯ããã«
ãã®è¨äºã§ã¯
- Swift 3.0.2
- Swift Package Manager
- Kitura
ãå©ç¨ãã¦ãHerokuä¸ã§ç°¡åãªãµã¼ããåä½ãããæé ãè¨è¼ãã¾ãã
Swift PMã使ã£ã¦ããã¸ã§ã¯ããã»ããã¢ãã
ã¾ãã¯ãææ°çã®Xcode8.2ãã¤ã³ã¹ãã¼ã«ãã¾ãããããã®å¾ã¯ã·ã§ã«ä¸ã§æ¬¡ã®ã³ãã³ããå©ãã¾ãã
$ mkdir kitura-test $ cd kitura-test $ swift package init --type executable
ããã§ã³ã¼ããæ¸ãæºåãæ´ãã¾ããããã£ã¬ã¯ããªæ§é ã¯æ¬¡ã®ããã«ãªã£ã¦ããã¯ãã§ããã·ã³ãã«ã§å®å¿ã
. âââ Package.swift âââ Sources â  âââ main.swift âââ Tests
ãã®æ®µéã§ããã«ããå¯è½ã§ãã
$ swift build $ ./.build/debug/kitura-test # Hello, world!
ã³ã³ã½ã¼ã«ã« Hello, world!
ãåºåãããã¨æãã¾ãããããã³ã¼ãã®ç·¨éããã«ããXcodeã§è¡ãããå ´åã¯ã
$ swift package generate-xcodeproj
ã¨ããã°Xcodeããã¸ã§ã¯ããçæããã¾ãã
Kituraãå°å ¥ãã¦ãã¼ã«ã«ã«ãµã¼ããç«ã¦ã
Package.swift
ãç·¨éãã¦ãKituraã使ããããã«ãã¾ãã
import PackageDescription let package = Package( name: "kitura-test", dependencies: [ .Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 1, minor: 4) ] )
次㫠Sources/main.swift
ãç·¨éãã¾ãã
import Foundation import Kitura let router = Router() router.get("/") { request, response, next in response.send("Hello, World!") next() } Kitura.addHTTPServer(onPort: 8090, with: router) Kitura.run()
ããã¾ã§ç·¨éãçµãã£ãã
$ swift build
$ ./.build/debug/kitura-test
ãå®è¡ãã¾ããããã©ã¤ãã©ãªãèªåã§å°å
¥ããããã«ããèµ°ãã¾ãã ./.build/debug/kitura-test
å®è¡æã«ç¹ã«ãã°ã¯æµãã¦ãã¾ãããããµã¼ãã¯ç«ã£ã¦ããã®ã§ãã©ã¦ã¶ããlocalhost:8090
ã«ã¢ã¯ã»ã¹ãã¦ã¿ã¾ããããHello, World!
ã¨è¡¨ç¤ºãããã°OKã§ãã
Herokuã«ãããã¤ãã
Herokuã«ãããã¤ããåã«ãPORTçªå·ããããªã«ããå¿
è¦ãããã¾ããã¾ã㯠Sources/main.swift
ãç·¨éãã¾ãããã
import Foundation import Kitura let router = Router() router.get("/") { request, response, next in response.send("Hello, World!") next() } // Herokuã§ãã¼ãçªå·ãå¤ããããããã«ãã let port: Int = Int(ProcessInfo.processInfo.environment["PORT"] ?? "8090") ?? 8090 Kitura.addHTTPServer(onPort: 8090, with: router) Kitura.run()
次ã«Procfile
ãçæãã¾ã
$ echo web: .build/release/kitura-test > Procfile
ãã¦ãããã§ã¯Herokuã«ãããã¤ãã¦ããã¾ãããã
$ git init $ git add . $ git commit -m 'first commit' $ heroku create your-project-name $ heroku buildpacks:set https://github.com/kylef/heroku-buildpack-swift $ git push heroku master # ãã«ããèµ°ãã®ã§ã¡ãã£ã¨æéãããã¾ã $ heroku open
heroku open
ãããã¨ãã©ã¦ã¶ãç«ã¡ä¸ããã¾ããç»é¢ã«Hello, World!
ã表示ãããã°åå©ããµã¼ããµã¤ãSwiftãHerokuã§åãããã¨ãã§ãã¾ããã
è£è¶³ï¼ãã°ãæµããã人ã¸
HeliumLoggerã¨ããã©ã¤ãã©ãªãå©ç¨ãã¾ããPackage.swift
ã¨Sources/main.swift
ãç·¨éãã¾ãã
import PackageDescription let package = Package( name: "kitura-test", dependencies: [ .Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 1, minor: 4), .Package(url: "https://github.com/IBM-Swift/HeliumLogger.git", majorVersion: 1, minor: 4) ] )
import Kitura import Foundation import HeliumLogger // HeliumLoggerãåæå HeliumLogger.use() let router = Router() router.get("/") { request, response, next in response.send("Hello, World!") next() } // Herokuã§ãã¼ãçªå·ãå¤ããããããã«ãã let port: Int = Int(ProcessInfo.processInfo.environment["PORT"] ?? "8090") ?? 8090 Kitura.addHTTPServer(onPort: port, with: router) Kitura.run()
ããã ãã§ãããã¨ã¯swift build
ãããå¾ã§ãµã¼ããç«ã¡ä¸ããã¢ã¯ã»ã¹ãããã¨
[2017-01-11T18:20:25.581+09:00] [VERBOSE] [Router.swift:67 init(mergeParameters:)] Router initialized [2017-01-11T18:20:25.596+09:00] [VERBOSE] [Kitura.swift:70 run()] Starting Kitura framework... [2017-01-11T18:20:25.596+09:00] [VERBOSE] [Kitura.swift:80 start()] Starting an HTTP Server on port 8090... [2017-01-11T18:20:25.597+09:00] [INFO] [HTTPServer.swift:85 listen(on:)] Listening on port 8090 [2017-01-11T18:20:43.031+09:00] [VERBOSE] [HTTPIncomingMessage.swift:245 parsingCompleted()] HTTP request from=127.0.0.1; proto=http;
ãã®ããã«ãã°ãåããã¾ãã便å©ä¾¿å©ã
æå¾ã«
Swift3ãéçºä¸ã ã£ãããã®ãµã¼ããµã¤ãSwiftã¯ãå¿ è¦ã§æå ã§åããã®ã大å¤ã§ããããä»ã§ã¯å°ãªãã³ã¼ãéã§ç°¡åã«åãããã¨ãã§ãã¾ãã
ã¾ããXcodeãå©ç¨ããã«ã³ã¼ãã®ç·¨éã»ãã«ãã»å®è¡ãã§ããã®ã§ãEmacsãVimãAtomã§Swiftãæ¸ããã人ã§ãå®å¿ã§ãããã ããè£å®æ©è½ã«ã¤ãã¦ã¯SourceKittenã使ãå¿ è¦ããããã¾ã ã¡ãã£ã¨ä¸ä¾¿ã§ãã
ã¨ã¯ãããæ®æ®µå©ç¨ãã¦ããSwiftã§ç°¡åã«ãµã¼ããä½ããã®ã§ãJSONãè¿ãã ãã®è¶£å³ãµã¼ããªã©ã¯ãRubyããSwiftã«ç§»è¡ãã¦ãããã¨æãã¾ãã