Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesTaylor7 committed May 12, 2024
1 parent 647622f commit c03d333
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target/
/.env
/styles/
/public/styles/
/node_modules/
6 changes: 3 additions & 3 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ primary_region = "iad"


[env]
_SUPABASE_PROJECT_URL="https://ryvsflpspddwwacxrnst.supabase.co"
SUPABASE_PROJECT_URL="https://ryvsflpspddwwacxrnst.supabase.co"
2 changes: 1 addition & 1 deletion mprocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 14 additions & 2 deletions src/markup/base.rs
Original file line number Diff line number Diff line change
@@ -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! {
Expand All @@ -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 {
Expand Down
19 changes: 7 additions & 12 deletions src/server/auth.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
}
};

Expand Down
66 changes: 66 additions & 0 deletions upload-assets.js
Original file line number Diff line number Diff line change
@@ -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}`);
}
}
33 changes: 0 additions & 33 deletions upload-css.js

This file was deleted.

0 comments on commit c03d333

Please sign in to comment.