frenchkit 2017: server(less) swift

84
Server(less) Swift with Kitura and OpenWhisk Chris Bailey @Chris__Bailey FrenchKit 2017 September 22nd

Upload: chris-bailey

Post on 22-Jan-2018

344 views

Category:

Software


0 download

TRANSCRIPT

Page 1: FrenchKit 2017: Server(less) Swift

Server(less) Swiftwith Kitura and OpenWhisk

Chris Bailey@Chris__Bailey

FrenchKit 2017September 22nd

Page 2: FrenchKit 2017: Server(less) Swift

2

(less) SwiftServer

Page 3: FrenchKit 2017: Server(less) Swift

3

SwiftServer

Page 4: FrenchKit 2017: Server(less) Swift

4

FullStack

0

17.5

35

52.5

70

Full

Stac

k

Back

end

Fron

tend

11.9

24.4

63.7

Page 5: FrenchKit 2017: Server(less) Swift

5

FullStack

0

17.5

35

52.5

70

Full

Stac

k

Back

end

Fron

tend

11.9

24.4

63.7

0

40

80

120

160

141.5

16.24.34

Fast

Page 6: FrenchKit 2017: Server(less) Swift

6

FullStack

0

17.5

35

52.5

70

Full

Stac

k

Back

end

Fron

tend

11.9

24.4

63.7

0

40

80

120

160

141.5

16.24.34

Fast

0

15

30

45

60

50.2

31.329.9

11.7

Efficient

Page 7: FrenchKit 2017: Server(less) Swift

7

iOS APP

Hosted ServicesGATEWAY

PUBLIC NETWORK CLOUD NETWORK

SWAGGERDATA

PUSH ANALYTICS

DEVOPS AVAILABILITY MONITORINGSCALING

SOCIAL

COGNATIVE

AUTH

WEB APP

Page 8: FrenchKit 2017: Server(less) Swift

8

Page 9: FrenchKit 2017: Server(less) Swift

import Kitura import SwiftyJSON

9

Page 10: FrenchKit 2017: Server(less) Swift

import Kitura import SwiftyJSON

router.all("/*", middleware: BodyParser()) router.post(path, handler: handleCreate)

10

Page 11: FrenchKit 2017: Server(less) Swift

import Kitura import SwiftyJSON

router.all("/*", middleware: BodyParser()) router.post(path, handler: handleCreate)

func handleCreate(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) {

}

11

Page 12: FrenchKit 2017: Server(less) Swift

import Kitura import SwiftyJSON

router.all("/*", middleware: BodyParser()) router.post(path, handler: handleCreate)

func handleCreate(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) { guard case .json(let json)? = request.body else { response.status(.badRequest) response.send(json: JSON([ "error": "Request not JSON" ])) return next() }

}

12

Page 13: FrenchKit 2017: Server(less) Swift

import Kitura import SwiftyJSON

router.all("/*", middleware: BodyParser()) router.post(path, handler: handleCreate)

