reorganize content and templates
This commit is contained in:
parent
84354c3834
commit
980d02a946
|
@ -0,0 +1,3 @@
|
||||||
|
Let's say this page talks about cats.
|
||||||
|
|
||||||
|
We will want to _insist_ on some.
|
|
@ -0,0 +1 @@
|
||||||
|
This could be any sunny day.
|
33
src/main.rs
33
src/main.rs
|
@ -9,33 +9,35 @@ 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 User {
|
struct Content {
|
||||||
name: String,
|
title: String,
|
||||||
content: String,
|
content: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'r> FromParam<'r> for User {
|
impl<'r> FromParam<'r> for Content {
|
||||||
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!("users/{}.md", param);
|
let filename = format!("{}/{}.md", CONTENT_DIR, param);
|
||||||
let content = fs::read_to_string(&filename);
|
let content = fs::read_to_string(&filename);
|
||||||
match content {
|
match content {
|
||||||
Ok(value) => Ok(User{ name: param.to_string(), content: value }),
|
Ok(value) => Ok(Content{ title: param.to_string(), content: value }),
|
||||||
Err(_) => Err(param),
|
Err(_) => Err(param),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_users() -> io::Result<Vec<String>> {
|
fn list_contents() -> io::Result<Vec<String>> {
|
||||||
let mut users: Vec<String> = vec!();
|
let mut contents: Vec<String> = vec!();
|
||||||
for path in fs::read_dir("users")? {
|
for path in fs::read_dir(CONTENT_DIR)? {
|
||||||
let file = path?;
|
let file = path?;
|
||||||
let user = format!("{:?}", file.file_name()).replace("\"", "").replace(".md", "");
|
let content = format!("{:?}", file.file_name()).replace("\"", "").replace(".md", "");
|
||||||
users.push(user);
|
contents.push(content);
|
||||||
}
|
}
|
||||||
Ok(users)
|
Ok(contents)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(serde::Serialize)]
|
#[derive(serde::Serialize)]
|
||||||
|
@ -54,16 +56,15 @@ fn not_found(req: &Request<'_>) -> Template {
|
||||||
Template::render("error/404", &map)
|
Template::render("error/404", &map)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/<user>")]
|
#[get("/<content>")]
|
||||||
fn show(user: User) -> Template {
|
fn show(content: Content) -> Template {
|
||||||
//format!("{:#?}", user)
|
Template::render("content", &content)
|
||||||
Template::render("user", &user)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index() -> io::Result<Template> {
|
fn index() -> io::Result<Template> {
|
||||||
let users = list_users()?;
|
|
||||||
let context = IndexContext { name: "home", items: users };
|
let context = IndexContext { name: "home", items: users };
|
||||||
|
let contents = list_contents()?;
|
||||||
Ok(Template::render("index", &context))
|
Ok(Template::render("index", &context))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{% extends "base" %}
|
{% extends "base" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>{{ name }}</h1>
|
<h1>{{ title }}</h1>
|
||||||
<p>
|
<p>
|
||||||
{{ content }}
|
{{ content }}
|
||||||
</p>
|
</p>
|
|
@ -1,7 +1,7 @@
|
||||||
{% extends "base" %}
|
{% extends "base" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Here is {{name}}</h1>
|
<h1>Here is {{title}}</h1>
|
||||||
<h3>Here are the registered users :</h3>
|
<h3>Here are the registered users :</h3>
|
||||||
<ul>
|
<ul>
|
||||||
{% for s in items %}
|
{% for s in items %}
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
# Cat
|
|
||||||
|
|
||||||
Hi, I'm Cat, that's my preferred username.
|
|
|
@ -1 +0,0 @@
|
||||||
This is Sunny's landing page.
|
|
Loading…
Reference in New Issue