Skip to content

Commit

Permalink
Merge pull request #208 from zapcannon87/master
Browse files Browse the repository at this point in the history
feat: support custom server
  • Loading branch information
zapcannon87 authored Jun 21, 2019
2 parents 7c271bf + 30d7392 commit acba07e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion LeanCloud.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'LeanCloud'
s.version = '16.0.0'
s.version = '16.1.0'
s.license = { :type => 'Apache License, Version 2.0', :file => 'LICENSE' }
s.summary = 'LeanCloud Swift SDK'
s.homepage = 'https://leancloud.cn/'
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
* [x] Short Message
* [x] File Hosting
* [x] Push Notification

## Wanted Features
* [ ] Search Query
* [ ] Instant Messaging
* [ ] Your good idea we are looking forward to :)
* [x] Instant Messaging

## Communication
* If you **have some advice**, open an issue.
Expand Down
38 changes: 38 additions & 0 deletions Sources/Storage/Foundation/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public class LCApplication {
/// Application Configuration.
public struct Configuration {

public let customizedServers: [ServerCustomizableModule]

/// HTTP Request Timeout Interval, default is 60.0 second.
public let HTTPRequestTimeoutInterval: TimeInterval

Expand All @@ -106,17 +108,39 @@ public class LCApplication {
public static let `default` = Configuration()

public init(
customizedServers: [ServerCustomizableModule] = [],
HTTPRequestTimeoutInterval: TimeInterval = 60.0,
RTMConnectingTimeoutInterval: TimeInterval = 15.0,
RTMCommandTimeoutInterval: TimeInterval = 30.0,
RTMCustomServerURL: URL? = nil)
{
self.customizedServers = customizedServers
self.HTTPRequestTimeoutInterval = HTTPRequestTimeoutInterval
self.RTMConnectingTimeoutInterval = RTMConnectingTimeoutInterval
self.RTMCommandTimeoutInterval = RTMCommandTimeoutInterval
self.RTMCustomServerURL = RTMCustomServerURL
}
}

public enum ServerCustomizableModule {
case api(_ host: String)
case push(_ host: String)
case engine(_ host: String)
case rtm(_ host: String)

var moduleKeyAndHost: (key: String, host: String) {
switch self {
case .api(let host):
return (HTTPRouter.Module.api.key, host)
case .engine(let host):
return (HTTPRouter.Module.engine.key, host)
case .push(let host):
return (HTTPRouter.Module.push.key, host)
case .rtm(let host):
return (HTTPRouter.Module.rtm.key, host)
}
}
}

/// Application ID.
public private(set) var id: String!
Expand Down Expand Up @@ -245,6 +269,20 @@ public class LCApplication {
application: self,
configuration: .default
)

Logger.shared.debug(
"""
\n
------ LCApplication Initializing Infomation
LCApplication with ID<\"\(self.id!)\"> did initialize success.
The Configuration of this Application is \(configuration).
------ END
"""
)
}

}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Storage/Foundation/LeanCloud.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

public let version = "16.0.0"
public let version = "16.1.0"

/// `version` is a common word, so use `__LeanCloudVersion` to wrap it to avoid conflict.
var __LeanCloudVersion: String {
Expand Down
14 changes: 14 additions & 0 deletions Sources/Storage/HTTPRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class HTTPRouter {

init(application: LCApplication, configuration: Configuration) {
self.application = application

var _customizedServerTable: [String: String] = [:]
application.configuration.customizedServers.forEach { (item) in
let tuple = item.moduleKeyAndHost
_customizedServerTable[tuple.key] = tuple.host
}
self.customizedHostTable = _customizedServerTable

self.configuration = configuration
if let localStorageContext = application.localStorageContext {
do {
Expand Down Expand Up @@ -100,6 +108,8 @@ class HTTPRouter {
"statistics": .stats,
"always_collect": .stats
]

private let customizedHostTable: [String: String]

/**
Get module of path.
Expand Down Expand Up @@ -366,6 +376,10 @@ class HTTPRouter {
func route(path: String, module: Module? = nil) -> URL? {
let module = module ?? findModule(path: path)
let fullPath = versionizedPath(path, module: module)

if let host = self.customizedHostTable[module.key] {
return absoluteUrl(host: host, path: fullPath)
}

if let url = cachedUrl(path: fullPath, module: module) {
return url
Expand Down

0 comments on commit acba07e

Please sign in to comment.