Slide 1

Slide 1 text

S W I F T O U T S I D E T H E B O X Y U S U K E I T O S H I R O YA G I C O R P O R AT I O N T RY ! S W I F T T O K Y O 2 0 1 7 Twitter @novi_ Github @novi

Slide 2

Slide 2 text

T O P I C S • Server Side Swift • 2 Real Projects • 1. • 2. • Porting Library to Swift

Slide 3

Slide 3 text

S E R V E R S I D E S W I F T

Slide 4

Slide 4 text

• Swift is open-sourced since Dec 2015 • Foundation, XCTest and Dispatch • Package Manager • Works on macOS and Linux(Ubuntu) • Platform: x86_64, ARM(unofficial) • Oct 2016: Server APIs Work Group launched

Slide 5

Slide 5 text

• Swift for Non-Apple Platform • for Web servers, API servers • for Microservices • for Daemons, Utilities, Tools • Runs on… • Linux (IBM, Amazon, Google cloud) • Linux board computers (e.g. Raspberry Pi)

Slide 6

Slide 6 text

W H Y S W I F T ? • Traditionally Use Node.js, Python, Ruby, etc… for Server-Side programming • Swift has… • Types, Protocol, Value Associated Enumeration, etc… for Better, Safer Programming • Good Performance • Alternatives for Scripting Programming Languages

Slide 7

Slide 7 text

B A C K E N D S E R V I C E 
 W I T H S W I F T R E A L P R O J E C T 1 , O U R C O M PA N Y P R O J E C T

Slide 8

Slide 8 text

S W I F T B A S E D W E B C R AW L E R • Backend service of our products • RSS/HTML Crawling / Web scraping • Crawl specified Web pages, Parse the HTML, Save to the database as a Search bot • Extract body text excluding Navigation or submenu on the HTML

Slide 9

Slide 9 text

a Web Page URL = https://swift.org/blog/swift-4-0-release-process/ N AV I G AT I O N E L E M E N T S METADATA ELEMENTS BODY ELEMENTS

Slide 10

Slide 10 text

Settings Contents Images Save settings, Set HTML extraction rules (XPaths) Get HTML and Text result of crawling Fetch a Web HTML Parse the HTML/XML Save to the Database CRAWLER (Process) D ATA B A S E ( M Y S Q L / S 3 ) API SERVER (In-house use) Based on gRPC protcol

Slide 11

Slide 11 text

W H AT W E N E E D ? • HTTP(S)Client • XML/HTML Parsing, XPath(CSS Selector) traversing • MySQL Library • S3 Client • gRPC, Protocol Buffers Library • Japanese Charset converter (for Shift-JIS, EUC_JP charset)

Slide 12

Slide 12 text

S W I F T A N D C L A N G U A G E • Swift is LLVM based • Link with C based Library • Export C interface to Swift transparently • Swift Package Manager supports C compile (.c and .h) All C Libraries are Available on Swift!!

Slide 13

Slide 13 text

W H AT W E N E E D ? ( A G A I N ) • HTTPClient, AWS S3Client • libcurl (C based) • XML/HTML Parsing, XPath(CSS Selector) traversal • libxml2 (C based) • MySQL Library • C based mysqlconnector • Japanese Charset converter (Shift-JIS, EUC_JP) • nkf(Network Kanji Filter) (C based)

Slide 14

Slide 14 text

• gRPC: grpc/grpc-swift • Protobuf: apple/swift-protobuf • SHA1: IBM-Swift/BlueCryptor

Slide 15

Slide 15 text

O U R C U R R E N T S TAT U S • Code base(including Tests) • Crawler:~10,000 lines • In-house library: ~9,000 lines • Unit Tests is works well with Xcode • Running on macOS fine, ~20 days OK! • Running on our Development environment (Docker, Linux), 7 days OK!

Slide 16

Slide 16 text

O U R N E X T S T E P … • Deploy to Production environment (AWS) • Mesure performance, stability

Slide 17

Slide 17 text

W O R K I N G W I T H 
 S E N S O R S P R O J E C T 2 : P E R S O N A L P R O J E C T

Slide 18

Slide 18 text

• Swift for ARM and Raspberry Pi (Linux board computer) • Control many Devices via GPIO and I2C bus • Various Sensors, Lights, LCD, … • Combine with Swift libraries • Of Course, Written in Swift all !! Swift for ARM: https://github.com/iachievedit/package-swift

Slide 19

Slide 19 text

Linux board Air Pressure Sensor S E N S O R R A S P B E R RY P I S E R V E R S I D E S W I F T Slack API S E R V E R HTTPS POST hPa ℉ (℃) I2C I N T E R N E T WiFi

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

• Controlling I2C bus • #include • I2C bus file: /dev/i2c-0… • Use “fopen”, “ioctl” system call • github.com/novi/i2c-swift-example

Slide 23

Slide 23 text

sensor data MesurementResult(temperature: 229, pressure: 101507) // get current air pressure and temperature let device = try getCurrentI2CDevice() let sensor = BMP180(device: device) try sensor.reset() try sensor.calibrate() let result = try sensor.mesure(oversampling: .oss3) print("sensor data", result)

Slide 24

Slide 24 text

// build message let temp = Float(result.temperature) / 10 let fahrenheit = 1.8 * Float(result.temperature) / 10 + 32 let press = Float(result.pressure) / 100 // in hpa let tempStr = String(format: "%.0f°F(%.1f°C)", fahrenheit, temp) let pressStr = String(format: "%.2fhPa", press) let message = "\(tempStr) \(pressStr) \(formatDate(Date()))" // post to slack slack.sendMessage(channel: sendChannel, text: message, success: { (a, b) in

Slide 25

Slide 25 text

P O R T I N G L I B R A RY T O S W I F T

Slide 26

Slide 26 text

1. Find C based Library • Wrap with Swift ➜ • Make it more Swifty ➜ • e.g.) libcurl, libxml, mysqlconnector 2. Find similar Libraries in Node.js, Python, Ruby… • Port other Language’s Library back to Swift ➜ • Make it more Swifty ➜ • e.g.) NKF Write your Swift program with them

Slide 27

Slide 27 text

M Y L I B R A R I E S F O R S E R V E R S I D E S W I F T • MySQL: novi/mysql-swift • NKF: novi/nkf-swift • I2C: novi/i2c-swift

Slide 28

Slide 28 text

Tokyo-Server Side Swift Meetup https://tokyo-ss-swift.connpass.com

Slide 29

Slide 29 text

S TA RT I N G S E R V E R S I D E S W I F T W I T H … • Batch programs • API Servers • In-house deployment tools • Command-line utilities

Slide 30

Slide 30 text

• Start with SPM(Swift Package Manager) • Similar to Carthage and Cocoapods $ swift package init —type=executable hello world $ swift package init —type=library $ swift package generate-xcodeproj

Slide 31

Slide 31 text

S C R I P T I N G S W I F T • $ touch test.swift && chmod a+x test.swift • $ ./test.swift
 apple.com #!/usr/bin/env swift import Foundation let url = URLComponents(string: "https://apple.com")! print(url.host!)

Slide 32

Slide 32 text

C O N C L U S I O N • Swift is great programming language • Safety, Efficiently • Open-sourced • Works well with C based Library • Works well on Various Platforms

Slide 33

Slide 33 text

try! Server Side Swift with your Small Projects