Repository navigation
Swift endpoint setup
In a Swift setup endpoints can be added to the server in two ways. One is to pass them as an argument when you launch the server, the other is afterwards through a number of functions.
The initialiser for the server has as it's last argument, a Swift Result Builder that returns Endpoint instances to be configured. Here's an example of setting up Voodoo with endpoints during initialisation:
server = try VoodooServer {
HTTPEndpoint(.GET, "/config", response: .json(["featureFlag": true])
HTTPEndpoint(.POST, "/login", response: .dynamic { request, cache in
cache.loggedIn = true
cache.username = request.formParameters.username
return .ok()
})
HTTPEndpoint(.GET, "/profile", response: .dynamic { request, cache in
if cache.loggedIn ?? false {
return .authorisationFailure()
}
return .ok(body: .json(["username": cache.username]))
})
}As you can see this is a builder style argument that allows you to avoid a lot of the boilerplate that would be required if an array was being passed. Note though, that you can also return arrays of Endpoints in this as the result builder has been applied to both Endpoint and [Endpoint] arrays.
Endpoints can also be added after the server has been started and there is a variety of convenience methods on VoodooServer to make this easy. You will find functions that take endpoints, endpoint builders, arrays and even some functions that take the individual endpoint arguments, both for HTTPEndpoint and GraphQLEndpoint instances.