func handleCreate(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) { guard case .json(let json)? = request.body else { response.status(.badRequest) response.send(json: JSON([ "error": "Request not JSON" ])) return next() } let name = json["name"].string let rating = json[“rating"].number // etc // etc

}

13

Page 14: FrenchKit 2017: Server(less) Swift

import Kitura import SwiftyJSON

router.all("/*", middleware: BodyParser()) router.post(path, handler: handleCreate)

func handleCreate(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) { guard case .json(let json)? = request.body else { response.status(.badRequest) response.send(json: JSON([ "error": "Request not JSON" ])) return next() } let name = json["name"].string let rating = json[“rating"].number // etc // etc next() }

14

Page 15: FrenchKit 2017: Server(less) Swift

15

Decreasing concern (and control) over stack implementation

Incr

easi

ng fo

cus

on b

usin

ess

logi

c

IaaS

Page 16: FrenchKit 2017: Server(less) Swift

16

Decreasing concern (and control) over stack implementation

Incr

easi

ng fo

cus

on b

usin

ess

logi

c

IaaS

Page 17: FrenchKit 2017: Server(less) Swift

17

Decreasing concern (and control) over stack implementation

Incr

easi

ng fo

cus

on b

usin

ess

logi

c

IaaS

CaaS

Page 18: FrenchKit 2017: Server(less) Swift

18

Decreasing concern (and control) over stack implementation

Incr

easi

ng fo

cus

on b

usin

ess

logi

c

IaaS

CaaS

PaaS

Page 19: FrenchKit 2017: Server(less) Swift

19

Decreasing concern (and control) over stack implementation

Incr

easi

ng fo

cus

on b

usin

ess

logi

c

IaaS

CaaS

PaaS

?

Page 20: FrenchKit 2017: Server(less) Swift

20

Decreasing concern (and control) over stack implementation

Incr

easi

ng fo

cus

on b

usin

ess

logi

c

IaaS

CaaS

PaaS

? Serverless

Serverless

Page 21: FrenchKit 2017: Server(less) Swift

21

Functions-as-a-Service

Page 22: FrenchKit 2017: Server(less) Swift

22

PaaS FaaS

OpenWhisk

Page 23: FrenchKit 2017: Server(less) Swift

23

PaaSWrite application centric business logic.

FaaSWrite event-driven business logic.

OpenWhisk

Page 24: FrenchKit 2017: Server(less) Swift

24

PaaSWrite application centric business logic.

Treat compute resources as utilities.

FaaSWrite event-driven business logic.Treat compute resources as utilities.

OpenWhisk

Page 25: FrenchKit 2017: Server(less) Swift

25

PaaSWrite application centric business logic.

Treat compute resources as utilities.

Provides scaling, resilience and availability

FaaSWrite event-driven business logic.Treat compute resources as utilities.

Provides scaling, resilience andavailability

OpenWhisk

Page 26: FrenchKit 2017: Server(less) Swift

26

“If your PaaS can efficiently start instances in 20ms that run for half a second, then call it serverless.”

- Adrian Cockcroft

Page 27: FrenchKit 2017: Server(less) Swift

27

Page 28: FrenchKit 2017: Server(less) Swift

28

Page 29: FrenchKit 2017: Server(less) Swift

29

func main(args: [String:Any]) -> [String:Any] { // Action code // goes here return [ “result" : "value" ] }

Page 30: FrenchKit 2017: Server(less) Swift

30

func main(args: [String:Any]) -> [String:Any] { // Action code // goes here return [ “result" : "value" ] }

Entry Point

Page 31: FrenchKit 2017: Server(less) Swift

31

func main(args: [String:Any]) -> [String:Any] { // Action code // goes here return [ “result" : "value" ] }

Entry Point Event Parameters

Page 32: FrenchKit 2017: Server(less) Swift

32

func main(args: [String:Any]) -> [String:Any] { // Action code // goes here return [ “result" : "value" ] }

Entry Point Event Parameters

Result

Page 33: FrenchKit 2017: Server(less) Swift

33

func main(args: [String:Any]) -> [String:Any] {

}

Page 34: FrenchKit 2017: Server(less) Swift

34

func main(args: [String:Any]) -> [String:Any] { if let name = args["name"] as? String {

} }

Page 35: FrenchKit 2017: Server(less) Swift

35

func main(args: [String:Any]) -> [String:Any] { if let name = args["name"] as? String { return [ "greeting" : "Hello \(name)!" ]

} }

Page 36: FrenchKit 2017: Server(less) Swift

36

func main(args: [String:Any]) -> [String:Any] { if let name = args["name"] as? String { return [ "greeting" : "Hello \(name)!" ] } else { return [ "greeting" : "Hello stranger!" ] } }

Page 37: FrenchKit 2017: Server(less) Swift

37

$ git clone https://github.com/openwhisk/openwhisk.git

Get OpenWhisk

Page 38: FrenchKit 2017: Server(less) Swift

38

$ git clone https://github.com/openwhisk/openwhisk.git

$ cd openwhisk/tools/vagrant

$ vagrant up

Get OpenWhisk

Page 39: FrenchKit 2017: Server(less) Swift

39

$ git clone https://github.com/openwhisk/openwhisk.git

$ cd openwhisk/tools/vagrant

$ vagrant up

Get OpenWhisk

Start OpenWhisk (in a Linux VM)

Page 40: FrenchKit 2017: Server(less) Swift

40

Page 41: FrenchKit 2017: Server(less) Swift

$ wsk action create hello main.swift

41

Page 42: FrenchKit 2017: Server(less) Swift

$ wsk action create hello main.swift

42

Create Action

Page 43: FrenchKit 2017: Server(less) Swift

$ wsk action create hello main.swift

43

Create Action Called

Page 44: FrenchKit 2017: Server(less) Swift

$ wsk action create hello main.swift

44

Create Action Called From

Page 45: FrenchKit 2017: Server(less) Swift

$ wsk action create hello main.swift ok: created action hello

45

Create Action Called From

Page 46: FrenchKit 2017: Server(less) Swift

$ wsk action create hello main.swift ok: created action hello

$ wsk action list

46

Create Action Called From

Page 47: FrenchKit 2017: Server(less) Swift

$ wsk action create hello main.swift ok: created action hello

$ wsk action list

actions hello private

47

Create Action Called From

Page 48: FrenchKit 2017: Server(less) Swift

$ wsk action create hello main.swift ok: created action hello

$ wsk action list

actions hello private

48

Create Action

Our Action

Called From

Page 49: FrenchKit 2017: Server(less) Swift

$ wsk action invoke --result hello --param name World

49

Page 50: FrenchKit 2017: Server(less) Swift

$ wsk action invoke --result hello --param name World

50

Invoke Action

Page 51: FrenchKit 2017: Server(less) Swift

$ wsk action invoke --result hello --param name World

51

Invoke Action Called

Page 52: FrenchKit 2017: Server(less) Swift

$ wsk action invoke --result hello --param name World

52

Invoke Action Called

With Parameters

Page 53: FrenchKit 2017: Server(less) Swift

$ wsk action invoke --result hello --param name World

{

“greeting" : “Hello World!”

}

53

Invoke Action Called

With Parameters

Page 54: FrenchKit 2017: Server(less) Swift

54

TTriggers• Database update • IoT sensor activation • GitHub hook • Slack notification • REST request

T

Page 55: FrenchKit 2017: Server(less) Swift

55

T ATriggers• Database update • IoT sensor activation • GitHub hook • Slack notification • REST request

Actions• Your code • Single task • Any language • Docker binary • Returns JSON

AT

Page 56: FrenchKit 2017: Server(less) Swift

56

T A RTriggers• Database update • IoT sensor activation • GitHub hook • Slack notification • REST request

Actions• Your code • Single task • Any language • Docker binary • Returns JSON

Rules• Link Triggers to

Actions • Create sequences • Many to many maps

become possible

ATR :=

Page 57: FrenchKit 2017: Server(less) Swift

57

T A RTriggers• Database update • IoT sensor activation • GitHub hook • Slack notification • REST request

Actions• Your code • Single task • Any language • Docker binary • Returns JSON

Rules• Link Triggers to

Actions • Create sequences • Many to many maps

become possible

ATR A A1 A2 A3:= + +:=

Page 58: FrenchKit 2017: Server(less) Swift

58

T A R PTriggers• Database update • IoT sensor activation • GitHub hook • Slack notification • REST request

Actions• Your code • Single task • Any language • Docker binary • Returns JSON

Rules• Link Triggers to

Actions • Create sequences • Many to many maps

become possible

Packages• Collection of triggers and actions

ATR A A1 A2 A3

AB A2 A1 A4TR

:=

:=

+

+

+

+

:=

:=

Page 59: FrenchKit 2017: Server(less) Swift

$ wsk api create /hello get hello —response-type json

59

Page 60: FrenchKit 2017: Server(less) Swift

$ wsk api create /hello get hello —response-type json

60

Create API

Page 61: FrenchKit 2017: Server(less) Swift

$ wsk api create /hello get hello —response-type json

61

Create API

REST Route Method

Page 62: FrenchKit 2017: Server(less) Swift

$ wsk api create /hello get hello —response-type json

62

Create API

REST Route Method From

Action

Page 63: FrenchKit 2017: Server(less) Swift

$ wsk api create /hello get hello —response-type json

ok: created API /hello GET for action /_/hello https://${APIHOST}:9001/api/21ef035/hello

63

Create API

REST Route Method From

Action

Page 64: FrenchKit 2017: Server(less) Swift

$ wsk api create /hello get hello —response-type json

ok: created API /hello GET for action /_/hello https://${APIHOST}:9001/api/21ef035/hello

$ curl https://${APIHOST}:9001/api/21ef035/hello?name=World

64

Create API

REST Route Method From

Action

Page 65: FrenchKit 2017: Server(less) Swift

65

Page 66: FrenchKit 2017: Server(less) Swift

66

• Mobile client SDK for iOS and watchOS.

• Fire remote triggers and invoke remote actions.

• Swift 3, iOS9+ and watchOS 3.

• Install by using CocoaPods, Carthage, Swift Package Manager, or from the source directory.

https://github.com/apache/incubator-openwhisk-client-swift

Page 67: FrenchKit 2017: Server(less) Swift

var params = [String: String]() params["payload"] = "Hello"

67

Page 68: FrenchKit 2017: Server(less) Swift

var params = [String: String]() params["payload"] = "Hello"

do { try whisk.invokeAction(name: "hello", parameters: params, hasResult: true, callback: {(reply, error) -> Void in

}) } catch { print("Error \(error)") }

68

Page 69: FrenchKit 2017: Server(less) Swift

var params = [String: String]() params["payload"] = "Hello"

do { try whisk.invokeAction(name: "hello", parameters: params, hasResult: true, callback: {(reply, error) -> Void in if let error = error { print("Error invoking action \(error.localizedDescription)") } else { var result = reply["result"] print("Got result \(result)") } }) } catch { print("Error \(error)") }

69

Page 70: FrenchKit 2017: Server(less) Swift

70

Weather Gods

Page 71: FrenchKit 2017: Server(less) Swift

Weather Gods

• Premium app from Meyume Ltd.

• Offers a rich interactive experience unlike other weather apps.

• Sources high quality weather data from multiple providers including The Weather Company.

• Provides weather notifications: • type (rain, snow etc.) • schedule (evening, morning, or live) • options (e.g. only notify about heavy

rain).

71

Page 72: FrenchKit 2017: Server(less) Swift

72

3-minute alarm

Group scanner

Weather collector

Wind scanner

Rain scanner

Fog scanner

Alarm trigger

Alarm trigger

Alarm trigger

Send push

Send push

Send push

Push notification

service

Page 73: FrenchKit 2017: Server(less) Swift

73

BluePic

Page 74: FrenchKit 2017: Server(less) Swift

74

BluePic

• Sample app from IBM • iOS and Web front ends

• Provides photo sharing

• Adds image recognition and weather data to the photo

• Provides notifications to users • Social sharing • Updates to data

https://github.com/IBM/BluePic

Page 75: FrenchKit 2017: Server(less) Swift

75

PUBLIC NETWORK CLOUD NETWORK

iOS APP

WEB APP

Page 76: FrenchKit 2017: Server(less) Swift

76

PUBLIC NETWORK CLOUD NETWORK

iOS APP

WEB APP

Page 77: FrenchKit 2017: Server(less) Swift

77

PUBLIC NETWORK CLOUD NETWORK

iOS APP

WEB APP

Page 78: FrenchKit 2017: Server(less) Swift

78

PUBLIC NETWORK CLOUD NETWORK

iOS APP

WEB APP

Page 79: FrenchKit 2017: Server(less) Swift

79

PUBLIC NETWORK CLOUD NETWORK

iOS APP

WEB APP

Page 80: FrenchKit 2017: Server(less) Swift

80

PUBLIC NETWORK CLOUD NETWORK

iOS APP

WEB APP

Page 81: FrenchKit 2017: Server(less) Swift

81

PUBLIC NETWORK CLOUD NETWORK

iOS APP

WEB APP

Page 82: FrenchKit 2017: Server(less) Swift

82

PUBLIC NETWORK CLOUD NETWORK

iOS APP

WEB APP

Page 83: FrenchKit 2017: Server(less) Swift

83

Decreasing concern (and control) over stack implementation

Incr

easi

ng fo

cus

on b

usin

ess

logi

c

Page 84: FrenchKit 2017: Server(less) Swift

84

Decreasing concern (and control) over stack implementation

Incr

easi

ng fo

cus

on b

usin

ess

logi

c

Server

Serverless