Compare commits
3 Commits
225c67fb4c
...
84354c3834
Author | SHA1 | Date |
---|---|---|
la Fleur | 84354c3834 | |
la Fleur | 79d66cfc4a | |
la Fleur | 88e8c43052 |
66
src/main.rs
66
src/main.rs
|
@ -2,26 +2,76 @@
|
||||||
|
|
||||||
#[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)]
|
#[derive(serde::Serialize, Debug)]
|
||||||
struct Context {
|
struct User {
|
||||||
name: String,
|
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("/")]
|
#[get("/")]
|
||||||
fn index() -> Template {
|
fn index() -> io::Result<Template> {
|
||||||
let c = String::from("home");
|
let users = list_users()?;
|
||||||
let context = Context { name: c, items: vec!["One", "Two", "Three"] };
|
let context = IndexContext { name: "home", items: users };
|
||||||
Template::render("index", &context)
|
Ok(Template::render("index", &context))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
rocket::ignite()
|
rocket::ignite()
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
.mount("/", routes![index]).launch();
|
.mount("/", routes![index, show])
|
||||||
|
.register(catchers![not_found])
|
||||||
|
.launch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,14 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<title>404</title>
|
<title>{{ title }}</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,10 +2,14 @@
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Here is {{name}}</h1>
|
<h1>Here is {{name}}</h1>
|
||||||
<h3>Here are your items:</h3>
|
<h3>Here are the registered users :</h3>
|
||||||
<ul>
|
<ul>
|
||||||
{% for s in items %}
|
{% for s in items %}
|
||||||
<li>{{ s }}</li>
|
<li>
|
||||||
|
<a href="/{{ s }}">
|
||||||
|
{{ s }}
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</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