Types of framework

We need a lot of types for the microservice. Let's talk a little about every type we import here:

use failure::{Error, format_err};
use futures::{Future, Stream, future};
use gotham::handler::HandlerFuture;
use gotham::middleware::state::StateMiddleware;
use gotham::pipeline::single::single_pipeline;
use gotham::pipeline::single_middleware;
use gotham::router::Router;
use gotham::router::builder::{DefineSingleRoute, DrawRoutes, build_router};
use gotham::state::{FromState, State};
use gotham_derive::StateData;
use hyper::Response;
use hyper::header::{HeaderMap, USER_AGENT};
use std::sync::{Arc, Mutex};
use tokio::runtime::Runtime;
use tokio_postgres::{Client, NoTls};

Most likely, you are familiar with types from failure and futures crates, because we used them a lot in the first part of the book. The most interesting are types of the gotham crate. There are modules that cover different parts of the framework; the handler module contains HandlerFuture, which is an alias to the Future trait with predefined types:

type HandlerFuture = dyn Future<
Item = (State, Response<Body>),
Error = (State, HandlerError)
> + Send;

We will use this Future alias in our asynchronous handlers. Also, this module contains the IntoHandlerFuture trait, which is implemented for a tuple that can be converted into a response.

The middleware module contains StateMiddleware, which we will use to attach a state to our microservice.

The pipeline module contains two functions we will use: single_middleware and single_pipeline. The first creates a Pipeline with a single provided middleware inside. The second function is necessary to create a pipeline chain from a single pipeline instance.

The router module includes types we need to construct a routing table for our microservice. The Router struct is a type that contains routes and we have to instantiate and provide it for a server. We will do this with the build_router function call.

For the DrawRoutes trait, we need to have methods of Router to add paths. It adds get, get_or_head, put, post, and other methods to register paths with corresponding HTTP methods. Calling those methods returns the SingleRouteBuilder instance and we need to use the DefineSingleRoute trait for the to method, which allows us to map a registered path to a Handler.

The state module provides us with the capability to use generic State and convert it to a type we need by calling the borrow_from method of the FromState trait that implemented the types that implement the StateData trait.

Generic State in gotham is a very flexible concept and provides the capability to get references to different parts of the environment. You can get a reference to your own state type or to the request data.

We need some types from the hyper crate, because the crate is used in the  gotham implementation and in some types of hyper. We imported the Response type to create responses for a client and the HeaderMap to get access to request headers, because we need to get a value for the USER_AGENT header.

Since we are developing an asynchronous application, we have to use the same reactor to execute all tasks in the same runtime. To do this, we will use a manually created Runtime from the tokio crate.

To connect to a database, we need to import the Client type from the tokio-postgres crate and NoTls to configure a connection.

Now we have imported all we need to write the main function of the application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.181.252