7. Swift
Swift is a powerful and intuitive
programming language for macOS, iOS,
watchOS and tvOS. Writing Swift code is
interactive and fun, the syntax is concise
yet expressive, and Swift includes
modern features developers love. Swift
code is safe by design, yet also produces
software that runs lightning-fast.
8. History of Swift
• 0.x (2014/06)
• 1.0 (2014/09)
• 1.1 (2014/10)
• 1.2 (2015/02)
• 2.0 (2015/06)
• 2.1 (2015/10)
• 2.2 (2016/03)
• 3.0 (2016/09)
9. History of Swift
• 0.x (2014/06)
• 1.0 (2014/09)
• 1.1 (2014/10)
• 1.2 (2015/02)
• 2.0 (2015/06)
• 2.1 (2015/10)
• 2.2 (2016/03)
• 3.0 (2016/09)
黎明期
10. History of Swift
• 0.x (2014/06)
• 1.0 (2014/09)
• 1.1 (2014/10)
• 1.2 (2015/02)
• 2.0 (2015/06)
• 2.1 (2015/10)
• 2.2 (2016/03)
• 3.0 (2016/09)
成長期
11. History of Swift
• 0.x (2014/06)
• 1.0 (2014/09)
• 1.1 (2014/10)
• 1.2 (2015/02)
• 2.0 (2015/06)
• 2.1 (2015/10)
• 2.2 (2016/03)
• 3.0 (2016/09)
全盛期
12. [swift-evolution] Looking back on
Swift 3 and ahead to Swift 4
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160725/025676.html
ABI安定化など、Swift 3.0で実装予定だっ
た機能が見送られ、Swift 4.0まで持ち越
される。。。
13. Swift is Open Source
https://developer.apple.com/swift/blog/?id=34
14. Going Server-side with Swift Open Source
https://developer.apple.com/videos/play/
wwdc2016/415/
WWDC2016
37. $ swift —version
Apple Swift version 3.0 (swiftlang-800.0.43.6 clang-800.0.38)
Target: x86_64-apple-macosx10.9
$ xcode-select --switch /Applications/Xcode.app/Contents/Developer
Build
38. $ brew install openssl
$ brew link openssl --force
Build
$ git clone https://github.com/PerfectlySoft/PerfectTemplate.git
$ cd PerfectTemplate
$ swift build
$ .build/debug/PerfectTemplate
39.
40. // Create HTTP server.
let server = HTTPServer()
// Register your own routes and handlers
var routes = Routes()
// Add the routes to the server.
server.addRoutes(routes)
// Set a listen port of 8181
server.serverPort = 8181
// Set a document root.
// This is optional. If you do not want to serve static content then do not set this.
// Setting the document root will automatically add a static file handler for the route /**
server.documentRoot = "./webroot"
// Gather command line options and further configure the server.
// Run the server with --help to see the list of supported arguments.
// Command line arguments will supplant any of the values set above.
configureServer(server)
do {
// Launch the HTTP server.
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: (err) (msg)")
}
Server Start
41. // Create HTTP server.
let server = HTTPServer()
// Register your own routes and handlers
var routes = Routes()
// Add the routes to the server.
server.addRoutes(routes)
// Set a listen port of 8181
server.serverPort = 8181
// Set a document root.
// This is optional. If you do not want to serve static content then do not set this.
// Setting the document root will automatically add a static file handler for the route /**
server.documentRoot = "./webroot"
// Gather command line options and further configure the server.
// Run the server with --help to see the list of supported arguments.
// Command line arguments will supplant any of the values set above.
configureServer(server)
do {
// Launch the HTTP server.
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: (err) (msg)")
}
Server Start
42. // Create HTTP server.
let server = HTTPServer()
// Register your own routes and handlers
var routes = Routes()
// Add the routes to the server.
server.addRoutes(routes)
// Set a listen port of 8181
server.serverPort = 8181
// Set a document root.
// This is optional. If you do not want to serve static content then do not set this.
// Setting the document root will automatically add a static file handler for the route /**
server.documentRoot = "./webroot"
// Gather command line options and further configure the server.
// Run the server with --help to see the list of supported arguments.
// Command line arguments will supplant any of the values set above.
configureServer(server)
do {
// Launch the HTTP server.
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: (err) (msg)")
}
Server Start
43. // Create HTTP server.
let server = HTTPServer()
// Register your own routes and handlers
var routes = Routes()
// Add the routes to the server.
server.addRoutes(routes)
// Set a listen port of 8181
server.serverPort = 8181
// Set a document root.
// This is optional. If you do not want to serve static content then do not set this.
// Setting the document root will automatically add a static file handler for the route /**
server.documentRoot = "./webroot"
// Gather command line options and further configure the server.
// Run the server with --help to see the list of supported arguments.
// Command line arguments will supplant any of the values set above.
configureServer(server)
do {
// Launch the HTTP server.
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: (err) (msg)")
}
Server Start
44. // Create HTTP server.
let server = HTTPServer()
// Register your own routes and handlers
var routes = Routes()
// Add the routes to the server.
server.addRoutes(routes)
// Set a listen port of 8181
server.serverPort = 8181
// Set a document root.
// This is optional. If you do not want to serve static content then do not set this.
// Setting the document root will automatically add a static file handler for the route /**
server.documentRoot = "./webroot"
// Gather command line options and further configure the server.
// Run the server with --help to see the list of supported arguments.
// Command line arguments will supplant any of the values set above.
configureServer(server)
do {
// Launch the HTTP server.
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: (err) (msg)")
}
Server Start
57. create table message (
id serial primary key,
message text,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
• create message table
Create Table
58. GET Method
// get message
routes.add(method: .get, uri: "/message", handler: {
request, response in
defer {
response.completed()
}
do {
response.setHeader(.contentType, value: "application/json")
let connection = PGConnection()
let status = connection.connectdb(db)
let result = connection.exec(
statement: "select * FROM message order by updated_at desc")
…
// DB
let db = "postgresql://samplefuku:fukuoka@localhost:5432/exampledb"
59. GET Method…
let num = result.numTuples()
let messages: [[String : Any]] = (0..<num).map { x in
let t1 = result.getFieldString(tupleIndex: x, fieldIndex: 0)
let t2 = result.getFieldString(tupleIndex: x, fieldIndex: 1)
let t3 = result.getFieldString(tupleIndex: x, fieldIndex: 2)
let t4 = result.getFieldString(tupleIndex: x, fieldIndex: 3)
let message: [String : Any] = [
"id" : t1,
"message" : t2,
"created_at" : t3,
"updated_at" : t4
]
return message
}
result.clear()
connection.close()
try response.setBody(json: ["messages" : messages])
} catch let error as NSError {
print(error)
}
})
60. POST Method
// post message
routes.add(method: .post, uri: "/message", handler: {
request, response in
defer {
response.completed()
}
do {
let json = try request.postBodyString?.jsonDecode()
response.setHeader(.contentType, value: “application/json")
guard let decoded = json as? [String : Any] else { return }
…
61. POST Method
…
let result: [String : Any] = decoded["message"].map { message in
let connection = PGConnection()
let status = connection.connectdb(db)
let date = Date()
let createdAt = RFC3339DateFormatter.string(from: date)
let updatedAt = RFC3339DateFormatter.string(from: date)
let result = connection.exec(
statement: "insert into message (message, created_at,
updated_at) values($1, $2, $3)",
params: ["(message)", "(createdAt)", "(updatedAt)"])
result.clear()
connection.close()
return ["message" : message]
} ?? [:]
try response.setBody(json: result)
…