Compare commits

...

3 Commits

Author SHA1 Message Date
la Fleur 84354c3834 reorganize std::fs and std::io imports 2020-11-10 11:25:21 +01:00
la Fleur 79d66cfc4a user guard with file user definition 2020-11-10 03:41:39 +01:00
la Fleur 88e8c43052 add a 404 template and page 2020-11-08 00:37:05 +01:00
6 changed files with 82 additions and 12 deletions

View File

@ -2,26 +2,76 @@
#[macro_use] extern crate rocket;
use std::result::Result;
use std::fs;
use std::io;
use rocket::request::{Request, FromParam};
use rocket::http::RawStr;
use rocket_contrib::templates::Template;
#[derive(serde::Serialize)]
struct Context {
#[derive(serde::Serialize, Debug)]
struct User {
name: String,
items: Vec<&'static str>
content: String,
}
impl<'r> FromParam<'r> for User {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> 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() -> 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<String>
}
#[catch(404)]
fn not_found(req: &Request<'_>) -> Template {
let dbg = format!("{:#?}", req);
let mut map = std::collections::HashMap::new();
map.insert("title", "Page not found");
map.insert("path", req.uri().path());
map.insert("req", &dbg);
Template::render("error/404", &map)
}
#[get("/<user>")]
fn show(user: User) -> Template {
//format!("{:#?}", user)
Template::render("user", &user)
}
#[get("/")]
fn index() -> Template {
let c = String::from("home");
let context = Context { name: c, items: vec!["One", "Two", "Three"] };
Template::render("index", &context)
fn index() -> io::Result<Template> {
let users = list_users()?;
let context = IndexContext { name: "home", items: users };
Ok(Template::render("index", &context))
}
fn main() {
rocket::ignite()
.attach(Template::fairing())
.mount("/", routes![index]).launch();
.mount("/", routes![index, show])
.register(catchers![not_found])
.launch();
}

View File

@ -2,10 +2,14 @@
<html>
<head>
<meta charset="utf-8" />
<title>404</title>
<title>{{ title }}</title>
</head>
<body>
<h1>404: Hey! There's nothing here.</h1>
The page at {{ path }} does not exist!
The request was
<code><pre>
{{ req }}
</pre></code>
</body>
</html>

View File

@ -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>

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.