Compare commits

..

No commits in common. "84354c3834544dc0534c098eaa9985d6b9cbfd0d" and "225c67fb4c882dd6f4467ef991ee6699b7eda073" have entirely different histories.

6 changed files with 12 additions and 82 deletions

View File

@ -2,76 +2,26 @@
#[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, Debug)]
struct User {
name: String,
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>
struct Context {
name: String,
items: Vec<&'static str>
}
#[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() -> io::Result<Template> {
let users = list_users()?;
let context = IndexContext { name: "home", items: users };
Ok(Template::render("index", &context))
fn index() -> Template {
let c = String::from("home");
let context = Context { name: c, items: vec!["One", "Two", "Three"] };
Template::render("index", &context)
}
fn main() {
rocket::ignite()
.attach(Template::fairing())
.mount("/", routes![index, show])
.register(catchers![not_found])
.launch();
.mount("/", routes![index]).launch();
}

View File

@ -2,14 +2,10 @@
<html>
<head>
<meta charset="utf-8" />
<title>{{ title }}</title>
<title>404</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,14 +2,10 @@
{% block content %}
<h1>Here is {{name}}</h1>
<h3>Here are the registered users :</h3>
<h3>Here are your items:</h3>
<ul>
{% for s in items %}
<li>
<a href="/{{ s }}">
{{ s }}
</a>
</li>
<li>{{ s }}</li>
{% endfor %}
</ul>

View File

@ -1,8 +0,0 @@
{% extends "base" %}
{% block content %}
<h1>{{ name }}</h1>
<p>
{{ content }}
</p>
{% endblock content %}

View File

@ -1,3 +0,0 @@
# Cat
Hi, I'm Cat, that's my preferred username.

View File

@ -1 +0,0 @@
This is Sunny's landing page.