From 79aa32e8e4b0be778319a19f88d2e99f84c389a0 Mon Sep 17 00:00:00 2001 From: Edwin Eames Date: Sat, 16 Aug 2025 21:51:14 -0400 Subject: [PATCH] website online working --- Dockerfile => Dockerfile.dev | 0 Dockerfile.local | 37 ++++++++++++++++++ Dockerfile.prod | 18 ++++++++- app/__init__.py | 29 +++++++------- app/main/views.py | 16 +------- app/reports/views.py | 9 +---- config.py | 13 ++++--- nginx.conf | 17 ++++++++ local_settings.py => settings_dev.py | 8 ++-- settings_local.py | 58 ++++++++++++++++++++++++++++ prod_settings.py => settings_prod.py | 15 ++++--- start.sh | 8 ++++ 12 files changed, 175 insertions(+), 53 deletions(-) rename Dockerfile => Dockerfile.dev (100%) create mode 100644 Dockerfile.local create mode 100644 nginx.conf rename local_settings.py => settings_dev.py (96%) mode change 100755 => 100644 create mode 100755 settings_local.py rename prod_settings.py => settings_prod.py (83%) create mode 100644 start.sh diff --git a/Dockerfile b/Dockerfile.dev similarity index 100% rename from Dockerfile rename to Dockerfile.dev diff --git a/Dockerfile.local b/Dockerfile.local new file mode 100644 index 0000000..95bcdfe --- /dev/null +++ b/Dockerfile.local @@ -0,0 +1,37 @@ +FROM python:3.13.3-bullseye + +ENV PYTHONFAULTHANDLER=1 + +ENV PYTHONUNBUFFERED=1 + +ENV TZ=America/New_York + +ENV MODE="LOCAL" + +RUN mkdir -p /app + +COPY requirements.txt /app + +WORKDIR /app + +RUN pip3 install -r requirements.txt +RUN pip3 install gunicorn + +# Install Nginx +RUN apt-get update && apt-get install -y nginx && rm -rf /var/lib/apt/lists/* + +COPY . /app + +# Copy Nginx configuration +COPY nginx.conf /etc/nginx/sites-available/default + +# Enable the Nginx site +RUN ln -sf /etc/nginx/sites-available/default /etc/nginx/sites-enabled/ + +# Copy start script +COPY start.sh /app/start.sh +RUN chmod +x /app/start.sh + +EXPOSE 80 + +CMD ["/app/start.sh"] diff --git a/Dockerfile.prod b/Dockerfile.prod index 327f737..6c045a5 100644 --- a/Dockerfile.prod +++ b/Dockerfile.prod @@ -5,7 +5,6 @@ ENV PYTHONFAULTHANDLER=1 ENV PYTHONUNBUFFERED=1 ENV TZ=America/New_York -# RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone ENV MODE="PRODUCTION" @@ -16,8 +15,23 @@ COPY requirements.txt /app WORKDIR /app RUN pip3 install -r requirements.txt +RUN pip3 install gunicorn + +# Install Nginx +RUN apt-get update && apt-get install -y nginx && rm -rf /var/lib/apt/lists/* COPY . /app -CMD ["python", "app.py", "--host", "0.0.0.0"] +# Copy Nginx configuration +COPY nginx.conf /etc/nginx/sites-available/default +# Enable the Nginx site +RUN ln -sf /etc/nginx/sites-available/default /etc/nginx/sites-enabled/ + +# Copy start script +COPY start.sh /app/start.sh +RUN chmod +x /app/start.sh + +EXPOSE 80 + +CMD ["/app/start.sh"] diff --git a/app/__init__.py b/app/__init__.py index 0929ad0..253325b 100755 --- a/app/__init__.py +++ b/app/__init__.py @@ -39,12 +39,8 @@ app.jinja_env.autoescape = True # configuration -UPLOADED_FILES_DEST_ITEM = ApplicationConfig.UPLOADED_FILES_DEST_ITEM -UPLOADED_FILES_ALLOW = ApplicationConfig.UPLOADED_FILES_ALLOW -CURRENT_SETTINGS = ApplicationConfig.CURRENT_SETTINGS -WHITE = ApplicationConfig.WHITE -app.config['CORS_ORIGIN_WHITELIST'] = ApplicationConfig.CORS_ORIGIN_WHITELIST +app.config['CORS_ALLOWED_ORIGINS'] = ApplicationConfig.CORS_ALLOWED_ORIGINS app.config['UPLOADED_FILES_DEST_ITEM'] = ApplicationConfig.UPLOADED_FILES_DEST_ITEM app.config['UPLOADED_FILES_ALLOW'] = ApplicationConfig.UPLOADED_FILES_ALLOW app.config['MAX_CONTENT_LENGTH'] = ApplicationConfig.MAX_CONTENT_LENGTH @@ -54,7 +50,6 @@ app.config['SESSION_COOKIE_HTTPONLY'] = ApplicationConfig.SESSION_COOKIE_HTTPONL app.config['SESSION_COOKIE_SAMESITE'] = ApplicationConfig.SESSION_COOKIE_SAMESITE app.config['SESSION_PERMANENT'] = ApplicationConfig.SESSION_PERMANENT app.config['SESSION_USE_SIGNER'] = ApplicationConfig.SESSION_USE_SIGNER -app.config['ORIGIN_URL'] = ApplicationConfig.ORIGIN_URL app.config['CURRENT_SETTINGS'] = ApplicationConfig.CURRENT_SETTINGS app.config['SECRET_KEY'] = ApplicationConfig.SECRET_KEY @@ -99,15 +94,18 @@ def load_user_from_request(request): return None -api_main = { - "origins": [ApplicationConfig.ORIGIN_URL], - "methods": ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"], - "allow_headers": ['Authorization', 'application/json', 'authorization', 'Content-Type', - 'Access-Control-Allow-Headers', 'Origin,Accept', - 'X-Requested-With', 'Content-Type', 'Access-Control-Request-Method', - 'Access-Control-Request-Headers'] -} -cors = CORS(app, supports_credentials=True, resources={r'/*': api_main}) +# api_main = { +# "origins": [ApplicationConfig.ORIGIN_URL], +# "methods": ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"], +# "allow_headers": ['Authorization', 'application/json', 'authorization', 'Content-Type', +# 'Access-Control-Allow-Headers', 'Origin,Accept', +# 'X-Requested-With', 'Content-Type', 'Access-Control-Request-cMethod', +# 'Access-Control-Request-Headers'] +# } +cors = CORS(app, + supports_credentials=True, + resources={r"/*": {"origins": ApplicationConfig.CORS_ALLOWED_ORIGINS} + }) # bind a function after each request, even if an exception is encountered. @@ -171,7 +169,6 @@ app.register_blueprint(main_blueprint, url_prefix='/main') from .customer import customer as customer_blueprint app.register_blueprint(customer_blueprint, url_prefix='/customer') - from .delivery import delivery as delivery_blueprint app.register_blueprint(delivery_blueprint, url_prefix='/delivery') diff --git a/app/main/views.py b/app/main/views.py index f46a8f4..b2d2291 100755 --- a/app/main/views.py +++ b/app/main/views.py @@ -1,5 +1,5 @@ -from flask import jsonify, Response, request, url_for -from app import app, WHITE +from flask import jsonify, Response, url_for +from app import app @app.route("/favicon.ico") def favicon(): @@ -21,15 +21,3 @@ def static_from_root(): def index(): return jsonify({"success": "Api is online"}), 200 -# @app.after_request -# def add_cors_headers(response): -# r = request.referrer[:-1] -# if r in WHITE: -# response.headers.add('Access-Control-Allow-Origin', r) -# response.headers.add('Access-Control-Allow-Credentials', 'true') -# response.headers.add('Access-Control-Allow-Headers', 'Content-Type') -# response.headers.add('Access-Control-Allow-Headers', 'Cache-Control') -# response.headers.add('Access-Control-Allow-Headers', 'X-Requested-With') -# response.headers.add('Access-Control-Allow-Headers', 'Authorization') -# response.headers.add('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE') -# return response \ No newline at end of file diff --git a/app/reports/views.py b/app/reports/views.py index 45f4363..fa725de 100755 --- a/app/reports/views.py +++ b/app/reports/views.py @@ -1,15 +1,10 @@ -from flask import request, jsonify -from flask_login import current_user +from flask import jsonify from sqlalchemy.sql import func -from datetime import date, timedelta from app.reports import reports from app import db -from datetime import datetime -from app.classes.auth import Auth_User from app.classes.customer import Customer_Customer -from app.classes.employee import Employee_Employee from app.classes.delivery import Delivery_Delivery @@ -20,7 +15,7 @@ def oil_total_gallons(): .group_by(Delivery_Delivery.id)\ .all() - return jsonify({"ok": True }), 200 + return jsonify({"ok": True, "oil": total_oil }), 200 @reports.route("/customers/list", methods=["GET"]) def customer_list(): diff --git a/config.py b/config.py index 45e19a7..01e0280 100644 --- a/config.py +++ b/config.py @@ -5,16 +5,19 @@ def load_config(mode=os.environ.get('MODE')): try: print(f"mode is {mode}") if mode == 'PRODUCTION': - - from prod_settings import ApplicationConfig + from settings_prod import ApplicationConfig return ApplicationConfig - elif mode == 'DEVELOPMENT': - from local_settings import ApplicationConfig + elif mode == 'DEVELOPMENT': + from settings_dev import ApplicationConfig + return ApplicationConfig + + elif mode == 'LOCAL': + from settings_local import ApplicationConfig return ApplicationConfig else: pass except ImportError: - from local_settings import ApplicationConfig + from settings_local import ApplicationConfig return ApplicationConfig \ No newline at end of file diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..7f9253d --- /dev/null +++ b/nginx.conf @@ -0,0 +1,17 @@ +server { + listen 80; + server_name _; + + location = /favicon.ico { access_log off; log_not_found off; } + location /static/ { + root /app; + } + + location / { + proxy_pass http://127.0.0.1:8000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } +} diff --git a/local_settings.py b/settings_dev.py old mode 100755 new mode 100644 similarity index 96% rename from local_settings.py rename to settings_dev.py index 91671e9..0dbf686 --- a/local_settings.py +++ b/settings_dev.py @@ -44,10 +44,12 @@ class ApplicationConfig: # CORS - ORIGIN_URL = "*" + CORS_ALLOWED_ORIGINS = [ + "*" + ] CORS_SEND_WILDCARD = False CORS_SUPPORT_CREDENTIALS = True CORS_EXPOSE_HEADERS = None CORS_ALLOW_HEADERS = "*" - CORS_ORIGIN_WHITELIST = ['*'] - WHITE = ['*'] + + diff --git a/settings_local.py b/settings_local.py new file mode 100755 index 0000000..54d9ad1 --- /dev/null +++ b/settings_local.py @@ -0,0 +1,58 @@ + + +class ApplicationConfig: + """ + Basic Configuration for a generic User + """ + CURRENT_SETTINGS = 'LOCAL' + # databases info + POSTGRES_USERNAME = 'postgres' + POSTGRES_PW = 'password' + POSTGRES_SERVER = '192.168.1.204:5432' + POSTGRES_DBNAME00 = 'auburnoil' + SQLALCHEMY_DATABASE_URI = "postgresql+psycopg2://{}:{}@{}/{}".format(POSTGRES_USERNAME, + POSTGRES_PW, + POSTGRES_SERVER, + POSTGRES_DBNAME00 + ) + SQLALCHEMY_BINDS = {'auburnoil': SQLALCHEMY_DATABASE_URI} + # sqlalchemy config + SQLALCHEMY_TRACK_MODIFICATIONS = False + TRAP_HTTP_EXCEPTIONS = True + PROPAGATE_EXCEPTIONS = True + DEBUG = True + UPLOADED_FILES_DEST_ITEM = '/data/item' + + # file uploads + UPLOADED_FILES_ALLOW = ['png', 'jpeg', 'jpg', 'png', 'gif'] + MAX_CONTENT_LENGTH = 5 * 2500 * 2500 + ALLOWED_EXTENSIONS = ['png', 'jpeg', 'jpg', 'png', 'gif'] + + # secret keys + SECRET_KEY = "youwillneverguessthiskeycia" + + # sessions + # Available SESSION_TYPE options: 'redis', 'sqlalchemy', 'mongodb', 'filesystem', 'memcached' + SESSION_TYPE = "sqlalchemy" + SESSION_COOKIE_NAME = "eamco_session" + SESSION_COOKIE_SECURE = False + SESSION_COOKIE_HTTPONLY = True + REMEMBER_COOKIE_HTTPONLY = True + SESSION_COOKIE_SAMESITE = "Strict" + SESSION_PERMANENT = False + SESSION_USE_SIGNER = True + + # CORS + + CORS_SEND_WILDCARD = False + CORS_SUPPORT_CREDENTIALS = True + CORS_EXPOSE_HEADERS = None + CORS_ALLOW_HEADERS = "*" + CORS_ALLOWED_ORIGINS = [ + 'http://192.168.1.204:9610', + 'http://192.168.1.204:9611', + 'http://192.168.1.204:9612', + 'http://192.168.1.204:9613', + 'http://192.168.1.204:9614', + + ] \ No newline at end of file diff --git a/prod_settings.py b/settings_prod.py similarity index 83% rename from prod_settings.py rename to settings_prod.py index 8e56c0c..7457e07 100644 --- a/prod_settings.py +++ b/settings_prod.py @@ -4,7 +4,7 @@ class ApplicationConfig: """ Basic Configuration for a generic User """ - CURRENT_SETTINGS = 'PROD' + CURRENT_SETTINGS = 'PRODUCTION' # databases info POSTGRES_USERNAME = 'postgres' POSTGRES_PW = 'password' @@ -15,12 +15,12 @@ class ApplicationConfig: POSTGRES_SERVER, POSTGRES_DBNAME00 ) - SQLALCHEMY_BINDS = {'eamco': SQLALCHEMY_DATABASE_URI} + SQLALCHEMY_BINDS = {'auburnoil': SQLALCHEMY_DATABASE_URI} # sqlalchemy config SQLALCHEMY_TRACK_MODIFICATIONS = False TRAP_HTTP_EXCEPTIONS = True PROPAGATE_EXCEPTIONS = True - DEBUG = True + DEBUG = False UPLOADED_FILES_DEST_ITEM = '/data/item' # file uploads @@ -43,10 +43,13 @@ class ApplicationConfig: # CORS - ORIGIN_URL = "http://192.168.1.204:9511" + + CORS_SEND_WILDCARD = False CORS_SUPPORT_CREDENTIALS = True CORS_EXPOSE_HEADERS = None CORS_ALLOW_HEADERS = "*" - CORS_ORIGIN_WHITELIST = ['http://192.168.1.204','http://localhost', 'http://192.168.1.204:9511', "*"] - WHITE= ['http://192.168.1.204','http://localhost', "*"] + CORS_ALLOWED_ORIGINS = [ + 'https://oil.edwineames.com', + 'https://edwineames.com' + ] \ No newline at end of file diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..a697749 --- /dev/null +++ b/start.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e + +# Start Gunicorn +gunicorn --bind 127.0.0.1:8000 --workers 4 --timeout 120 app:app & + +# Start Nginx +nginx -g 'daemon off;'