Skip to content

Instantly share code, notes, and snippets.

@siavashalipour
Created April 10, 2018 23:13
Show Gist options
  • Save siavashalipour/57d113898ade24b0f8434cf96d668944 to your computer and use it in GitHub Desktop.
Save siavashalipour/57d113898ade24b0f8434cf96d668944 to your computer and use it in GitHub Desktop.
ServerSideSwift - First part of Application.swift
func postInit() throws {
// Endpoints
initializeHealthRoutes(app: self)
let dbServiceCollection = dbService.getCollection()
// register
router.all("/register", middleware: BodyParser())
router.post("/register") { request, response, next in
guard let parsedBody = request.body else {
next()
return
}
switch parsedBody {
case .json(let jsonBody):
let jsonData = try JSONSerialization.data(withJSONObject: jsonBody, options: .prettyPrinted)
let user = try JSONDecoder().decode(UserModel.self, from: jsonData)
try dbServiceCollection?.append(user.createDocument())
try response.send(user).end()
default:
break
}
next()
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment