From ab42fff18537c45535657a5491bb7c9fd87c43dc Mon Sep 17 00:00:00 2001 From: jessebot Date: Thu, 30 May 2024 11:15:17 +0200 Subject: [PATCH] change app.py to be __init__.py and update dockerfile and readme accordingly --- Dockerfile | 3 ++- README.md | 25 +++++++++++++++---- tiny_personal_website/{app.py => __init__.py} | 9 ++++--- 3 files changed, 28 insertions(+), 9 deletions(-) rename tiny_personal_website/{app.py => __init__.py} (87%) diff --git a/Dockerfile b/Dockerfile index e068c28..c0d0862 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,4 +38,5 @@ RUN addgroup --gid 1000 app && \ USER 1000 # CMD ["/app/.venv/bin/gunicorn", "--bind", ":80", "app:app"] -CMD ["/app/.venv/bin/gunicorn" , "-b", "0.0.0.0:8080", "app:app"] +# CMD ["/app/.venv/bin/gunicorn" , "-b", "0.0.0.0:8080", "app:app"] +CMD ["/app/.venv/bin/gunicorn" , "-b", "0.0.0.0:8080", "tiny_personal_website:app"] diff --git a/README.md b/README.md index c7edb7e..86ad5a2 100644 --- a/README.md +++ b/README.md @@ -23,14 +23,29 @@ Clone this github repo into your desired webroot, and install dependencies with You can configure everything (e.g. website title, your photo, quote, etc) by editing `tiny_personal_website/config/config.yaml` and replacing all the Jesse data with your own. +### Testing Locally + +```bash +# get into a poetry shell +poetry shell + +# run gunicorn +gunicorn tiny_personal_website:app +``` + +Then you can go to http://127.0.0.1:8000 in a browser to view your changes. + +#### Docker + For testing locally with docker, you can do: ```bash -docker build . -t +# this tag can be anything, but this is typically what I do locally +docker build . -t jessebot/tiny-personal-website:dev # to test locally, you can do -p 8000:8080 to forward # port 8080 on the container to port 8000 on your local machine -docker run --rm -p 8000:8080 +docker run --rm -p 8000:8080 jessebot/tiny-personal-website:dev ``` Then you can go to http://127.0.0.1:8000 in a browser to view your changes. @@ -40,7 +55,7 @@ You can now use an environment variable to set the location of the `config.yaml` ```bash # mount the current directory to /config, and set the CONFIG_FILE env var to /config/config.yaml # this assumes you've built or pulled jessebot/tiny-personal-website:latest locally -docker run --rm -v .:/config -e CONFIG_FILE=/config/config.yaml -p 8000:8080 jessebot/tiny-personal-website:latest +docker run --rm -v .:/config -e CONFIG_FILE=/config/config.yaml -p 8000:8080 jessebot/tiny-personal-website:dev ``` ### Deploying on an app platform @@ -49,7 +64,7 @@ You want the following command plugged into where-ever this runs (e.g. digital ocean app platform): ```bash -gunicorn --worker-tmp-dir /dev/shm app:app +gunicorn --worker-tmp-dir /dev/shm tiny_personal_website:app ``` -And the container port of note is port 8080. +And the container port of note is port `8080`. diff --git a/tiny_personal_website/app.py b/tiny_personal_website/__init__.py similarity index 87% rename from tiny_personal_website/app.py rename to tiny_personal_website/__init__.py index 33aef49..b3327a6 100755 --- a/tiny_personal_website/app.py +++ b/tiny_personal_website/__init__.py @@ -5,17 +5,20 @@ from flask import Flask from flask import render_template import logging as log from os import environ as env +from os import path import sys import yaml +PWD = path.dirname(__file__) +# get config file location from env vars +CONFIG_FILE = env.get("CONFIG_FILE", f"{PWD}/config/config.yaml") + # set logging log.basicConfig(stream=sys.stderr, level=log.INFO) log.info("tiny personal website logging config loaded") -# get config file location from env vars -CONFIG_FILE = env.get("CONFIG_FILE", "./config/config.yaml") -def get_config_variables(): +def get_config_variables() -> dict: """ Gets config.yaml variables from YAML file. Returns dict. """