Skip to content

Commit

Permalink
Merge pull request #2 from iwanalabs/remove-pyodide
Browse files Browse the repository at this point in the history
Remove pyodide
  • Loading branch information
dylanjcastillo authored Feb 3, 2024
2 parents 3997260 + 25efd1a commit 0506190
Show file tree
Hide file tree
Showing 810 changed files with 68,004 additions and 375 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DJANGO_SECRET_KEY=
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,8 @@ dmypy.json
cython_debug/
node_modules/
.DS_Store

db.sqlite3-shm
db.sqlite3-wal

tmp/
1 change: 0 additions & 1 deletion CNAME

This file was deleted.

29 changes: 15 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
PROJECT_DIR=$(shell pwd)
PACKAGE_DIR=$(PROJECT_DIR)/src
SRC_DIR=$(PROJECT_DIR)/src
INPUT_DIR=$(PROJECT_DIR)/src/static/input
OUTPUT_DIR=$(PROJECT_DIR)/src/static/output

.PHONY: wheel
run:
cd ${SRC_DIR} && poetry run python manage.py runserver 8000

wheel:
poetry build --format wheel

run-django:
cd ${PACKAGE_DIR} && poetry run python manage.py runserver 5000

run-pyodide:
make wheel && python3 -m http.server

clear-migrations:
rm ${PACKAGE_DIR}/db.sqlite3 && cd ${PACKAGE_DIR} && find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
generate-key-no-django:
poetry run python -c 'import secrets; print(secrets.token_urlsafe(50))'

migrate:
cd ${PACKAGE_DIR} && poetry run python manage.py makemigrations && poetry run python manage.py migrate
cd ${SRC_DIR} && poetry run python manage.py makemigrations && poetry run python manage.py migrate

tailwind:
npx tailwindcss -i $(INPUT_DIR)/style.css -o $(OUTPUT_DIR)/style.css --watch -c tailwind.config.js --minify

collectstatic:
cd ${SRC_DIR} && poetry run python manage.py collectstatic

redis:
docker run --rm --name redis-server -p 6379:6379 -v ${PROJECT_DIR}/tmp:/data redis

clear-cache:
cd $(SRC_DIR) && poetry run python manage.py clear_cache
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,8 @@ Contributions are welcome! Please open an issue or pull request if you have a co
```bash
make run-django
```
5. Open http://localhost:5000/ in your browser.
6. Once you're happy with your changes, test that it works within Pyodide:
```bash
make run-pyodide
```
5. Open http://localhost:8000/ in your browser.
6. Make your changes and test them in your browser.
7. Commit your changes and open a pull request.

## License
Expand Down
38 changes: 0 additions & 38 deletions app.js

This file was deleted.

13 changes: 13 additions & 0 deletions deployment/add_cron_job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e

source config.sh

JOB="*/5 * * * * $PROJECT_DIR/.venv/bin/python $PROJECT_DIR/$PROJECT_SUBDIR/manage.py regenerate_data"

echo "Adding the following cron job:"
echo "$JOB"

(crontab -l | grep -q "$JOB") && echo "Cron job already exists" || (crontab -l 2>/dev/null; echo "$JOB") | crontab -

echo "Cron job added successfully"
15 changes: 15 additions & 0 deletions deployment/add_ssh_key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set -e

KEY_NAME="Components.pem"
ORIGIN_DIR="$HOME/Downloads"
ORIGIN_KEY="$ORIGIN_DIR/$KEY_NAME"
DEST_DIR="$HOME/.ssh"
DEST_KEY="$DEST_DIR/$KEY_NAME"

mkdir -p "$DEST_DIR"
mv "$ORIGIN_KEY" "$DEST_KEY"
chmod 600 "$DEST_KEY"

