-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
32 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,30 @@ | ||
//! HTTP endpoints. | ||
use crate::shared::AppState; | ||
use crate::shared::{AppState, UserInfo, SESSION_USER_INFO_KEY}; | ||
use anyhow::Result; | ||
use axum::{extract::State, http::StatusCode, response::Html}; | ||
use minijinja::context; | ||
use serde::{Deserialize, Serialize}; | ||
use std::sync::Arc; | ||
use tower_sessions::Session; | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
struct Counter(usize); | ||
pub async fn handler_404( | ||
State(state): State<Arc<AppState>>, | ||
session: Session, | ||
) -> Result<Html<String>, StatusCode> { | ||
let user_info: Option<UserInfo> = session.get(SESSION_USER_INFO_KEY).await.unwrap(); | ||
let template = state.templates.get_template("404").unwrap(); | ||
let rendered = template.render(context! { user_info }).unwrap(); | ||
|
||
Ok(Html(rendered)) | ||
} | ||
|
||
pub async fn handler_home( | ||
State(state): State<Arc<AppState>>, | ||
session: Session, | ||
) -> Result<Html<String>, StatusCode> { | ||
let counter: Counter = session.get("COUNTER").await.unwrap().unwrap_or_default(); | ||
let user_info: Option<UserInfo> = session.get(SESSION_USER_INFO_KEY).await.unwrap(); | ||
let template = state.templates.get_template("home").unwrap(); | ||
let rendered = template | ||
.render(context! { | ||
title => "Home", | ||
welcome_text => "Hello World!", | ||
counter => counter, | ||
}) | ||
.unwrap(); | ||
let rendered = template.render(context! { user_info }).unwrap(); | ||
|
||
Ok(Html(rendered)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- prettier-ignore --> | ||
{% extends "layout" %} | ||
|
||
{% block title %}Not found | {{ super() }}{% endblock %} | ||
|
||
<!-- prettier-ignore --> | ||
{% block body %} | ||
|
||
<h1>Something went wrong</h1> | ||
<h4>That page couldn't be found.</h4> | ||
{% endblock %} |