Skip to content

Commit

Permalink
Merge pull request #849 from hackclub/vercel-fix
Browse files Browse the repository at this point in the history
Vercel fix
  • Loading branch information
recursiveforte authored Aug 22, 2024
2 parents b0cb077 + f4ba4c1 commit ee14375
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 145 deletions.
58 changes: 58 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import express from 'express';

import signUpEmail from "../backend/api/signUpEmail.js";
import checkSignIn from "../backend/api/checkSignIn.js";
import saveFile from "../backend/api/saveFile.js";
import getUser from "../backend/api/getUser.js";
import submitCode from "../backend/api/submitCode.js";
import getFiles from "../backend/api/getFiles.js";
import createShareLink from "../backend/api/createShareLink.js";
import logout from "../backend/api/logout.js";
import deleteFile from "../backend/api/deleteFile.js";

import { supabase } from "../backend/api/supabase.js";

const app = express();

app.use(express.json());

app.post('/signUpEmail', signUpEmail);
app.post('/check-signed-in', checkSignIn);
app.post('/get-files', getFiles);
app.post('/save-file', saveFile);
app.post('/delete-file', deleteFile);
app.post('/logout', logout);
app.post('/get-user', getUser);
app.post('/submit-code', submitCode);
app.post('/create-share-link', createShareLink);
app.get('/read-share-link', async (req, res) => {
const { id } = req.query;

try {

console.log("check in database");
// check if email is in database
let { data: file, error: fileError } = await supabase
.from('share_link')
.select('*')
.eq('id', id)
.single();

if (fileError && fileError.message !== 'No rows found') {
res.send("no share link here");
return;
}

res.send(file.content);
} catch (error) {
res.status(500).send({ error: error.message });
}
});
app.get('/assembly', (req, res) => {
res.redirect('https://github.com/hackclub/blot/blob/main/docs/assembly/ASSEMBLY.md');
});
app.get('/welcome-qr-code', (req, res) => {
res.redirect('/assembly');
});

export default app;
44 changes: 44 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import inlineWorkerPlugin from 'esbuild-plugin-inline-worker';

import { spawn } from 'child_process';

import { wrapHTML } from "./backend/wrapHTML.js";

import navBar from "./backend/pages/navBar.js";
import guides from "./backend/pages/guides.js";
import gallery from "./backend/pages/gallery.js";
import landing from "./backend/pages/landing.js";
import docs from "./backend/pages/docs.js";

spawn('npx', ['tailwindcss', '-i', './styles.css', '-o', './dist/styles.css']);

Expand Down Expand Up @@ -92,3 +99,40 @@ export async function build(htmls) {
await fs.cpSync("./public", OUTPUT_DIR, { recursive: true });
// console.timeEnd("COPY")
}

build({
index: wrapHTML(`
${navBar(true)}
${landing()}
`),
docs: wrapHTML(`
${navBar()}
${docs()}
`),
editor: wrapHTML(`
<!-- TODO: add automatically when building -->
<link rel="stylesheet" href='./assets/initApp.css'>
<main></main>
<script type="module" src="./src/initApp.js"></script>
`),
guides: wrapHTML(`
${navBar()}
${guides()}
`),
gallery: wrapHTML(`
${navBar()}
${gallery()}
`),
404: wrapHTML(`
${navBar()}
<div class="p-2">nothing here</div>
`),
test: wrapHTML(`
${navBar()}
<div class="bg-red-400">test another page</div>
`),

});
39 changes: 39 additions & 0 deletions dev_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import app from './api/index.js'
import './build.js'

import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

import express from 'express';
import path from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

app.use(express.static(join(__dirname, 'dist')));

app.use((req, res, next) => {
let pathName = req.path;

if (pathName === "/") pathName = "/index";

if (req.path.indexOf('.') === -1) {
const file = path.join(__dirname, 'dist', `${pathName}.html`);
res.sendFile(file, (err) => {
if (err) next();
});
} else {
next();
}
});

app.use((req, res, next) => {
const file = path.join(__dirname, 'dist', `404.html`);
res.status(404).sendFile(file);
});

const port = process.env.PORT || 3000;

app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"type": "module",
"version": "0.0.1",
"scripts": {
"start": "node server.js",
"start": "node dev_server.js",
"build": "node build.js",
"dev": "nodemon -r dotenv/config server.js"
"dev": "nodemon -r dotenv/config dev_server.js"
},
"nodemonConfig": {
"watch": [
Expand Down
143 changes: 0 additions & 143 deletions server.js

This file was deleted.

5 changes: 5 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": 2,
"cleanUrls": true,
"rewrites": [{ "source": "/(.*)", "destination": "/api" }]
}

0 comments on commit ee14375

Please sign in to comment.