eval "$(ssh-agent -s)"
ssh-add "$DEST_KEY"
echo "SSH key '$KEY_NAME' has been setup successfully."
33 changes: 33 additions & 0 deletions deployment/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
APP_NAME=django-htmx-components
PROJECT_DIR=/home/ubuntu/django-htmx-components
PROJECT_SUBDIR=src
REPOSITORY_NAME=iwanalabs/django-htmx-components.git
SERVER_NAME=dhc.iwanalabs.com
PYTHON_VERSION=3.10

function confirm_action {
read -p "$1 [y/n] (y)" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]] || [[ -z $REPLY ]]
then
return 0
else
return 1
fi
}

function configure_bin_script() {
script_name="$1"
cp "${script_name}" "${PROJECT_DIR}/bin/${script_name}"
chmod u+x "${PROJECT_DIR}/bin/${script_name}"
sudo sed -i "s|APP_NAME|${APP_NAME}|g" "${PROJECT_DIR}/bin/${script_name}"
sudo sed -i "s|PROJECT_DIR|${PROJECT_DIR}|g" "${PROJECT_DIR}/bin/${script_name}"
sudo sed -i "s|PROJECT_SUBDIR|${PROJECT_SUBDIR}|g" "${PROJECT_DIR}/bin/${script_name}"
}

function create_supervisor_config() {
config_name="$1"
sudo cp "${config_name}.conf" "/etc/supervisor/conf.d/${APP_NAME}-${config_name}.conf"
sudo sed -i "s|APP_NAME|${APP_NAME}|g" "/etc/supervisor/conf.d/${APP_NAME}-${config_name}.conf"
sudo sed -i "s|PROJECT_DIR|${PROJECT_DIR}|g" "/etc/supervisor/conf.d/${APP_NAME}-${config_name}.conf"
}
8 changes: 8 additions & 0 deletions deployment/copy_to_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -e

SERVER_ADDRESS=components

echo "Copying files to server"
scp ./init.sh ubuntu@$SERVER_ADDRESS:~/
scp ./config.sh ubuntu@$SERVER_ADDRESS:~/
106 changes: 106 additions & 0 deletions deployment/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/bin/bash
set -e

source config.sh

if confirm_action "Do you want to create the logs and run directories?"
then
echo "Creating logs directory"
mkdir -p $PROJECT_DIR/logs;

echo "Creating run directory"
mkdir -p $PROJECT_DIR/run;
fi

if confirm_action "Do you want to create/update the gunicorn binary?"
then
echo "Creating gunicorn and binary"
mkdir -p $PROJECT_DIR/bin;

configure_bin_script "gunicorn_start"
fi

if confirm_action "Do you want to create/update the supervisor configs?"
then
echo "Creating gunicorn supervisor config"
create_supervisor_config "gunicorn_supervisor"
fi

if confirm_action "Do you want to create/update the nginx config?"
then
echo "Creating nginx config"
if confirm_action "Do you want to use the SSL enabled version?"
then
if confirm_action "Do you want to generate a certificate using Let's Encrypt?"
then
sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo certbot certonly --nginx -d $SERVER_NAME
sudo certbot renew -d $SERVER_NAME --dry-run
fi
sudo cp nginx_ssl.conf /etc/nginx/sites-available/$APP_NAME
else
sudo cp nginx.conf /etc/nginx/sites-available/$APP_NAME
fi
sudo sed -i "s|PROJECT_DIR|${PROJECT_DIR}|g" /etc/nginx/sites-available/$APP_NAME
sudo sed -i "s|SERVER_NAME|${SERVER_NAME}|g" /etc/nginx/sites-available/$APP_NAME
fi

if confirm_action "Do you want to create/update the aliases?"
then
echo "Removing old aliases"
# Remove existing aliases
sed -i '/alias gunicorn-logs/d' ~/.bashrc
sed -i '/alias gunicorn-bin/d' ~/.bashrc
sed -i '/alias gunicorn-conf/d' ~/.bashrc

