How to write your first web-app using Rust & Rocket?
Starting from today, I'm writing a series of posts to share how to use Rust to create a web-app based on Rocket web framework. Yes, the name is Rocket! Cool, isn't it? That's what got me interested and I would like to share with you my experience with it.
But first, let's start with the prerequisites. As you can find in the getting started page of the Rocket web site, you'll need to install the nightly Rust toolchain. The best way to do that is to use RustUp. If your computer is running macOS, like mine is, you can install rustup simply with the following command:
$ curl https://sh.rustup.rs -sSf | sh
[...]
Current installation options:
default host triple: x86_64-apple-darwin
default toolchain: stable
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
1 # <====== Enter 1 to proceed
[...]
Rust is installed now. Great!
[...]
Simple, right? That's why I love macOS. Now, you must reload your environment variables to use cargo
without specifying the path. All it takes is to run:
$ source ~/.cargo/env
Next, verify that everything is working correctly:
$ cargo
Rust's package manager
[...]
Rocket always requires the latest version of Rust nightly, and we can just install that and set it as the default toolchain using rustup:
$ rustup install nightly
[...]
$ rustup default nightly
[...]
Great job! We're finally ready to write our first web application with Rocket. We'll call our application — "hello-rocket". Let's start by creating a new folder for our "hello-rocket" using Rust's package manager cargo
:
$ cargo new hello-rocket --bin
[...]
$ cd hello-rocket
After you're done, you should be seeing a Cargo.toml
configuration file and src/main.rs
inside the "hello-rocket" folder. Now, we've to modify the Cargo.toml
file in the [dependencies]
section to add Rocket and its code generation facilities:
[package]
name = "hello-rocket"
version = "0.1.0"
authors = ["mrblueberry"]
[dependencies]
rocket = "0.3.14"
rocket_codegen = "0.3.14"
Alright, we're almost ready to launch! The main.rs
file is the starting point of a Rust application. Let's modify that using our favourite text editor with the following source code:
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate rocket;
#[get("/")]
fn index() -> &'static str {
"Rocket to the moon!"
}
fn main() {
rocket::ignite().mount("/", routes![index]).launch();
}
We'll leave the explanation in future series when we explore Rocket's capabilities. In short, it creates an index
route, mounts the route at the /
path, and launches the application.
Let's compile and run our web-app:
$ cargo run
[...]
🔧 Configured for development.
=> address: localhost
=> port: 8000
=> log: normal
=> workers: 4
=> secret key: generated
=> limits: forms = 32KiB
=> tls: disabled
🛰 Mounting '/':
=> GET /
🚀 Rocket has launched from http://localhost:8000
Visit http://localhost:8000
to see your first Rocket application in action!
upvote for me please? https://steemit.com/news/@bible.com/6h36cq
Congratulations,
You are one of this weeks Featured Followers !.