我正在尝试在swift中创建一个REST API,我有一个代码示例,我无法完全理解。
import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
let server = HTTPServer()
server.serverPort = 8080
server.documentRoot = "webroot"
private func getClientRoutes()->Routes{
var routes = Routes()
routes.add(method: .post, uri: "/client/register", handler:httpClientHandler.registerClientHandler)
routes.add(method: .post, uri: "/client/activity", handler:httpClientHandler.registerActivityClientHandler)
routes.add(method: .get, uri: "/client/getVPNConfig", handler:clientGetVpnConfigHandler)
return routes
}
public func registerClientHandler(request: HTTPRequest, _ response: HTTPResponse) {
response.setHeader(.contentType, value: "application/json")
var clientid: String?
if let bodyJson = request.postBodyString{
do {
let incoming = try bodyJson.jsonDecode() as! [String: String]
if let i = incoming["clientid"]{
clientid = i
}
} catch {
}
}
if let clientid = clientid{
if let vpnConfigForClientDTO = clientController.registerClient(clientId: clientid){
try! response.appendBody(string: vpnConfigForClientDTO.jsonEncodedString())
}else{
try! response.appendBody(string: "VPN ERROR".jsonEncodedString())
response.status = HTTPResponseStatus.badRequest
}
}else{
try! response.appendBody(string: "Lacking clientid parameter".jsonEncodedString())
response.status = HTTPResponseStatus.badRequest
}
response.completed()
}
do {
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: \(err) \(msg)")
}
有这个处理程序handler:httpClientHand
,我现在不知道为什么它在那里,它是什么?当我尝试构建它时,会发生错误,说这个处理程序(httpClientHand)是一个未解析的标识符。我的问题是如何修改代码以便错误显示?