user guard with file user definition
This commit is contained in:
parent
88e8c43052
commit
79d66cfc4a
60
src/main.rs
60
src/main.rs
|
@ -2,19 +2,53 @@
|
|||
|
||||
#[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::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)]
|
||||
struct IndexContext {
|
||||
name: &'static str,
|
||||
items: Vec<&'static str>
|
||||
items: Vec<String>
|
||||
}
|
||||
|
||||
#[catch(404)]
|
||||
fn not_found(req: &Request<'_>) -> Template {
|
||||
println!("{:#?}", req);
|
||||
let dbg = format!("{:?}", req);
|
||||
let dbg = format!("{:#?}", req);
|
||||
let mut map = std::collections::HashMap::new();
|
||||
map.insert("title", "Page not found");
|
||||
map.insert("path", req.uri().path());
|
||||
|
@ -22,16 +56,26 @@ fn not_found(req: &Request<'_>) -> Template {
|
|||
Template::render("error/404", &map)
|
||||
}
|
||||
|
||||
#[get("/<user>")]
|
||||
fn show(user: User) -> Template {
|
||||
//format!("{:#?}", user)
|
||||
Template::render("user", &user)
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> Template {
|
||||
let context = IndexContext { name: "home", items: vec!["One", "Two", "Three"] };
|
||||
Template::render("index", &context)
|
||||
fn index() -> std::io::Result<Template> {
|
||||
let users = list_users()?;
|
||||
let context = IndexContext { name: "home", items: users };
|
||||
Ok(Template::render("index", &context))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
//let user_map = get_user_map();
|
||||
//println!("{:#?}", user_map);
|
||||
|
||||
rocket::ignite()
|
||||
.attach(Template::fairing())
|
||||
.mount("/", routes![index])
|
||||
.mount("/", routes![index, show])
|
||||
.register(catchers![not_found])
|
||||
.launch();
|
||||
}
|
||||
|
|
|
@ -2,10 +2,14 @@
|
|||
|
||||
{% block content %}
|
||||
<h1>Here is {{name}}</h1>
|
||||
<h3>Here are your items:</h3>
|
||||
<h3>Here are the registered users :</h3>
|
||||
<ul>
|
||||
{% for s in items %}
|
||||
<li>{{ s }}</li>
|
||||
<li>
|
||||
<a href="/{{ s }}">
|
||||
{{ s }}
|
||||
</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
{% extends "base" %}
|
||||
|
||||
{% block content %}
|
||||
<h1>{{ name }}</h1>
|
||||
<p>
|
||||
{{ content }}
|
||||
</p>
|
||||
{% endblock content %}
|
|
@ -0,0 +1,3 @@
|
|||
# Cat
|
||||
|
||||
Hi, I'm Cat, that's my preferred username.
|
|
@ -0,0 +1 @@
|
|||
This is Sunny's landing page.
|
Loading…
Reference in New Issue