Avatar
Kishikawa Katsumi 10/6/2020 6:46 AM
app.get("*") { req -> EventLoopFuture<EventLoopFuture<View>> in let pattern = try! NSRegularExpression(pattern: #"^\/([a-f0-9]{32})$"#, options: [.caseInsensitive]) let matches = pattern.matches(in: req.url.path, options: [], range: NSRange(location: 0, length: NSString(string: req.url.path).length)) guard matches.count == 1 && matches[0].numberOfRanges == 2 else { throw Abort(.notFound) } let gistId = NSString(string: req.url.path).substring(with: matches[0].range(at: 1)) let promise = req.eventLoop.makePromise(of: EventLoopFuture<View>.self) let session = URLSession(configuration: .default) let request = URLRequest(url: URL(string: "https://api.github.com/gists/\(gistId)")!) session.dataTask(with: request) { (data, response, error) in if let error = error { promise.fail(error) return } guard let data = data else { promise.fail(Abort(.notFound)) return } guard let contents = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], let files = contents["files"] as? [String: Any], let filename = files.keys.first, let file = files[filename] as? [String: Any], let content = file["content"] as? String else { promise.fail(Abort(.notFound)) return } promise.succeed( req.view.render( "index", [ "title": "Swift AST Explorer", "defaultSampleCode": content, "swiftVersion": swiftVersion, ] ) ) } .resume() return promise.futureResult } VaporでAsyncでビューを返すのってこれであってます?動いてるんですけど EventLoopFuture<EventLoopFuture<View>> となってるところがホントに合ってるのかな?と。