Compare commits
3 Commits
225c67fb4c
...
84354c3834
Author | SHA1 | Date |
---|---|---|
la Fleur | 84354c3834 | |
la Fleur | 79d66cfc4a | |
la Fleur | 88e8c43052 |
68
src/main.rs
68
src/main.rs
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
{% extends "base" %}
|
||||
|
||||
{% block content %}
|
||||
<h1>{{ name }}</h1>
|
||||
<p>
|
||||
{{ content }}
|
||||
</p>
|
||||
{% endblock content %}
|
|
@ -0,0 +1,3 @@
|
|||
# Cat
|
||||
|
||||
Hi, I'm Cat, that's my preferred username.
|
|
@ -0,0 +1 @@
|
|||
This is Sunny's landing page.
|
Loading…
Reference in New Issue