Skip to content

Commit acba07e

Browse files
authored
Merge pull request #208 from zapcannon87/master
feat: support custom server
2 parents 7c271bf + 30d7392 commit acba07e

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

LeanCloud.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'LeanCloud'
3-
s.version = '16.0.0'
3+
s.version = '16.1.0'
44
s.license = { :type => 'Apache License, Version 2.0', :file => 'LICENSE' }
55
s.summary = 'LeanCloud Swift SDK'
66
s.homepage = 'https://leancloud.cn/'

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
* [x] Short Message
1111
* [x] File Hosting
1212
* [x] Push Notification
13-
14-
## Wanted Features
15-
* [ ] Search Query
16-
* [ ] Instant Messaging
17-
* [ ] Your good idea we are looking forward to :)
13+
* [x] Instant Messaging
1814

1915
## Communication
2016
* If you **have some advice**, open an issue.

Sources/Storage/Foundation/Application.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public class LCApplication {
9191
/// Application Configuration.
9292
public struct Configuration {
9393

94+
public let customizedServers: [ServerCustomizableModule]
95+
9496
/// HTTP Request Timeout Interval, default is 60.0 second.
9597
public let HTTPRequestTimeoutInterval: TimeInterval
9698

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

108110
public init(
111+
customizedServers: [ServerCustomizableModule] = [],
109112
HTTPRequestTimeoutInterval: TimeInterval = 60.0,
110113
RTMConnectingTimeoutInterval: TimeInterval = 15.0,
111114
RTMCommandTimeoutInterval: TimeInterval = 30.0,
112115
RTMCustomServerURL: URL? = nil)
113116
{
117+
self.customizedServers = customizedServers
114118
self.HTTPRequestTimeoutInterval = HTTPRequestTimeoutInterval
115119
self.RTMConnectingTimeoutInterval = RTMConnectingTimeoutInterval
116120
self.RTMCommandTimeoutInterval = RTMCommandTimeoutInterval
117121
self.RTMCustomServerURL = RTMCustomServerURL
118122
}
119123
}
124+
125+
public enum ServerCustomizableModule {
126+
case api(_ host: String)
127+
case push(_ host: String)
128+
case engine(_ host: String)
129+
case rtm(_ host: String)
130+
131+
var moduleKeyAndHost: (key: String, host: String) {
132+
switch self {
133+
case .api(let host):
134+
return (HTTPRouter.Module.api.key, host)
135+
case .engine(let host):
136+
return (HTTPRouter.Module.engine.key, host)
137+
case .push(let host):
138+
return (HTTPRouter.Module.push.key, host)
139+
case .rtm(let host):
140+
return (HTTPRouter.Module.rtm.key, host)
141+
}
142+
}
143+
}
120144

121145
/// Application ID.
122146
public private(set) var id: String!
@@ -245,6 +269,20 @@ public class LCApplication {
245269
application: self,
246270
configuration: .default
247271
)
272+
273+
Logger.shared.debug(
274+
"""
275+
\n
276+
------ LCApplication Initializing Infomation
277+
278+
LCApplication with ID<\"\(self.id!)\"> did initialize success.
279+
280+
The Configuration of this Application is \(configuration).
281+
282+
------ END
283+
284+
"""
285+
)
248286
}
249287

250288
}

Sources/Storage/Foundation/LeanCloud.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import Foundation
1010

11-
public let version = "16.0.0"
11+
public let version = "16.1.0"
1212

1313
/// `version` is a common word, so use `__LeanCloudVersion` to wrap it to avoid conflict.
1414
var __LeanCloudVersion: String {

Sources/Storage/HTTPRouter.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ class HTTPRouter {
6161

6262
init(application: LCApplication, configuration: Configuration) {
6363
self.application = application
64+
65+
var _customizedServerTable: [String: String] = [:]
66+
application.configuration.customizedServers.forEach { (item) in
67+
let tuple = item.moduleKeyAndHost
68+
_customizedServerTable[tuple.key] = tuple.host
69+
}
70+
self.customizedHostTable = _customizedServerTable
71+
6472
self.configuration = configuration
6573
if let localStorageContext = application.localStorageContext {
6674
do {
@@ -100,6 +108,8 @@ class HTTPRouter {
100108
"statistics": .stats,
101109
"always_collect": .stats
102110
]
111+
112+
private let customizedHostTable: [String: String]
103113

104114
/**
105115
Get module of path.
@@ -366,6 +376,10 @@ class HTTPRouter {
366376
func route(path: String, module: Module? = nil) -> URL? {
367377
let module = module ?? findModule(path: path)
368378
let fullPath = versionizedPath(path, module: module)
379+
380+
if let host = self.customizedHostTable[module.key] {
381+
return absoluteUrl(host: host, path: fullPath)
382+
}
369383

370384
if let url = cachedUrl(path: fullPath, module: module) {
371385
return url

0 commit comments

Comments
 (0)