Slide 1

Slide 1 text

B U I L D I N G P R A C T I C A L W E B A P P L I C AT I O N W I T H S L I M A N E 2 0 1 6 M A Y 3 1 T O K Y O S E R V E R S I D E S W I F T M E E T U P Y U K I TA K E I

Slide 2

Slide 2 text

Hello! I’m Yuki Takei

Slide 3

Slide 3 text

ChatCast

Slide 4

Slide 4 text

SmartDrive

Slide 5

Slide 5 text

ຊ೔ͷൃද͸ɺ్தͰσϞΛ͸͞Έ·͢ɻ ࣗ෼΋खΛಈ͔͠ͳΒσϞΛମݧ͍ͨ͠ͱ͍͏͜ͱ͸ɺ https://github.com/noppoMan/slimane-github-issue-manager Λgit cloneͯ͠vagrant upΛ༧Ί͓͍͍ͯͯͩ͘͠͞ɻ

Slide 6

Slide 6 text

I have glad news

Slide 7

Slide 7 text

Finally My PRs are merged to Open-Swift!

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

W H AT I S O P E N - S W I F T ? • C7: Core standards for Swift (Data, Stream etc…) • S4: HTTP standards for Swift (Request/Response etc..) • D5: Database standards for Swift. Open source cross project standards for Swift.

Slide 11

Slide 11 text

Anyways

Slide 12

Slide 12 text

How do you make Web Applications?

Slide 13

Slide 13 text

With Rails, Node.js, Django and Play etc..?

Slide 14

Slide 14 text

Today, I propose you to build it with Swift

Slide 15

Slide 15 text

Building Practical Web Application with Swift? Seriously?

Slide 16

Slide 16 text

Yes We can!

Slide 17

Slide 17 text

W H AT S O F T WA R E S A R E N E E D E D T O B U I L D I T ? • HTTP Servers • Application Frameworks • Template Engines • Database Clients • Deployment

Slide 18

Slide 18 text

Slimane Provides all of them!

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

S L I M A N E I S … • Express inspired Web Application Framework • 100% asynchronous with event loop backend (libuv) • Standalone HTTP Server • Faster • Moduler • Full managed asynchronous Stream, TCP, Pipe, FileSystem, Process, Timer etc.. • Adopt Open-Swift

Slide 21

Slide 21 text

Today I show you how to create the Issue Manager for Github made with Slimane

Slide 22

Slide 22 text

S P E C I F I C AT I O N • List issues • Get a single issue • Create issues • Post comments • Broadcast the comment to all clients via Web socket

Slide 23

Slide 23 text

B A C K E N D S T R U C T U R E S pub/sub pub/sub reverse proxy app1 app2 github api ɾOS: Ubuntu(Vagrant) ɾCore: 4 ɾRAM: 2048MB

Slide 24

Slide 24 text

F R O N T E N D • Template Engine: Mustache/lodash • JS Framework: jQuery • Css Framework: Bootstrap

Slide 25

Slide 25 text

Demo https://github.com/noppoMan/slimane-github-issue-manager

Slide 26

Slide 26 text

ɾvagrant up ɾlaunch single app ɾlogin ɾplay around(list issues, create an issue) ɾlaunch cluster app ɾweb socket ɾGraceful restart(Not implemented yet..) Menu

Slide 27

Slide 27 text

Let's take a closer look the codes!

Slide 28

Slide 28 text

• Get a Slimane server up • HTML Rendering • Deal with Github Api • Authentication • Web socket with Redis pub/sub • Asynchronous flow controll • Cluster/Worker • Graceful Restart(Not implemented yet…) Basic Technical Part of Github Issue Manager

Slide 29

Slide 29 text

Get a Slimane server up import Slimane import JSON let app = Slimane() // access log app.use { req, res, next in print("[pid:\(Process.pid)]\t\(Time())\t\(req.path ?? "/")") next(.Chain(req, res)) } // Responsd to the simple json app.get("/") { req, responder in let json: JSON = ["foo": "bar"] responder { Response(body: JSONSerializer().serialize(json: json)) } } try! app.listen()

Slide 30

Slide 30 text

HTML Rendering // app.swift import Slimane import MustacheViewEngine import Render extension Response { static func render(_ path: String, data: TemplateData = ["foo": "bar"]) -> Response { let render = Render(engine: MustacheViewEngine(templateData: data), path: path) return Response(custom: render) } } let app = Slimane() app.get("/") { req, responder in responder { Response.render("index", data: ["foo": "bar"]) } } try! app.listen() // index.mustache

{{ foo }}

Slide 31

Slide 31 text

Deal with Github API Actually, We don’t have any SSL implementation yet…

Slide 32

Slide 32 text

But We have….. libcurl! Deal with Github API

Slide 33

Slide 33 text

Deal with Github API https://github.com/noppoMan/slimane-github-issue- manager/blob/master/Sources/RestClient.swift

Slide 34

Slide 34 text

Deal with Github API import Thrush import QWFuture struct CurlContext { let writeCallback: ([Byte]) -> () } return Promise { resolve, reject in let future = QWFuture(loop: self.loop) { (completion: (() throws -> Data) -> ()) in let handle = curl_easy_init() curlHelperSetOptString(handle, CURLOPT_URL, UnsafeMutablePointer(self.uri.buffer)) curlHelperSetOptBool(handle, CURLOPT_HTTPGET, CURL_TRUE) .................. var data = Data() let writeCallback = { (bytes: [Byte]) in data.append(contentsOf: bytes) } curlHelperSetOptWriteFunc(handle, context) { (buf, size, nMemb, privateData) -> Int in let ctx = UnsafePointer(privateData) let segsize = size * nMemb var bytes = [Byte]() for i in stride(from: 0, to: segsize, by: 1) { bytes.append(Byte(bitPattern: buf![i])) } ctx?.pointee?.writeCallback(bytes) return segsize } curl_easy_perform(handle) curl_easy_cleanup(handle) completion { data } } future.onSuccess { resolve($0) } future.onFailure { reject($0) }

Slide 35

Slide 35 text

libcurl + Future(uv_queue_work) + Promise Deal with Github API

Slide 36

Slide 36 text

Deal with Github API Usage RestClient( method: .get, uri: "https://api.github.com/repos/noppoMan/Slimane/issues", body: JSONSerializer().serialize(json: ["body": data["body"]!.string!]) ) .send() .then { response in if 200..<300 ~= response.statusCode { success() } else { error() } } .failure { error in print(error) }

Slide 37

Slide 37 text

Authentication https://github.com/noppoMan/slimane-github-issue- manager/blob/master/Sources/ AuthenticationMiddleware.swift

Slide 38

Slide 38 text

WebSocket with Redis pub/sub https://github.com/noppoMan/slimane-github-issue- manager/blob/master/Sources/ChatRoute.swift

Slide 39

Slide 39 text

Asynchronous flow controll ɾnoopoman/Thrush ɾslimane-swift/QWFuture

Slide 40

Slide 40 text

Asynchronous flow controll https://github.com/noppoMan/Thrush - Promise-

Slide 41

Slide 41 text

Asynchronous flow controll https://github.com/slimane-swift/QWFuture - QWFuture -

Slide 42

Slide 42 text

Cluster and Worker https://github.com/noppoMan/slimane-github-issue- manager

Slide 43

Slide 43 text

Thanks!