The next thing we have to add to our App instance is routing. There is a route function that lets you set a handler for any route. But it's more thoughtful to use scopes to construct a structure of nested paths. Look at the following code:
app.scope("/api", |scope| {
scope
.route("/signup", http::Method::POST, signup)
.route("/signin", http::Method::POST, signin)
.route("/new_comment", http::Method::POST, new_comment)
.route("/comments", http::Method::GET, comments)
})
The scope method of our App struct expects a prefix of a path and a closure with a scope as a single argument, and creates a scope that can contain subroutes. We create a scope for the /api path prefix and add four routes using the route method: /signup, /signin, /new_comment, and /comments. The route method expects a suffix including a path, a method, and a handler. For example, if a server now takes a request for /api/singup with the POST method, it will call the signup function. Let's add a default handler for other paths.
Our microservice also uses Counter middleware, which we will implement later, to count the total quantity of requests. We need to add a route to render statistics for the microservice, as follows:
.route("/counter", http::Method::GET, counter)
As you can see, we don't need scope here, since we have only one handler and can call the route method directly for the App instance (not scope).