Skip to content

Commit

Permalink
Merge pull request #14 from dokun1/caching-and-cors
Browse files Browse the repository at this point in the history
Caching and cors
  • Loading branch information
dokun1 authored Jun 9, 2018
2 parents abd5936 + b30262a commit a43f0cc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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" ])
]
Expand Down
5 changes: 5 additions & 0 deletions Sources/Application/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions Sources/Application/Models/SlackTeam.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions Sources/Application/Routes/SlackRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion Sources/slackin-swift/SlackArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a43f0cc

Please sign in to comment.