user guard with file user definition

This commit is contained in:
la Fleur 2020-11-10 03:41:39 +01:00
parent 88e8c43052
commit 79d66cfc4a
5 changed files with 70 additions and 10 deletions

View File

@ -2,19 +2,53 @@
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
use rocket::Request; //use std::fmt::Display;
use std::fs;
//use rocket::Request;
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
//use rocket::request::{Outcome, Request, FromRequest, FromParam};
use rocket::request::{Request, FromParam};
//use rocket::http::Status;
use rocket::http::RawStr;
#[derive(serde::Serialize, Debug)]
struct User {
name: String,
content: String,
}
impl<'r> FromParam<'r> for User {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> std::result::Result<Self, Self::Error> {
let filename = format!("users/{}.md", param);
let content = fs::read_to_string(&filename);
match content {
Ok(value) => Ok(User{ name: param.to_string(), content: value }),
Err(_) => Err(param),
}
}
}
fn list_users() -> std::io::Result<Vec<String>> {
let mut users: Vec<String> = vec!();
for path in fs::read_dir("users")? {
let file = path?;
let user = format!("{:?}", file.file_name()).replace("\"", "").replace(".md", "");
users.push(user);
}
Ok(users)
}
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
struct IndexContext { struct IndexContext {
name: &'static str, name: &'static str,
items: Vec<&'static str> items: Vec<String>
} }
#[catch(404)] #[catch(404)]
fn not_found(req: &Request<'_>) -> Template { fn not_found(req: &Request<'_>) -> Template {
println!("{:#?}", req); let dbg = format!("{:#?}", req);
let dbg = format!("{:?}", req);
let mut map = std::collections::HashMap::new(); let mut map = std::collections::HashMap::new();
map.insert("title", "Page not found"); map.insert("title", "Page not found");
map.insert("path", req.uri().path()); map.insert("path", req.uri().path());
@ -22,16 +56,26 @@ fn not_found(req: &Request<'_>) -> Template {
Template::render("error/404", &map) Template::render("error/404", &map)
} }
#[get("/<user>")]
fn show(user: User) -> Template {
//format!("{:#?}", user)
Template::render("user", &user)
}
#[get("/")] #[get("/")]
fn index() -> Template { fn index() -> std::io::Result<Template> {
let context = IndexContext { name: "home", items: vec!["One", "Two", "Three"] }; let users = list_users()?;
Template::render("index", &context) let context = IndexContext { name: "home", items: users };
Ok(Template::render("index", &context))
} }
fn main() { fn main() {
//let user_map = get_user_map();
//println!("{:#?}", user_map);
rocket::ignite() rocket::ignite()
.attach(Template::fairing()) .attach(Template::fairing())
.mount("/", routes![index]) .mount("/", routes![index, show])
.register(catchers![not_found]) .register(catchers![not_found])
.launch(); .launch();
} }

View File

@ -2,10 +2,14 @@
{% block content %} {% block content %}
<h1>Here is {{name}}</h1> <h1>Here is {{name}}</h1>
<h3>Here are your items:</h3> <h3>Here are the registered users :</h3>
<ul> <ul>
{% for s in items %} {% for s in items %}
<li>{{ s }}</li> <li>
<a href="/{{ s }}">
{{ s }}
</a>
</li>
{% endfor %} {% endfor %}
</ul> </ul>

8
templates/user.html.tera Normal file
View File

@ -0,0 +1,8 @@
{% extends "base" %}
{% block content %}
<h1>{{ name }}</h1>
<p>
{{ content }}
</p>
{% endblock content %}

3
users/cat.md Normal file
View File

@ -0,0 +1,3 @@
# Cat
Hi, I'm Cat, that's my preferred username.

1
users/sunny.md Normal file
View File

@ -0,0 +1 @@
This is Sunny's landing page.