IRON, Framework on which you can rely while working on RUST

Reading Time: 3 minutes

Hello Readers! Rust’s Iron framework is a high level web framework which is built in and built for Rust. It is basically built on hyper. It is designed to take advantage of Rust’s greatest features – it’s excellent type system and principled approach to ownership in both single threaded and multi threaded contexts.

Rust’s Iron is highly concurrent and can scale horizontally on more machines behind a load balancer or by running more threads on a more powerful machine. Iron avoids the bottlenecks encountered in highly concurrent code by avoiding shared writes and locking in the core framework.

Rust Iron framework

Lets Start with Hello World

extern crate iron;

use iron::prelude::*;
use iron::status;

fn main() {
    Iron::new(|_: &mut Request| {
        Ok(Response::with((status::Ok, "Hello World!")))
    }).http("localhost:3000").unwrap();
}

Iron is as extensible and pluggable as possible. Iron’s core is concentrated and avoids unnecessary features by leaving them to middleware, plugins, and modifiers.

Iron is extended to new functionality mainly by using Middleware, Plugins, and Modifiers. Most extensions that would be provided by middleware in other web frameworks are instead addressed by the much simpler Modifier and Plugin systems.

Modifiers allow external code to manipulate Requests and Response in a comfortable manner, allowing third-party extensions to get the same treatment as modifiers defined in Iron itself. Plugins allow for loosly – evaluated, automatically cached extensions to Requests and Responses, perfect for parsing, accessing, and otherwise lazily manipulating an http connection.

When it is necessary to modify the control flow of a Request flow, Middleware come into existance, hijack the entire handling of a Request, check an incoming Request, or to do final post-processing. This covers areas such as routing, mounting, static asset serving, final template rendering, authentication, and logging.

Iron comes with only basic modifiers for setting the status, body, and various headers, and the infrastructure for creating modifiers, plugins, and middleware. No plugins or middleware are bundled with Iron.

Installation

If you’re using Cargo, just add Iron to your Cargo.toml:

[dependencies.iron]
version = "*"

Iron is 100% safe code :

$ rg unsafe src | wc
       0       0       0

Re-exports

pub use request::Request;

The Request given to all Middleware.

Stores all the properties of the client’s request plus an TypeMap for data communication between middleware.

pub use response::Response;

The response representation given to Middleware

pub use error::IronError;

The type of Errors inside and when using Iron.

IronError informs its receivers of two things:

  • What went wrong
  • What to do about it

The error field is responsible for informing receivers of which error occured, and receivers may also modify the error field by layering it (building up a cause chain).

The response field provides a tangible action to be taken if this error is not otherwise handled.

Modules

errorIron’s error type and associated utilities.
headersHeaders container, and common header fields.
methodHTTP Methods
middlewareThis module contains Iron’s middleware and handler system, the fundamental building blocks for handling HTTP requests and generating responses.
mimeRe-exporting the mime crate, for convenience.
modifierRe-exports from the Modifier crate.
modifiersThis module defines a series of convenience modifiers for changing Responses.
preludeA module meant to be glob imported when using Iron.
requestIron’s HTTP Request representation and associated methods.
responseIron’s HTTP Response representation and associated methods.
statusStatus Codes
typemapRe-exports from the TypeMap crate.
urlRe-exports from the url crate.
Modules in IRON framework,Rust

Type Definitions

IronResultThe Result alias used throughout Iron and in clients of Iron.

So this was a brush-up for iron framework in rust. Stay connected to explore more such topics. Thank You for reading.

If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter: https://bit.ly/2Vdlld7.

<br />

<a href="http://www.knoldus.com/connect/contact-us.knol" target="_blank" rel="noopener">
<img class="  wp-image-38019 aligncenter" src="https://www.knoldus.com/images/knoldus-blog-footer-banner.jpg" alt="Knoldus-blog-footer-image" width="595" height="420" />
</a>

<br />

Written by 

Ayushi is a Software Developer having more than 1.5 year of experience in RUST. Her practice area is Rust and Go. She loves to solve daily coding challenges.