sed -i '/alias nginx-error-logs/d' ~/.bashrc
sed -i '/alias nginx-access-logs/d' ~/.bashrc
sed -i '/alias nginx-conf/d' ~/.bashrc

sed -i '/alias restart-supervisor/d' ~/.bashrc
sed -i '/alias restart-app/d' ~/.bashrc
sed -i '/alias restart-nginx/d' ~/.bashrc

sed -i '/alias status-supervisor/d' ~/.bashrc
sed -i '/alias test-socket/d' ~/.bashrc
sed -i '/alias sudovim/d' ~/.bashrc

sed -i '/alias deploy-app/d' ~/.bashrc
sed -i '/alias cd-logs/d' ~/.bashrc

# Add new aliases
echo "Creating new aliases"

echo 'alias gunicorn-logs="tail -f '$PROJECT_DIR'/logs/gunicorn-error.log -n 10"' >> ~/.bashrc
echo 'alias gunicorn-bin="vim '$PROJECT_DIR'/bin/gunicorn_start"' >> ~/.bashrc
echo 'alias gunicorn-conf="sudo -E vim /etc/supervisor/conf.d/'$APP_NAME'-gunicorn_supervisor.conf"' >> ~/.bashrc

echo 'alias nginx-error-logs="tail -f '$PROJECT_DIR'/logs/nginx-error.log -n 10" ' >> ~/.bashrc
echo 'alias nginx-access-logs="tail -f '$PROJECT_DIR'/logs/nginx-access.log -n 10"' >> ~/.bashrc
echo 'alias nginx-conf="sudo -E vim /etc/nginx/sites-available/'$APP_NAME'"' >> ~/.bashrc

echo "alias restart-supervisor='sudo supervisorctl reread && sudo supervisorctl update'" >> ~/.bashrc
echo "alias restart-app='sudo supervisorctl restart "$APP_NAME"'" >> ~/.bashrc
echo "alias restart-nginx='sudo systemctl restart nginx'" >> ~/.bashrc

echo "alias test-socket='curl --unix-socket $PROJECT_DIR/run/gunicorn.sock localhost'" >> ~/.bashrc
echo "alias status-supervisor='sudo supervisorctl status'" >> ~/.bashrc
echo "alias deploy-app='cd $PROJECT_DIR/deployment && bash deploy.sh'" >> ~/.bashrc
echo "alias cd-logs='cd $PROJECT_DIR/logs'" >> ~/.bashrc
fi

# if users says yes, create a symlink to the nginx config
if confirm_action "Do you want to create a symlink to the nginx config in sites-enabled?"
then
# don't create a symlink if it already exists
if [ -L /etc/nginx/sites-enabled/$APP_NAME ]
then
echo "Symlink already exists"
exit 0
else
echo "Creating symlink"
sudo ln -s /etc/nginx/sites-available/$APP_NAME /etc/nginx/sites-enabled
fi
fi
29 changes: 29 additions & 0 deletions deployment/gunicorn_start
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

NAME=APP_NAME
DIR=PROJECT_DIR
APP_DIR=$DIR/PROJECT_SUBDIR
USER=ubuntu
GROUP=ubuntu
WORKERS=3
DJANGO_SETTINGS_MODULE=config.settings
DJANGO_WSGI_MODULE=config.wsgi
VENV=$DIR/.venv/bin/activate
BIND=unix:$DIR/run/gunicorn.sock
LOG_LEVEL=error
TIMEOUT=300

cd $APP_DIR
source $VENV

export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE

exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $WORKERS \
--user=$USER \
--group=$GROUP \
--bind=$BIND \
--log-level=$LOG_LEVEL \
--log-file=- \
--timeout $TIMEOUT
7 changes: 7 additions & 0 deletions deployment/gunicorn_supervisor.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[program:APP_NAME]
command=PROJECT_DIR/bin/gunicorn_start
user=ubuntu
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=PROJECT_DIR/logs/gunicorn-error.log
Loading

0 comments on commit 0506190

Please sign in to comment.