-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor app, make ready for authentication
- Loading branch information
glendc
committed
Oct 22, 2023
1 parent
6f8ab2f
commit eaf6039
Showing
14 changed files
with
110 additions
and
79 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,38 +1,18 @@ | ||
use std::path::PathBuf; | ||
|
||
use axum::Router; | ||
use shuttle_secrets::SecretStore; | ||
use tower::ServiceBuilder; | ||
use tower_http::{ | ||
compression::CompressionLayer, normalize_path::NormalizePathLayer, services::ServeDir, | ||
trace::TraceLayer, | ||
}; | ||
|
||
mod auth; | ||
mod router; | ||
mod services; | ||
|
||
#[shuttle_runtime::main] | ||
async fn axum(#[shuttle_secrets::Secrets] secret_store: SecretStore) -> shuttle_axum::ShuttleAxum { | ||
// TODO: | ||
// - add `authorizer to state` ??? | ||
// - make it compile again... | ||
|
||
let _auth = auth::Auth::new( | ||
let auth = services::Auth::new( | ||
secret_store.get("AUTH_PRIVATE_KEY").unwrap(), | ||
secret_store.get("AUTHORIZED_EMAILS").unwrap(), | ||
secret_store.get("SENDGRID_API_KEY").unwrap(), | ||
); | ||
|
||
let router = Router::new() | ||
.nest_service("/static", ServeDir::new(PathBuf::from("static"))) | ||
.nest("/", router::new()) | ||
.fallback(router::not_found::any) | ||
.layer( | ||
ServiceBuilder::new() | ||
.layer(TraceLayer::new_for_http()) | ||
.layer(CompressionLayer::new()) | ||
.layer(NormalizePathLayer::trim_trailing_slash()), | ||
); | ||
let state = router::State { auth }; | ||
let router = router::new(state); | ||
|
||
Ok(router.into()) | ||
} |
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,21 @@ | ||
use axum::http::HeaderMap; | ||
|
||
pub async fn get_robots_txt() -> &'static str { | ||
r"User-agent: * | ||
Allow: / | ||
Sitemap: https://bckt.xyz/sitemap.xml | ||
" | ||
} | ||
|
||
pub async fn get_sitemap_xml() -> impl axum::response::IntoResponse { | ||
let body = r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<url> | ||
<loc>https://bckt.xyz/</loc> | ||
</url> | ||
</urlset>"#; | ||
|
||
let mut headers = HeaderMap::new(); | ||
headers.insert("content-type", "application/xml".parse().unwrap()); | ||
|
||
(headers, body) | ||
} |
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,46 +1,50 @@ | ||
use std::{path::PathBuf, sync::Arc}; | ||
|
||
use axum::{ | ||
http::HeaderMap, | ||
response::IntoResponse, | ||
routing::{get, post}, | ||
Router, | ||
}; | ||
use tower::ServiceBuilder; | ||
use tower_http::{ | ||
compression::CompressionLayer, normalize_path::NormalizePathLayer, services::ServeDir, | ||
trace::TraceLayer, | ||
}; | ||
|
||
mod index; | ||
mod link; | ||
mod login; | ||
mod memory; | ||
mod not_found; | ||
mod redirect; | ||
mod shared; | ||
|
||
pub mod not_found; | ||
|
||
pub async fn get_robots_txt() -> &'static str { | ||
r"User-agent: * | ||
Allow: / | ||
Sitemap: https://bckt.xyz/sitemap.xml | ||
" | ||
} | ||
|
||
pub async fn get_sitemap_xml() -> impl IntoResponse { | ||
let body = r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<url> | ||
<loc>https://bckt.xyz/</loc> | ||
</url> | ||
</urlset>"#; | ||
|
||
let mut headers = HeaderMap::new(); | ||
headers.insert("content-type", "application/xml".parse().unwrap()); | ||
|
||
(headers, body) | ||
#[derive(Debug, Clone)] | ||
pub struct State { | ||
pub auth: crate::services::Auth, | ||
} | ||
|
||
pub fn new() -> Router { | ||
fn new_root(state: State) -> Router { | ||
Router::new() | ||
.route("/", get(index::get)) | ||
.route("/robots.txt", get(get_robots_txt)) | ||
.route("/sitemap.xml", get(get_sitemap_xml)) | ||
.route("/robots.txt", get(memory::get_robots_txt)) | ||
.route("/sitemap.xml", get(memory::get_sitemap_xml)) | ||
.route("/link", get(link::get)) | ||
.route("/link", post(link::post)) | ||
.route("/login", get(login::get)) | ||
.route("/login", post(login::post)) | ||
.route("/:hash", get(redirect::get)) | ||
.with_state(Arc::new(state)) | ||
} | ||
|
||
pub fn new(state: State) -> Router { | ||
Router::new() | ||
.nest_service("/static", ServeDir::new(PathBuf::from("static"))) | ||
.nest("/", new_root(state)) | ||
.fallback(not_found::any) | ||
.layer( | ||
ServiceBuilder::new() | ||
.layer(TraceLayer::new_for_http()) | ||
.layer(CompressionLayer::new()) | ||
.layer(NormalizePathLayer::trim_trailing_slash()), | ||
) | ||
} |
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,9 +1,16 @@ | ||
use askama::Template; | ||
use axum::{ | ||
body::Body, | ||
http::{Request, StatusCode}, | ||
}; | ||
|
||
#[derive(Template)] | ||
#[template(path = "../templates/404.html")] | ||
pub struct GetTemplate; | ||
use crate::router::shared::ErrorTemplate; | ||
|
||
pub async fn any() -> GetTemplate { | ||
GetTemplate | ||
pub async fn any(request: Request<Body>) -> (StatusCode, ErrorTemplate) { | ||
( | ||
StatusCode::NOT_FOUND, | ||
ErrorTemplate { | ||
title: "404 — Not Found".to_string(), | ||
message: format!("The page '{}' does not exist.", request.uri().path()), | ||
}, | ||
) | ||
} |
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,4 +1,4 @@ | ||
#[derive(Debug)] | ||
#[derive(Debug, Clone)] | ||
pub struct Auth; | ||
|
||
// TODO implement using | ||
|
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,2 @@ | ||
mod auth; | ||
pub use auth::Auth; |
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,15 @@ | ||
function init() { | ||
console.log('hello'); | ||
|
||
document.body.addEventListener('htmx:beforeSwap', (evt) => { | ||
console.log('beforeSwap', evt); | ||
if ([400, 404].includes(evt.detail.xhr.status)) { | ||
evt.detail.shouldSwap = true; | ||
evt.detail.isError = false; | ||
} | ||
}); | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", init); | ||
|
||
export { }; |
This file was deleted.
Oops, something went wrong.
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
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,10 +1,10 @@ | ||
{% extends "base.html" %} | ||
{% block content %} | ||
<div class="box bad" style="width: 100%; max-width: 500px;"> | ||
<strong class="block titlebar" style="color: var(--bg); text-shadow: none;">{{ title }}</strong> | ||
<p>{{ message }}</p> | ||
<strong class="block titlebar" style="color: var(--custom-error-text); text-shadow: none;">{{ title }}</strong> | ||
<p style="color: var(--bg);">{{ message }}</p> | ||
<p> | ||
<a href="/" class="<button> big" style="margin: 10px 0 0 0; float: right;">↩ go back</a> | ||
<a href="/" class="<button> big" style="margin: 10px 0 0 0; float: right;" autofocus>↩ go back</a> | ||
</p> | ||
</div> | ||
{% endblock %} |
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