aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs51
1 files changed, 48 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs
index ed943c7..acc9f9f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -9,6 +9,8 @@ use axum::body;
use axum::extract::Path;
use axum::{error_handling::HandleErrorLayer, routing::get, BoxError, Extension, Router};
use axum::response::{Html, IntoResponse, Response};
+use axum_extra::extract::PrivateCookieJar;
+use axum_extra::extract::cookie::Key;
use errors::{StringResult, HtmlResult};
use hyper::StatusCode;
use sqlx::{PgPool, postgres::PgPoolOptions};
@@ -17,6 +19,8 @@ use tower_http::trace::TraceLayer;
use tracing::{error, info, debug};
use crate::errors::ServiceError;
use tracing_subscriber::prelude::*;
+use crate::models::DbUser;
+use crate::handlers::auth::get_user_or_403;
pub struct State {
pub config: config::Config,
@@ -49,6 +53,8 @@ async fn main() {
let shared_state = Arc::new(State { config, conn });
+ let key = load_or_gen_keypair().unwrap();
+
let app = Router::new()
.route("/health", get(health_check))
.route("/", get(index))
@@ -70,7 +76,8 @@ async fn main() {
.layer(TraceLayer::new_for_http())
.into_inner(),
)
- .layer(Extension(shared_state));
+ .layer(Extension(shared_state))
+ .layer(Extension(key));
let addr = match SocketAddr::from_str(&bind_addr) {
Ok(a) => a,
@@ -93,9 +100,14 @@ async fn health_check() -> &'static str {
}
#[axum_macros::debug_handler]
-async fn index() -> HtmlResult {
+async fn index(state: Extension<Arc<State>>, jar: PrivateCookieJar) -> HtmlResult {
+ let mut conn = state.conn.acquire().await?;
+ let user: Option<DbUser> = match get_user_or_403(jar, &mut conn).await {
+ Ok(u) => Some(u),
+ Err(_) => None,
+ };
let mut buf = Vec::new();
- crate::templates::index_html(&mut buf).unwrap();
+ crate::templates::index_html(&mut buf, user).unwrap();
match String::from_utf8(buf) {
Ok(s) => Ok(Html(s)),
@@ -124,4 +136,37 @@ async fn statics(Path(name): Path<String>) -> Result<Response, ServiceError> {
}
}
+use std::fs::{self, File};
+fn load_or_gen_keypair() -> Result<Key, ServiceError> {
+ let kp: Key;
+ let mut file = match File::open(".keypair") {
+ Ok(f) => f,
+ Err(_) => {
+ debug!("File does not exist, creating at .keypair");
+ File::create(".keypair").unwrap()
+ }
+ };
+ if let Ok(c) = fs::read(".keypair") {
+ if c.len() == 0 {
+ debug!("No keypair found. Generating...");
+ let key = Key::generate();
+ fs::write(".keypair", key.master().as_ref()).unwrap();
+ debug!("Written keypair {:?} to .keypair", key.master().as_ref());
+ kp = key;
+ } else {
+ debug!("Found keypair file, contents: {:?}", c);
+ kp = Key::from(&c);
+ debug!("Loaded keypair from file");
+ }
+ } else {
+ debug!("Generating new keypair");
+ let key = Key::generate();
+ fs::write(".keypair", key.master().as_ref()).unwrap();
+ debug!("Written keypair {:?} to .keypair", key.master().as_ref());
+ kp = key;
+ }
+ Ok(kp)
+}
+
+
include!(concat!(env!("OUT_DIR"), "/templates.rs"));