Skip to content

Commit

Permalink
Finalized production orchestration, now on to bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
gramcracker40 committed May 2, 2023
1 parent 3161ebc commit dd859af
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 41 deletions.
11 changes: 7 additions & 4 deletions .flaskenv
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
FLASK_APP=app:app
FLASK_DEBUG=0
DBHOST=postgresql://adminadminSECURE!!!:ThisIsProductionBaby$@db:5432/greenwatch_production
PRODUCTION=1
DBHOST=postgresql://adminadminSECURE!!!:ThisIsProductionBaby!!@db:5432/greenwatch_production
SQL_PORT=5432
SQL_HOST=db
DATABASE=postgres
DBUSER=adminadminSECURE!!!
DBPASS=ThisIsProductionBaby$
DBNAME=greenwatch_production
POSTGRES_USER=adminadminSECURE!!!
POSTGRES_PASSWORD=ThisIsProductionBaby!!
POSTGRES_DB=greenwatch_production
SECRET_KEY=317aed4a0352d37253f433dc475fa5e7e306de7ad657add546d34f4e7c8b046c
SERVER_IP="127.0.0.1:1337"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

#SSH Key file for Azure VM
greenwatch_key.pem
6 changes: 0 additions & 6 deletions AzureNotes.md
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
# Interacting with Web App

## below url opens ssh session with container instance in cloud given appropriate credentials in azure.

## https://greenwatch.scm.azurewebsites.net/webssh/host

2 changes: 1 addition & 1 deletion UserInterface/api/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Utils from "../js/utilities.js";

const url = "https://greenwatch.azurewebsites.net"
const url = "http://127.0.0.1:1337"
const token = `Bearer ${sessionStorage.getItem('access_token')}`;
let debugMode = false;

Expand Down
24 changes: 17 additions & 7 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,58 @@
# goes and reads the docker-compose.yml file and determines how to setup the development environment based off of the steps
# listed in the markup.
#
#
# DOCUMENTATION:
# While the development flask server is running on your local machine or in the cloud you can find the documentation
# for the API by going to 127.0.0.1:5000/swagger-ui, this is a well written piece of documentation that goes over exactly
# how to make each call to the server...

# Core
import os

# External
from werkzeug.middleware.proxy_fix import ProxyFix
from flask import Flask, jsonify
from flask_smorest import Api
from flask_jwt_extended import JWTManager
from flask_migrate import Migrate
from flask_cors import CORS

# Internal
from db import db
from blocklist import BLOCKLIST

from resources.greenhouse import blp as GreenhouseBlueprint
from resources.user import blp as UserBlueprint
from resources.room import blp as RoomBlueprint
from resources.experiment import blp as ExperimentBlueprint
from resources.server import blp as ServerBlueprint
from resources.ui import blp as UserInterfaceBlueprint

# Environment variables
from dotenv import dotenv_values
config = dotenv_values(".flaskenv")

db_ = config["DBHOST"]
production = bool(int(config["PRODUCTION"]))
db_uri = config["DBHOST"] if production else None

# factory pattern
def app(db_url=None):
# factory pattern --> .flaskenv FLASK_APP, allows for simple "flask run"
# command when running the app locally
def app():
app = Flask(__name__)
CORS(app)

if production: # If deploying multiple domains with proxy servers you would need change
app.wsgi_app = ProxyFix(
app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1
)

app.config["PROPAGATE_EXCEPTIONS"] = True
app.config["API_TITLE"] = "GreenWatch REST API"
app.config["API_VERSION"] = "v1"
app.config["OPENAPI_VERSION"] = "3.0.3"
app.config["OPENAPI_URL_PREFIX"] = "/"
app.config["OPENAPI_SWAGGER_UI_PATH"] = "/swagger-ui"
app.config["OPENAPI_SWAGGER_UI_PATH"] = "/swagger-ui" # Provides a very user friendly documentations
app.config["OPENAPI_SWAGGER_UI_URL"] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist/"
app.config["SQLALCHEMY_DATABASE_URI"] = db_ or "sqlite:///data.db"
app.config["SQLALCHEMY_DATABASE_URI"] = db_uri or "sqlite:///data.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)

Expand Down
5 changes: 3 additions & 2 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ version: '3.8'
services:
web:
build:
context: ./
dockerfile: Dockerfile.prod
command: gunicorn --bind 0.0.0.0:5000 --workers=2 'app:app'
command: gunicorn --bind 0.0.0.0:5000 --workers=2 'wsgi:application'
expose:
- 5000
env_file:
Expand All @@ -17,7 +18,7 @@ services:
volumes:
- postgres_data_prod:/var/lib/postgresql/data/
env_file:
- ./.flaskenv
- .flaskenv

nginx:
build:
Expand Down
6 changes: 2 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ services:
image: postgres:13-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=hello_flask
- POSTGRES_PASSWORD=hello_flask
- POSTGRES_DB=hello_flask_dev
env_file:
- .flaskenv

volumes:
postgres_data:
Expand Down
2 changes: 1 addition & 1 deletion services/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM nginx:1.23-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
COPY default.conf /etc/nginx/conf.d
16 changes: 16 additions & 0 deletions services/nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
upstream greenwatch {
server web:5000;
}

server {
listen 80;
server_name _;

location / {
proxy_pass http://greenwatch;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Prefix /;
}
}
16 changes: 0 additions & 16 deletions services/nginx/nginx.conf

This file was deleted.

8 changes: 8 additions & 0 deletions wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# +++++++++++ FLASK +++++++++++
# bind any WSGI server to this module instead of create_app()
# create_app() is good with flask run cause it handles everything for you
# when you switch to developments you must setup the WSGI server and bind
# to this module. See --> docker-compose.prod.yml

from app import app
application = app()

0 comments on commit dd859af

Please sign in to comment.