Rust Language

REST APIs with Rocket for RUST

Excerpts of code snippets to write rust based rest service using the rocket framework. Add dependencies #[macro_use] extern crate rocket; extern crate rocket_contrib; extern crate serde_json; extern crate log; Start Server fn main() { rocket::ignite().mount("/", routes![index, send]).launch(); } Get (Hello World Example) #[get("/")] fn index() -> &'static str { "Hello, world!" } Try it out curl http://localhost:15188/ POST with untyped object #[post("/send", format = "application/json", data ="<data>")] fn send(data: String) -> std::io::Result<String> { let data: Value = serde_json::from_str(&data)?

Writing Automated Test in Rust

Sample Structure #[cfg(test)] mod tests { #[test] fn it_works() { assert_eq!(2 + 2, 4); } } #[test] annotation before the fn line: this attribute indicates this is a test function, so the test runner knows to treat this function as a test. To run tests use cargo test

Rust and RabbitMQ

A very simple example Start Docker Container for RabbitMQ version: "3" services: rabbitmq: image: "rabbitmq:3-management" ports: - "5672:5672" - "15672:15672" volumes: - 'rabbitmq_data:/data' volumes: rabbitmq_data: Cargo.toml dependency Add the amqp dependency to the cargo.toml file [dependencies.amqp] version="0.1.3" Sample Code Import the crate to your application. In this instance, main.rs extern crate amqp; use amqp::Session; Simple Publish Function fn publish_message() -> () { use crate::amqp::Basic; let mut session = Session::open_url("amqp://localhost//").