Compare commits

..

No commits in common. "133e1e4aac45468a7407e3df4bb307fba1dce02b" and "84354c3834544dc0534c098eaa9985d6b9cbfd0d" have entirely different histories.

7 changed files with 25 additions and 29 deletions

View File

@ -1,3 +0,0 @@
Let's say this page talks about cats.
We will want to _insist_ on some.

View File

@ -1 +0,0 @@
This could be any sunny day.

View File

@ -9,40 +9,38 @@ use rocket::request::{Request, FromParam};
use rocket::http::RawStr; use rocket::http::RawStr;
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
const CONTENT_DIR:&str = "content";
#[derive(serde::Serialize, Debug)] #[derive(serde::Serialize, Debug)]
struct Content { struct User {
title: String, name: String,
content: String, content: String,
} }
impl<'r> FromParam<'r> for Content { impl<'r> FromParam<'r> for User {
type Error = &'r RawStr; type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> { fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
let filename = format!("{}/{}.md", CONTENT_DIR, param); let filename = format!("users/{}.md", param);
let content = fs::read_to_string(&filename); let content = fs::read_to_string(&filename);
match content { match content {
Ok(value) => Ok(Content{ title: param.to_string(), content: value }), Ok(value) => Ok(User{ name: param.to_string(), content: value }),
Err(_) => Err(param), Err(_) => Err(param),
} }
} }
} }
fn list_contents() -> io::Result<Vec<String>> { fn list_users() -> io::Result<Vec<String>> {
let mut contents: Vec<String> = vec!(); let mut users: Vec<String> = vec!();
for path in fs::read_dir(CONTENT_DIR)? { for path in fs::read_dir("users")? {
let file = path?; let file = path?;
let content = format!("{:?}", file.file_name()).replace("\"", "").replace(".md", ""); let user = format!("{:?}", file.file_name()).replace("\"", "").replace(".md", "");
contents.push(content); users.push(user);
} }
Ok(contents) Ok(users)
} }
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
struct IndexContext { struct IndexContext {
title: String, name: &'static str,
items: Vec<String> items: Vec<String>
} }
@ -56,18 +54,16 @@ fn not_found(req: &Request<'_>) -> Template {
Template::render("error/404", &map) Template::render("error/404", &map)
} }
#[get("/<content>")] #[get("/<user>")]
fn show(content: Content) -> Template { fn show(user: User) -> Template {
Template::render("content", &content) //format!("{:#?}", user)
Template::render("user", &user)
} }
#[get("/")] #[get("/")]
fn index() -> io::Result<Template> { fn index() -> io::Result<Template> {
let cwd = std::env::current_dir()?; let users = list_users()?;
let title = String::from(cwd.to_str().unwrap().split("/").last().unwrap()); let context = IndexContext { name: "home", items: users };
//println!("{}", title);
let contents = list_contents()?;
let context = IndexContext { title: title, items: contents };
Ok(Template::render("index", &context)) Ok(Template::render("index", &context))
} }

View File

@ -1,8 +1,8 @@
{% extends "base" %} {% extends "base" %}
{% block content %} {% block content %}
<h1>This is {{ title }}</h1> <h1>Here is {{name}}</h1>
<h3>Here are the available pages :</h3> <h3>Here are the registered users :</h3>
<ul> <ul>
{% for s in items %} {% for s in items %}
<li> <li>

View File

@ -1,7 +1,7 @@
{% extends "base" %} {% extends "base" %}
{% block content %} {% block content %}
<h1>{{ title }}</h1> <h1>{{ name }}</h1>
<p> <p>
{{ content }} {{ content }}
</p> </p>

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.