Personality Recent Works ɾLead Developer at iDEAKITT inc. ɾBackend/Data Analysis at SmartDrive inc. Programming Language: Node.js, Python, Ruby and Swift I have a radio program that named MikeTOKYO!!!
Covers from data/networking layer to web application layer (Nest is just Web Server Gateway Interface) ▸ Started from Zewo guys ▸ No Foundation ▸ Anyway Growing up day by day (Hard to follow…(*´Дʆ)) OPEN SWIFT IS
and Error ▸ Connection ▸ Time: timespec/mach_timebase wrapper ▸ URI: URI Schema struct ▸ And also has their Asynchronous apis C7 Core standards for Swift
throws -> Response) -> Void) { Redis.command(con, command: .PING) { result in if case .Success(let rep) = result { result { Responder(body: rep) } } } } } app.get("/foo", FooResponder()) 2. Routing with S4. AsyncResponder Slimane has 2 ways for routing
// Handy is also standard middleware for Slimane app.use { req, res, next in print("[\(Time())] \(req.path ?? "/")") next(.Chain(req, res)) } 1. Handy Middleware
String public init(root: String){ self.root = root } public func respond(req: Request, res: Response, next: MiddlewareChain) { guard let path = req.path , ext = path.split(by: “.").last else { return next(.Chain(req, res)) } guard mediaType = mediaTypeForFileExtension(ext) else { return next(.Chain(req, res)) } FS.readFile(root + path) { switch($0) { case .Success(let buffer): var res = res res.contentType = mediaType res.body = .buffer(buffer.data) next(.Chain(req, res)) case .Error(_): next(.Error(Error.ResourceNotFound("\(path) is not found"))) } } } } } Intercept Response at the middleware Pass the buffer to the response.body and call chain next, then the response is intercepted and respond soon.
Initialize Redis store let redisStore = try! RedisStore(host: "127.0.0.1", port: 6379) // SessionConfig let sesConf = SessionConfig( store: redisStore, secret: "my-secret-value", expires: 180, HTTPOnly: true ) // Enable to use session in Slimane app.use(SessionMiddleware(conf: sesConf)) app.get("/") { req, responder // set data into the session req.session![“foo”] = "bar" req.session!.id // show session id responder { Response() } } Register SessionMiddleware into the middleware chains then session is enabled
Actually Reactify is not an existence module. just my imagine. import Reactify public struct ReactViewEngine: Renderable { public var fileExtension = "jsx" public let templateData: [String: AnyObject] public init(templateData: [String: AnyObject]){ self.templateData = templateData } // Should implement render method public func render(path: String, result: (Void throws -> Data) -> Void) { let event = Reactify(path: "\(path).\(fileExtension)", templateData: templateData) event.on(.End) { result { return $0 } } event.on(.Error) { result { throw $0 } } } }
cores. // Check current process is master? if Cluster.isMaster { // Launch the worker processes the number of cores for _ in 0..<OS.cpuCount { try! Cluster.fork(silent: false) } try! Slimane().listen() } else { // on workers let app = Slimane() app.use("/") { req, responder in responder { Response(body: "Hello! I'm \(Process.pid)") } } try! app.listen() }
each other let worker = try! Cluster.fork(silent: false) // Send message to the worker worker.send(.Message("message from master")) // Receive event from the worker worker.on { event in if case .Message(let str) = event { print(str) } else if case .Online = event { print("Worker: \(worker.id) is online") } else if case .Exit(let status) = event { print("Worker: \(worker.id) is dead. status: \(status)") } } // Receive event from the master Process.on { event in if case .Message(let str) = event { print(str) } } // Send message to the master Process.send(.Message("Hey!")) On the Master Process On the Worker Process
It enables you to execute the synchronous i/o in the separated pooled thread, and the notification will be emitted to the main loop when the task is finished. var rows: [User] = [] var err: ErrorProtocol? = nil let onThread = { let nameParam: String = "some one" let ids: [QueryParameter] = [1, 2, 3, 4, 5, 6] let optional:Int? = nil let params: (Int, Int?, String, QueryArray) = ( 50, optional, nameParam, QueryArray(ids) ) do { rows: [User] = try conn.query( "SELECT id,name,created_at,age FROM users WHERE (age > ? OR age is ?) OR name = ? OR id IN (?)", build(params) ) } catch { err = error } } let onFinish = { if let error = err { print(error) return } print(rows) } Process.qwork(onThread: onThread, onFinish: onFinish)
VAPOR ɾ Command: ab -n 10000 -c 100 ɾServer: EC2 t2.medium(vCPU: 2, Mem: 4) ɾSwift-Version: swift-DEVELOPMENT-SNAPSHOT-2016-03-24-a-ubuntu14.04 Performance testing I also tried to build Zewo, Swifton and Kitura but that are failed to build…