From 9710e9c7e78cb7ec33eb18b5c55575db9ba6561e Mon Sep 17 00:00:00 2001 From: David Okun Date: Sat, 9 Jun 2018 05:29:18 -0700 Subject: [PATCH 1/3] Caching team in memory for user count call --- Package.swift | 3 ++- Sources/Application/Application.swift | 5 +++++ Sources/Application/Models/SlackTeam.swift | 5 +++++ Sources/Application/Routes/SlackRoutes.swift | 3 +++ Sources/Application/Routes/WebClientRoutes.swift | 1 + 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 5268b09..3977466 100644 --- a/Package.swift +++ b/Package.swift @@ -9,12 +9,13 @@ let package = Package( .package(url: "https://github.com/IBM-Swift/CloudEnvironment.git", from: "6.1.0"), .package(url: "https://github.com/RuntimeTools/SwiftMetrics.git", from: "2.0.0"), .package(url: "https://github.com/IBM-Swift/Health.git", from: "1.0.0"), + .package(url: "https://github.com/IBM-Swift/Kitura-CORS.git", from: "2.1.0"), .package(url: "https://github.com/IBM-Swift/Kitura-StencilTemplateEngine.git", .upToNextMinor(from: "1.10.0")), .package(url: "https://github.com/IBM-Swift/Kitura-OpenAPI.git", .upToNextMinor(from: "1.0.1")) ], targets: [ .target(name: "slackin-swift", dependencies: [ .target(name: "Application"), "Kitura" , "HeliumLogger"]), - .target(name: "Application", dependencies: [ "Kitura", "CloudEnvironment","SwiftMetrics","Health", "KituraStencil", "KituraOpenAPI"]), + .target(name: "Application", dependencies: [ "Kitura", "CloudEnvironment","SwiftMetrics","Health", "KituraStencil", "KituraOpenAPI", "KituraCORS"]), .testTarget(name: "ApplicationTests" , dependencies: [.target(name: "Application"), "Kitura","HeliumLogger" ]) ] diff --git a/Sources/Application/Application.swift b/Sources/Application/Application.swift index 0468a5a..081ebb5 100644 --- a/Sources/Application/Application.swift +++ b/Sources/Application/Application.swift @@ -6,12 +6,14 @@ import CloudEnvironment import KituraContracts import Health import KituraOpenAPI +import KituraCORS public let projectPath = ConfigurationManager.BasePath.project.path public let health = Health() public class App { var token: String? + static var slackTeam: SlackTeam? let router = Router() let cloudEnv = CloudEnv() @@ -22,6 +24,9 @@ public class App { func postInit() throws { // Endpoints + let options = Options(allowedOrigin: .all) + let cors = CORS(options: options) + router.all("/*", middleware: cors) initializeHealthRoutes(app: self) initializeSlackRoutes(app: self) initializeWebClientRoutes(app: self) diff --git a/Sources/Application/Models/SlackTeam.swift b/Sources/Application/Models/SlackTeam.swift index 00349bb..892f602 100644 --- a/Sources/Application/Models/SlackTeam.swift +++ b/Sources/Application/Models/SlackTeam.swift @@ -29,6 +29,10 @@ struct SlackTeam: Codable { static func getInfo(token: String, completion: @escaping (_ team: SlackTeam?, _ error: Error?) -> Void) { Log.verbose("Requesting team info") + Log.verbose("Checking cache first") + if let cachedTeam = App.slackTeam { + return completion(cachedTeam, nil) + } let url = "https://slack.com/api/team.info?token=\(token)" Log.verbose("Attempting to retrieve team information from url: \(url)") let request = RestRequest(method: .get, url: url, containsSelfSignedCert: false) @@ -39,6 +43,7 @@ struct SlackTeam: Codable { Log.error("Slack API error from team request: \(responseError)") completion(nil, SlackResponseError.notAllowed) } else { + App.slackTeam = slackResponse.team completion(slackResponse.team, nil) } case .failure(let error): diff --git a/Sources/Application/Routes/SlackRoutes.swift b/Sources/Application/Routes/SlackRoutes.swift index 2ac844c..ccd8276 100644 --- a/Sources/Application/Routes/SlackRoutes.swift +++ b/Sources/Application/Routes/SlackRoutes.swift @@ -87,6 +87,9 @@ private func channelHandler(completion: @escaping ([SlackChannel]?, RequestError } private func teamHandler(completion: @escaping (SlackTeam?, RequestError?) -> Void) { + if let cachedTeam = App.slackTeam { + return completion(cachedTeam, nil) + } guard let token = requestToken else { Log.error("Error processing team request - No token found") completion(nil, RequestError.unauthorized) diff --git a/Sources/Application/Routes/WebClientRoutes.swift b/Sources/Application/Routes/WebClientRoutes.swift index 4f70fb2..16d8918 100644 --- a/Sources/Application/Routes/WebClientRoutes.swift +++ b/Sources/Application/Routes/WebClientRoutes.swift @@ -12,6 +12,7 @@ import Kitura import LoggerAPI private var requestToken: String? +private var app: App? struct SlackWebContextError: Codable { var message: String From 8da43909e4ce79622eded10871681a356e608099 Mon Sep 17 00:00:00 2001 From: David Okun Date: Sat, 9 Jun 2018 08:40:40 -0700 Subject: [PATCH 2/3] Retrofitting debug token method to use existing method --- Sources/slackin-swift/SlackArguments.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/slackin-swift/SlackArguments.swift b/Sources/slackin-swift/SlackArguments.swift index ad91989..4d22dfd 100644 --- a/Sources/slackin-swift/SlackArguments.swift +++ b/Sources/slackin-swift/SlackArguments.swift @@ -36,7 +36,10 @@ class SlackArguments { if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { let fileURL = dir.appendingPathComponent("slackkey.txt") do { - let token = try String(contentsOf: fileURL, encoding: .utf8).trimmingCharacters(in: .newlines) + let tokenArg = try String(contentsOf: fileURL, encoding: .utf8).trimmingCharacters(in: .newlines) + guard let token = extractToken(from: [tokenArg]) else { + throw SlackError.noToken + } return token } catch { throw SlackError.noToken From b30262a06f3965bc3129f606deeda7e3f7e9d180 Mon Sep 17 00:00:00 2001 From: David Okun Date: Sat, 9 Jun 2018 09:14:04 -0700 Subject: [PATCH 3/3] Removed circular reference to app --- Sources/Application/Routes/WebClientRoutes.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/Application/Routes/WebClientRoutes.swift b/Sources/Application/Routes/WebClientRoutes.swift index 16d8918..4f70fb2 100644 --- a/Sources/Application/Routes/WebClientRoutes.swift +++ b/Sources/Application/Routes/WebClientRoutes.swift @@ -12,7 +12,6 @@ import Kitura import LoggerAPI private var requestToken: String? -private var app: App? struct SlackWebContextError: Codable { var message: String