Rocket (web framework)

From Wikipedia, the free encyclopedia

Rocket is a web framework written in Rust.[3][4] It supports handling HTTP requests, Web Sockets, JSON, templating, and more. Its design was inspired by Rails, Flask, Bottle, and Yesod.[5] It is dually licensed under the MIT License and the Apache License.

Quick Facts Developer(s), Initial release ...
Rocket
Developer(s)Sergio Benitez[1]
Initial release2016; 9 years ago (2016)
Stable release
0.5.1[2]  / 23 May 2024; 10 months ago (23 May 2024)
Repositorygithub.com/rwf2/Rocket
Written inRust
Operating systemLinux, macOS, Windows, FreeBSD, OpenBSD
TypeWeb framework
LicenseMIT License or Apache License
Websiterocket.rs
Close

To create a web server with Rocket, the user will define an application, then use the "mount" function to attach "routes" to it. Each "route" is a rust function with a macro attached to it. The function will define code that should respond to an HTTP request. The macro that is written as part of the function declaration will define which HTTP Method (such as GET, POST, PUT, etc.) it should be handle, as well as a pattern describing the URL it should be relevant to.

Example

This is an example of a working rocket application:

#[macro_use] extern crate rocket;

#[get("/hello/<name>/<age>")]
fn hello(name: &str, age: u8) -> String {
    format!("Hello, {} year old named {}!", age, name)
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![hello])
}

Sending an HTTP GET request to /hello/John/20 would return the following response:

Hello, 20 year old named John!.

Features

Rocket implements the following features:

  • Routing - Rocket allows the user to define the structure of routes that the application should consider, as well as the code that should run in different routing combination. For example, the following code will make the rocket application to respond to the /hello route with "Hello World":
    #[get("/")]
    fn index() -> &'static str {
        "Hello, world!"
    }
    
  • Form Data - Rocket allows the user to define a Serde model, and use it to parse the Form Data, and pass it as native rust object to the route handler.
  • Request Guards - the route handlers can contain a special kind of parameters named "Request Guard"s that are meant to prevent the code inside the handler to be called in case a certain condition is not met. This feature can be used for example, to prevent requests that do not contain a API Key. By using the Request Guard feature, the user can define the condition in one place, and apply it to prevent access to multiple routes by adding the guard to their list of parameters.

References

Loading related searches...

Wikiwand - on

Seamless Wikipedia browsing. On steroids.