Bootstrapping a server

To write a mailer microservice, we need two dependencies: the nickel crate and the lettre crate. The first is a framework inspired by the Express framework for Node.js. The second implements the SMTP protocol and lets us interact with a mail server such as Postfix. Add these dependencies to Cargo.toml:

failure = "0.1"
lettre = { git = "https://github.com/lettre/lettre" }
lettre_email = { git = "https://github.com/lettre/lettre" }
nickel = "0.10"

For the lettre crate, we're using version 0.9.0 from GitHub, because it's not available on crates.io at the time of writing. We need to import some types from these crates:

use lettre::{ClientSecurity, SendableEmail, EmailAddress, Envelope, SmtpClient, SmtpTransport, Transport};
use lettre::smtp::authentication::IntoCredentials;
use nickel::{Nickel, HttpRouter, FormBody, Request, Response, MiddlewareResult};
use nickel::status::StatusCode;
use nickel::template_cache::{ReloadPolicy, TemplateCache};

Types from the std and failure crates are not presented in the preceding code. Now we can declare the Data struct that represents the shared state of the server:

struct Data {
sender: Mutex<Sender<SendableEmail>>,
cache: TemplateCache,
}

This struct contains two fields—a Sender to send messages to the mailer worker that we will implement later, and TemplateCache, which lets us load and render templates from a local directory. We will use it directly for the body of emails only, because this microservice won't render HTML responses.

The following code spawns a mail sender worker, creates an instance of the Data struct, creates a Nickel server, and binds it to the 127.0.0.1:8002 socket address:

fn main() {
let tx = spawn_sender();

let data = Data {
sender: Mutex::new(tx),
cache: TemplateCache::with_policy(ReloadPolicy::Always),
};

let mut server = Nickel::with_data(data);
server.get("/", middleware!("Mailer Microservice"));
server.post("/send", send);
server.listen("127.0.0.1:8002").unwrap();
}

In the cache field of the Data struct, we set a TemplateCache instance that needs ReloadPolicy as an argument. The ReloadPolicy parameter controls how often templates will be reloaded. We use the Always variant, which means templates will be reloaded on every rendering. It lets an administrator update templates without interrupting the service.

To start the server, we need to create a Nickel instance, which we initialize with the Data instance using the with_data method. Since Data will be shared across threads, we have to wrap Sender with MutexTemplateCache already implements Sync and Send and can be shared safely.

We add two methods to the Nickel server instance using the get and post methods. We add two handlers. The first is for the root path, /, which uses the middleware! macro from the nickel crate to attach a handler that returns a text response. The second handles requests with the /send path and calls the send function, which is implemented beneath that. The last method call, listen, binds the server's socket to an address. Now we can move forward and implement a handler.

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

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