Compare commits
No commits in common. "84354c3834544dc0534c098eaa9985d6b9cbfd0d" and "225c67fb4c882dd6f4467ef991ee6699b7eda073" have entirely different histories.
84354c3834
...
225c67fb4c
68
src/main.rs
68
src/main.rs
|
@ -2,76 +2,26 @@
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[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;
|
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)]
|
#[derive(serde::Serialize)]
|
||||||
struct IndexContext {
|
struct Context {
|
||||||
name: &'static str,
|
name: String,
|
||||||
items: Vec<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("/")]
|
#[get("/")]
|
||||||
fn index() -> io::Result<Template> {
|
fn index() -> Template {
|
||||||
let users = list_users()?;
|
let c = String::from("home");
|
||||||
let context = IndexContext { name: "home", items: users };
|
let context = Context { name: c, items: vec!["One", "Two", "Three"] };
|
||||||
Ok(Template::render("index", &context))
|
Template::render("index", &context)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
rocket::ignite()
|
rocket::ignite()
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
.mount("/", routes![index, show])
|
.mount("/", routes![index]).launch();
|
||||||
.register(catchers![not_found])
|
|
||||||
.launch();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,14 +2,10 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<title>{{ title }}</title>
|
<title>404</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>404: Hey! There's nothing here.</h1>
|
<h1>404: Hey! There's nothing here.</h1>
|
||||||
The page at {{ path }} does not exist!
|
The page at {{ path }} does not exist!
|
||||||
The request was
|
|
||||||
<code><pre>
|
|
||||||
{{ req }}
|
|
||||||
</pre></code>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -2,14 +2,10 @@
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Here is {{name}}</h1>
|
<h1>Here is {{name}}</h1>
|
||||||
<h3>Here are the registered users :</h3>
|
<h3>Here are your items:</h3>
|
||||||
<ul>
|
<ul>
|
||||||
{% for s in items %}
|
{% for s in items %}
|
||||||
<li>
|
<li>{{ s }}</li>
|
||||||
<a href="/{{ s }}">
|
|
||||||
{{ s }}
|
|
||||||
</a>
|
|
||||||
</li>
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
{% extends "base" %}
|
|
||||||
|
|
||||||
{% block content %}
|
|
||||||
<h1>{{ name }}</h1>
|
|
||||||
<p>
|
|
||||||
{{ content }}
|
|
||||||
</p>
|
|
||||||
{% endblock content %}
|
|
|
@ -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