From c03d33310d582e3ff423ec788a47b53589e6ca11 Mon Sep 17 00:00:00 2001 From: Charles Taylor Date: Sun, 12 May 2024 18:57:26 -0400 Subject: [PATCH] Fix --- .gitignore | 2 +- deploy.sh | 6 ++--- fly.toml | 2 +- mprocs.yaml | 2 +- src/markup/base.rs | 16 +++++++++-- src/server/auth.rs | 19 +++++-------- upload-assets.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++ upload-css.js | 33 ----------------------- 8 files changed, 93 insertions(+), 53 deletions(-) create mode 100644 upload-assets.js delete mode 100644 upload-css.js diff --git a/.gitignore b/.gitignore index 256f8a4..127921a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /target/ /.env -/styles/ +/public/styles/ /node_modules/ diff --git a/deploy.sh b/deploy.sh index c5f9c9a..029a89c 100755 --- a/deploy.sh +++ b/deploy.sh @@ -4,13 +4,13 @@ set -eo pipefail export $(cat .env | xargs) # generate minified stylesheet -tailwindcss --input tailwind.source.css --output styles/index.min.css --minify +tailwindcss --input tailwind.source.css --output public/styles/index.css --minify # run supabase migrations supabase db push -# upload stylesheet to supabase cdn -node upload-css.js +# upload assets to supabase cdn +node upload-assets.js # deploy to citadels.fly.dev fly secrets set GIT_SHA=$(git show -s --format=%H) diff --git a/fly.toml b/fly.toml index dc02c3a..6662f8b 100644 --- a/fly.toml +++ b/fly.toml @@ -20,4 +20,4 @@ primary_region = "iad" [env] -_SUPABASE_PROJECT_URL="https://ryvsflpspddwwacxrnst.supabase.co" +SUPABASE_PROJECT_URL="https://ryvsflpspddwwacxrnst.supabase.co" diff --git a/mprocs.yaml b/mprocs.yaml index 8b2bf46..c749277 100644 --- a/mprocs.yaml +++ b/mprocs.yaml @@ -6,7 +6,7 @@ procs: shell: supabase db start Tailwind: - shell: tailwindcss --input tailwind.source.css --output styles/index.css --watch + shell: tailwindcss --input tailwind.source.css --output public/styles/index.css --watch autostart: true Docs: diff --git a/src/markup/base.rs b/src/markup/base.rs index 4b9cf1c..7acb64e 100644 --- a/src/markup/base.rs +++ b/src/markup/base.rs @@ -1,4 +1,16 @@ +use std::env; + use maud::{html, Markup, DOCTYPE}; +pub fn cdn(path: &'static str) -> String { + if cfg!(feature = "dev") { + format!("/public/{path}") + } else { + format!( + "{}/storage/v1/object/public/assets/{path}", + env::var("SUPABASE_PROJECT_URL").unwrap() + ) + } +} pub fn page(head: Markup, main: Markup) -> Markup { html! { @@ -8,8 +20,8 @@ pub fn page(head: Markup, main: Markup) -> Markup { title { "Citadels" } meta charset="utf-8"; link name="viewport" content="width=device-width, initial-scale=1"; - link rel="shortcut icon" href="/public/htmx.png"; - link rel="stylesheet" href="/styles/index.css"; + link rel="shortcut icon" href=(cdn("/htmx.png")); + link rel="stylesheet" href=(cdn("/styles/index.css")); (head) } body { diff --git a/src/server/auth.rs b/src/server/auth.rs index 2fff793..cda0fca 100644 --- a/src/server/auth.rs +++ b/src/server/auth.rs @@ -1,11 +1,7 @@ use super::supabase::SignInResponse; use crate::server::{state::AppState, supabase::EmailCreds}; use axum_extra::extract::{cookie::Cookie, PrivateCookieJar}; -use core::borrow; -use std::{ - borrow::{Borrow, Cow}, - collections::HashMap, -}; +use std::collections::HashMap; use uuid::Uuid; pub struct Session { @@ -67,14 +63,13 @@ pub async fn login( }; } None => { - log::info!("Setting new session_id cookie with 1 week expiry"); - let supabase = &state.supabase; - let session = supabase.signin_email(creds).await?; - let cookie = - Cookie::build((Cow::Borrowed("session_id"), Cow::Borrowed(&session.user_id))) - .max_age(time::Duration::WEEK); + let session = state.supabase.signin_email(creds).await?; + log::info!("Setting session cookie with 1 week expiry"); + cookies = cookies.add( + Cookie::build(("session_id", (session.user_id.clone()))) + .max_age(time::Duration::WEEK), + ); state.add_session(session).await; - cookies = cookies.add(cookie); } }; diff --git a/upload-assets.js b/upload-assets.js new file mode 100644 index 0000000..1e83cc6 --- /dev/null +++ b/upload-assets.js @@ -0,0 +1,66 @@ +import { createClient } from "@supabase/supabase-js"; +import fs from "node:fs"; +import path from "node:path"; +import process from "node:process"; + +// Create a single supabase client for interacting with your database +const url = "https://ryvsflpspddwwacxrnst.supabase.co"; + +const supabase = createClient(url, process.env.PROD_SUPABASE_SERVICE_ROLE_KEY); + +await supabase.storage + .createBucket("assets", { + public: true, + }) + .then(console.log); + +await supabase.storage + .from("assets") + .remove(["styles/index.css"]) + .then(console.log); +uploadDir("public"); + +function uploadDir(dir) { + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { + if (entry.isDirectory()) { + uploadDir(path.join(dir, entry.name)); + } else { + let filePath = path.join(entry.parentPath, entry.name); + let contents = fs.readFileSync(filePath); + const uploaded = supabase.storage + .from("assets") + .upload(filePath.substring(7), contents, { + contentType: mimeType(filePath), + }); + if (filePath.endsWith(".css")) { + console.log(uploaded); + } + } + } +} + +function mimeType(fileName) { + const ext = path.extname(fileName); + switch (ext) { + case ".txt": + return "text/plain;charset=UTF-8"; + case ".css": + return "text/css;charset=UTF-8"; + case ".mp3": + return "audio/mpeg"; + case ".wav": + return "audio/wav"; + case ".png": + return "image/x-png"; + case ".jpeg": + return "image/jpeg"; + case ".jpg": + return "image/jpeg"; + case ".pdf": + return "application/pdf"; + case ".js": + return "application/javascript"; + default: + throw new Error(`Unexpected file ext: ${ext}`); + } +} diff --git a/upload-css.js b/upload-css.js deleted file mode 100644 index 836f464..0000000 --- a/upload-css.js +++ /dev/null @@ -1,33 +0,0 @@ -import { createClient } from '@supabase/supabase-js' -import fs from 'node:fs'; - -// Create a single supabase client for interacting with your database -const url = "https://ryvsflpspddwwacxrnst.supabase.co"; - -const supabase = createClient(url, process.env.PROD_SUPABASE_SERVICE_ROLE_KEY); - -const create = await supabase - .storage - .createBucket('styles', { - public: true, - }); - -if (create.error) { - await supabase - .storage - .emptyBucket('styles').then(logResponse); -} - -const buffer = fs.readFileSync("styles/index.min.css"); -await supabase - .storage - .from('styles') - .upload('index.css', buffer, { - contentType: 'text/css;charset=UTF-8' - }).then(logResponse); - - -function logResponse({ data, error }) { - if (data) console.log(data); - if (error) console.error(error); -}