Running and testing

To run this example, we need a PostgreSQL database instance. The simplest way to run it is to start a Docker container. We already did that in Chapter 7, Reliable Integration with Databases, where we studied how to use Rust with databases. You can start a new container with a PostgreSQL database instance using the following command:

docker run -it --rm --name test-pg -p 5432:5432 postgres

When the container is started, run the example server we wrote in this section with the cargo run command. It prints after compilation and the server is ready to accept requests:

Listening for requests at http://127.0.0.1:7878

Now you can use the link provided to log visits from your browser. If it is configured successfully, you will see the response in the browser:

Gotham processed the request and returned a result to you. If you shut down the database, the server will return a response with a 500 error code and the "connection closed" string. The last thing we have to do is verify that the server added the records to the database, because we used an asynchronous approach to interact with the database and used the same Runtime to handle HTTP requests and perform SQL statements. Run the psql client for the postgres://postgres@localhost:5432 connection and enter a query:

postgres=# SELECT * FROM agents;
agent | timestamp
-------------------------------------------+-------------------------------
Mozilla/5.0 Gecko/20100101 Firefox/64.0 | 2019-01-10 19:43:59.064265+00
Chrome/71.0.3578.98 Safari/537.36 | 2019-01-10 19:44:08.264106+00
(2 rows)

We made two requests from two different browsers and now we have two records in the agents table.

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

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