|
|
|
@ -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();
|
|
|
|
|
}
|
|
|
|
|