From 464f991696ce5efeb693ea50f40c1999385d1ecf Mon Sep 17 00:00:00 2001 From: kirbylife Date: Wed, 13 Nov 2019 01:17:38 -0600 Subject: [PATCH] now generate the title randomly --- .gitignore | 3 +++ src/main.rs | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6936990..9b9bbab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /target **/*.rs.bk Cargo.lock + +# Enviroment variables +.env \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e7a11a9..549e28a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,31 @@ -fn main() { - println!("Hello, world!"); +#![feature(proc_macro_hygiene, decl_macro)] + +#[macro_use] +extern crate rocket; +extern crate rand; +extern crate rocket_contrib; + +use rand::Rng; +use rocket_contrib::serve::StaticFiles; +use rocket_contrib::templates::Template; +use std::collections::HashMap; + +const TITLES: [&str; 4] = ["/* {} */", "# {}", "// {}", ""]; + +#[get("/")] +fn index() -> Template { + let mut rng = rand::thread_rng(); + let mut context = HashMap::new(); + let title_fmt = TITLES[rng.gen_range(0, TITLES.len())]; + let title = str::replace(title_fmt, "{}", "CódigoComentado"); + context.insert("title", title); + Template::render("index", context) +} + +fn main() { + rocket::ignite() + .attach(Template::fairing()) + .mount("/", routes![index]) + .mount("/static", StaticFiles::from("static")) + .launch(); }