最經(jīng)幾天一直在拜讀Vapor中service的源碼葫录,連續(xù)幾天上班途中和下班之后都是在翻看源碼帐萎,從剛開始感覺云山霧繞到逐漸清晰
而最終內(nèi)部的所有被注冊的server都會通過下面來進(jìn)行實(shí)例化注冊到系統(tǒng)中來使用
看了這么久我們根據(jù)最新發(fā)送郵件的需求自己寫個service拉庵,在此我們通過Provider來實(shí)現(xiàn)IBM swift SMTP
在此我們分為SKSmtpProvider和SKSmtpConfig兩部分,其中SKSmtpConfig為SMTP的信息配置項(xiàng)而SKSmtpProvider為真實(shí)的Server實(shí)現(xiàn)部分
先上Provider的代碼
class SKSmtpProvider: Provider {
func register(_ services: inout Services) throws {
services.register { (container) -> SKSmtp in
let config = try container.make(SKSmtpConfig.self)
assert(config != nil, "選注冊SKSmtpConfig實(shí)例帆离,才能使用SKSmtp")
return SKSmtp.init(config: config)
}
}
func didBoot(_ container: Container) throws -> EventLoopFuture<Void> {
let result = container.eventLoop.newPromise(Void.self)
result.succeed()
return result.futureResult
}
}
對于使用Provider來實(shí)現(xiàn)service則必須實(shí)現(xiàn)Provider 協(xié)議
然后是我們SMTP的server實(shí)現(xiàn),對于service協(xié)議渗蟹,就只是一個類型聲明:
public protocol Service {}
class SKSmtp : Service {
fileprivate var smtp: SMTP?
init(config: SKSmtpConfig) {
self.smtp = SMTP.init(hostname: config.hostname, email: config.email, password: config.password, port: config.port, useTLS: config.useTLS, tlsConfiguration: config.tlsConfiguration, authMethods:config.authMethods, domainName: config.domainName, timeout: config.timeout)
}
/// Send an email.
///
/// - Parameters:
/// - mail: `Mail` object to send.
/// - completion: Callback when sending finishes. `Error` is nil on success. (optional)
public func send(_ mail: Mail, completion: ((Error?) -> Void)? = nil) {
smtp?.send(mail, completion: completion)
}
public func send(_ mails: [Mail],
progress: Progress = nil,
completion: Completion = nil) {
smtp?.send(mails, progress: progress, completion: completion)
}
}
最后是Config,很簡單Config其實(shí)就是SMTP的配置信息
public struct SKSmtpConfig : Service{
var hostname: String
var email: String
var password: String
var port: Int32 = 465
var useTLS: Bool = false
var tlsConfiguration: SwiftSMTP.TLSConfiguration?
var authMethods:[ AuthMethod] = []
var domainName: String = "localhost"
var timeout: UInt = 10
init(hostname: String, email: String, password: String, port: Int32 = 465, useTLS:Bool = false,tlsConfiguration:SwiftSMTP.TLSConfiguration? = nil, authMethods:[ AuthMethod] = [], domainName: String = "localhost", timeout: UInt = 10) {
self.hostname = hostname
self.email = email
self.password = password
self.port = port
self.useTLS = useTLS
self.tlsConfiguration = tlsConfiguration
self.domainName = domainName
self.timeout = timeout
}
}
所有的都已經(jīng)實(shí)現(xiàn)了拌滋,接下來就是使用了
1 系統(tǒng)的configure中注冊對應(yīng)的Provider
let smtpConfig = SKSmtpConfig.init(hostname: "", email: "", password: "")
services.register(smtpConfig)
try services.register(SKSmtpProvider())
2 在網(wǎng)絡(luò)請求中實(shí)現(xiàn)
public func regist(req: Request)throws-> EventLoopFuture<String>{
let smtp: SKSmtp = try req.make(SKSmtp.self)
smtp.send(<#T##mail: Mail##Mail#>, completion: <#T##((Error?) -> Void)?##((Error?) -> Void)?##(Error?) -> Void#>)
Vapor集成使用
.package(url: "https://github.com/skeyboy/SKSmtp.git", from:"0.0.1")