To run a microservice created with Rocket, you need to create a Rocket.toml configuration file. This allows you to configure a microservice before starting. Look at the following Rocket.toml contents:
[global]
template_dir = "static"
address = "127.0.0.1"
port = 8003
[global.databases.sqlite_database]
url = "test.db"
In this configuration, we declared global parameters such as: the template_dir directory with templates (if we use it), address and port, and a url for a database connection.
You can override any parameter using environment variables. For example, if we need to set the port parameter to 80, we can run a microservice with a command:
ROCKET_PORT=3721 cargo run
The Rocket framework also supports three different types of environment: development, staging, and production. It allows you to have three configurations in one. Add an extra section in addition to the global section and run a microservice with the corresponding mode:
ROCKET_ENV=staging cargo run
To test a microservice, it's sufficient to start it with a simple cargo run without extra parameters. When the service starts, we can add a comment with the following command and print a list of all comments:
curl -d 'uid=user_id&text="this is a comment"' -X POST http://localhost:8003/new_comment
curl http://localhost:8003/list
This command prints all comments in JSON format. As you can see, we don't convert any structs directly to JSON. Rocket does this automatically.