Section IV - executable
Once we have created our service and all the tests have passed, we are ready to build out the executable and run our service.
Add Log functionality
Since Logs is eleventh factor in a 12 Factor Application, we will enable this attribute by implementing automated logging for this RESTful endpoint. by including the
log
andenv_logger
creates.
We start by first adding the log
and env_logger
crates to the Cargo.toml
manifest.
Next, we update the lib.rs
file to include the logging creates and modules.
We place the extern crate
declarations for these crates at the top (so that they are shared in the project).
The final lib.rs
file should look like this:
Writing the executable
Now that we have the dependent crates and modules declared in our library, we can call the service in an executable.
In the hello-world.rs
file in the bin
directory, rewrite the file so it looks like the following:
Noteworthy: we call the module and its functionality by using the following code snippets:
use myapp::hello_world;
&hello_world::get_service_path()
hello_world::index
Make sure all your tests are still passing by using the cargo test
command.
Starting the service
We are now ready to start the RESTful service. There are 2 ways to start the service.
Running using
cargo run
command while developing (local service testing)
Since we are working on a virtual machine , we will use curl
to call our services.
Run the following script.
NOTE: Make sure you are in the environment directory.
cd $HOME/environment
You should see the message Hello World!
On the command line where the service is running, you will notice that the calls are being logged and printed to the console.
To stop the service, use ctrl
+ c
.
2. Running using the executable.
Whenever you use the cargo build
command, it places the created executable in the target/debug directory with the same name that was defined in the Cargo.toml
manifest.
Since it is an executable, simple run the executable from the command terminal, and make the same URL call from the browser.
NOTE: Example below is for Windows.
Last updated
Was this helpful?