You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.1 KiB

#[macro_use] extern crate rocket;
use rocket::tokio::time::{sleep, Duration};
use rocket::form::Form;
use rocket_dyn_templates::{Template, context};
#[derive(FromForm)]
struct Note<'r> {
title: &'r str,
description: &'r str,
}
#[post("/form", data="<note>")]
async fn form(note: Form<Note<'_>>) -> String {
format!("New note: {} with description\n{}.", note.title, note.description)
}
#[get("/hello/<name>")]
async fn hello(name: &str) -> String {
format!("Hello, {}", name)
}
fn test1() -> Vec {
Vec::new()
}
#[get("/")]
async fn index() -> Template {
let notes: Vec<Note<'_>> = Vec::new();
notes.append(Note{ title: "Test", description: "Test" });
Template::render("index", context!{
title: "Home",
name: "Tait Hoyem"
})
}
#[get("/delay/<seconds>")]
async fn delay(seconds: u64) -> String {
sleep(Duration::from_secs(seconds)).await;
format!("Waited for {} seconds", seconds)
}
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
let _rocket = rocket::build()
.mount("/", routes![index, delay, hello, form])
.attach(Template::fairing())
.launch()
.await?;
Ok(())
}