diff options
Diffstat (limited to 'src/server.rs')
-rw-r--r-- | src/server.rs | 65 |
1 files changed, 52 insertions, 13 deletions
diff --git a/src/server.rs b/src/server.rs index 9c5618b..a203af9 100644 --- a/src/server.rs +++ b/src/server.rs @@ -14,8 +14,11 @@ use rocket::request::Request; use rocket::response::Redirect; use rocket::State; use rocket_contrib::templates::Template; +use serde_json::map::Map; use serde_json::to_value; +use serde_json::Value as Json; use std::collections::HashMap; +use rocket_contrib::serve::StaticFiles; use crate::db; use crate::error::Error; @@ -32,6 +35,12 @@ pub struct LoggedInUser { pub username: String, } +fn default_context() -> Map<String, Json> { + let mut data = Map::new(); + data.insert("parent".to_string(), json!("layout")); + data +} + impl<'a, 'r> FromRequest<'a, 'r> for LoggedInUser { type Error = Error; @@ -63,23 +72,47 @@ impl<'a, 'r> FromRequest<'a, 'r> for LoggedInUser { } } -#[get("/")] -fn index(user: Option<LoggedInUser>) -> Template { - let mut context = HashMap::new(); - context.insert("parent", "layout".to_string()); - context.insert("message", "Hello, World".to_string()); - for user in user { - context.insert("user", user.username); - } - Template::render("index", context) +#[get("/p/<username>")] +fn profile(conn: Db, username: String) -> Result<Template, Error> { + let mut context = default_context(); + context.insert("title".to_string(), json!(username)); + + let entries = db::get_entries(&*conn, &username)?; + let headings = + entries.first() + .and_then(|e| e.payload.as_object()) + .map(|e| e.keys().collect::<Vec<_>>()); + context.insert("headings".to_string(), json!(headings)); + context.insert( + "entries".to_string(), + json!(entries + .into_iter() + .map(|e| e.payload) + .collect::<Vec<_>>())); + + Ok(Template::render("profile", context)) +} + +#[get("/", rank = 1)] +fn index_logged_in(user: LoggedInUser) -> Redirect { + Redirect::to(uri!(profile: user.username)) +} + +#[get("/", rank = 2)] +fn index() -> Result<Template, Error> { + let mut context = default_context(); + context.insert("message".to_string(), json!("Hello, World")); + Ok(Template::render("index", context)) } #[get("/login?<failed>")] fn login(failed: bool) -> Template { - let mut context = HashMap::new(); - context.insert("parent", "layout"); + let mut context = default_context(); if failed { - context.insert("message", "Incorrect username or password"); + context.insert( + "message".to_string(), + json!("Incorrect username or password"), + ); } Template::render("login", context) } @@ -163,7 +196,8 @@ fn link_strava(params: State<Params>) -> Redirect { )) } -pub fn start(conn: diesel::PgConnection, db_url: &str, base_url: &str) { +pub fn start(conn: diesel::PgConnection, db_url: &str, base_url: &str, + static_path: &str) { let mut database_config = HashMap::new(); let mut databases = HashMap::new(); database_config.insert("url", Value::from(db_url)); @@ -196,13 +230,18 @@ pub fn start(conn: diesel::PgConnection, db_url: &str, base_url: &str) { "/", routes![ index, + index_logged_in, login, import_strava, + profile, login_submit, link_strava, link_strava_callback ], ) + .mount( + "/static", + StaticFiles::from(static_path)) .attach(Template::fairing()) .attach(Db::fairing()) .launch(); |