Regenerate website on update

This commit is contained in:
Aditya Thakral 2021-04-06 01:05:10 -04:00
parent 5de03bf8d1
commit 39692a0333
6 changed files with 48 additions and 7 deletions

View File

View File

@ -4,6 +4,8 @@ from werkzeug.security import generate_password_hash, check_password_hash
import json
import sqlite3
import os
import subprocess
DB_PATH = os.path.join(os.path.dirname(__file__), 'links.db')
app = Flask(__name__)
@ -72,11 +74,16 @@ def update_links():
cur.executemany('INSERT INTO links VALUES (?,?,?,?,?)', links)
con.commit()
con.close()
data = regen_JSON()
# TODO: Trigger a rebuild of the frontend
outfile = open('data.json', 'w')
print(data, file=outfile)
outfile.close()
frontend_path = os.environ.get('FRONTEND_PATH', None)
if frontend_path is None:
raise Exception('FRONTEND_PATH is not defined')
update_command = f"cd '{frontend_path}' && ./update.sh"
status = subprocess.call(update_command, shell=True)
if status != 0:
raise Exception(f"`{update_command}` exited with an error ({status})")
except Exception as e:
cur.execute("rollback")

2
dev.sh
View File

@ -18,7 +18,7 @@ function dev_backend() {
cd ./backend
source venv/bin/activate
python main.py
FRONTEND_UPDATE_SCRIPT=$(realpath ./frontend/update.sh) python main.py
}
run_frontend_backend "dev_frontend" "dev_backend"

View File

@ -3,7 +3,7 @@ import { GetStaticProps } from "next";
import { Link, Links } from "components/Links";
export const getStaticProps: GetStaticProps<Props> = async () => {
const res = await fetch(`${process.env.DEV_URL}/links`);
const res = await fetch(`http://${process.env.SERVER_URL}/links`);
const links = await res.json();
return {

3
frontend/update.sh Executable file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
npm run build

31
prod.sh Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env bash
set -e
echo "Starting the python server..."
pushd backend
source venv/bin/activate
FRONTEND_PATH=$(realpath ../frontend) python main.py &
pid_backend=$!
trap_ctrlc() {
kill $pid_backend
}
trap trap_ctrlc INT
popd
sleep 3
echo "Building frontend..."
pushd frontend
npm run build
popd
echo "Done!"
wait