Skip to content

Rust Rocket Template Aksama Code Example

commandline_be edited this page May 16, 2019 · 5 revisions

After the introduction here is some code

1 create a project

cargo init myproject 

2.0 set the cargo.toml file to work for you

# this is a comment line in a .toml file

[package]
name = "myproject"
version = "0.1.0"
authors = ["author"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
askama = { version ="0.8.0", features = ["with-rocket"] }
rocket = { version = "0.4.1" }
rocket_contrib = { version = "0.4.1", features = ["serve"]}

2.1 create a /templates directory and create a valid index.html

<!DOCTYPE html>
<html lang=en>
<head>
    <title>{{ page_title }}</title>
</head>
<body>
    <h1>{{ page_intro }}</h1>
    <a href="{{ tohome }}"> to home </a>
</body>
</html>

3.0 Get some code to work (source code file is in /src/main.rs)

// these two slashes indicate a comment

#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;

// enable static files
use rocket_contrib::serve::StaticFiles;

// enable askama templates
use askama::Template;

// derive the Template and prepare to populate the index.html 
// with IndexTmpl supplied values from preset keys in struct IndexTmpl
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTmpl<'a> {
    page_title: &'a str, // populates {{ page_title }} in the index.html template file
    page_intro: &'a str, // populates {{ page_intro }} in the index.html template file
    tohome: &'a str // populates {{ page_title }} in the index.html template file
}

// this is the root of the web application, this listens only to the get http method
// offer a localhost:8000/contact mountpoint
#[get("/")]
fn index() -> IndexTmpl<'static> {
    IndexTmpl { 
                page_title: "homepage",
                page_intro: "homepage at localhost:8000",
                tohome: "/" 
            }
}

// offer a localhost:8000/contact mountpoint
// this listens only to the get http method
#[get("/contact")]
fn contacts() -> IndexTmpl<'static> {
    IndexTmpl { 
                page_title: "contacts",
                page_intro: "contact form",
                tohome: "/" 
            }
}

// this is the main loop causing the web service to mount various parts and components
// take note the templates folder was created beforehand and the sub directories as well
fn main () {
    rocket::ignite()        
        .mount("/", routes![index, contacts])
        // simpler than the below approach for StaticFiles is
        // create a folder /static and put all files and sub-folders (css, img ...) underneath
        // .mount("/", StaticFiles::from("static/"))
        // flip the comment for the appropriate lines above/below will show the difference (or similarity)
        // offer mountpoints for css img fonts scripts
        .mount("/css", StaticFiles::from("templates/css"))
        .mount("/img", StaticFiles::from("templates/img"))
        .mount("/fonts", StaticFiles::from("templates/fonts"))
        .mount("/scripts", StaticFiles::from("templates/scripts"))
        // just to let you know rocket can catch http error codes and offer custom error pages
       // .register(catchers![err404,err500])
    .launch(); // this is required or nothing works :)
} 

4 ready to run

  • now you can build the application

  • using cargo run --release is a short hand form and will build and launch the application

  • do note on first run it will download required crates and compile them, which will take some time

    cargo run --release

Clone this wiki locally