Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
klpulliam-37 committed May 2, 2023
2 parents 50bf4e7 + dd859af commit e2db401
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 39 deletions.
15 changes: 9 additions & 6 deletions .flaskenv
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
FLASK_APP=app
FLASK_DEBUG=1
DATABASE_URI=postgresql://adminadminSECURE!!!:C18BHHIT8EE32P0N06QIAV526@db:5432/greenwatch_production
SQL_HOST=db
FLASK_APP=app:app
FLASK_DEBUG=0
PRODUCTION=1
DBHOST=postgresql://adminadminSECURE!!!:ThisIsProductionBaby!!@db:5432/greenwatch_production
SQL_PORT=5432
SQL_HOST=db
DATABASE=postgres
POSTGRES_USER=adminadminSECURE!!!
POSTGRES_PASSWORD=C18BHHIT8EE32P0N06QIAV526
POSTGRES_DB=greenwatch_production
POSTGRES_PASSWORD=ThisIsProductionBaby!!
POSTGRES_DB=greenwatch_production
SECRET_KEY=317aed4a0352d37253f433dc475fa5e7e306de7ad657add546d34f4e7c8b046c
SERVER_IP="127.0.0.1:1337"
7 changes: 3 additions & 4 deletions .github/workflows/main_greenwatch.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
# More GitHub Actions for Azure: https://github.com/Azure/actions
# More info on Python, GitHub Actions, and Azure App Service: https://aka.ms/python-webapps-action
# More info on Python, GitHub Actions, and Azure App Service: https://aka.ms/python-webapps-actions

name: Build and deploy Python app to Azure Web App - greenwatch

Expand Down Expand Up @@ -28,7 +28,7 @@ jobs:
source venv/bin/activate
- name: Install dependencies
run: pip install -r Backend/Server/requirements.txt
run: pip install -r requirements.txt

# Optional: Add step to run tests here (PyTest, Django test suites, etc.)

Expand Down Expand Up @@ -60,5 +60,4 @@ jobs:
with:
app-name: 'greenwatch'
slot-name: 'Production'
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_E1130BCD4B124AB4B44F27DF5AB25E7B }}

publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_7E136301765E406B979F9F28392B8EA1 }}
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
5 changes: 0 additions & 5 deletions AzureNotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
# Interacting with Web App

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

## https://<app-name>.scm.azurewebsites.net/webssh/host
1 change: 0 additions & 1 deletion Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,3 @@ RUN chown -R app:app $APP_HOME

# change to the app user -- root no longer accessible
USER app

28 changes: 19 additions & 9 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,66 @@
# 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.
# 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["DATABASE_URI"]
production = bool(int(config["PRODUCTION"]))
db_uri = config["DBHOST"] if production else None

# factory pattern
def create_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_url 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)

migrate = Migrate(app, db)

api = Api(app)

app.config["JWT_SECRET_KEY"] = "127890032121005302028413019624734207168"
app.config["JWT_SECRET_KEY"] = config["SECRET_KEY"]
jwt = JWTManager(app)

@jwt.token_in_blocklist_loader
Expand Down
9 changes: 6 additions & 3 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:create_app()'
command: gunicorn --bind 0.0.0.0:5000 --workers=2 'wsgi:application'
expose:
- 5000
env_file:
Expand All @@ -17,10 +18,12 @@ services:
volumes:
- postgres_data_prod:/var/lib/postgresql/data/
env_file:
- ./.flaskenv
- .flaskenv

nginx:
build: ./services/nginx
build:
context: ./services/nginx
dockerfile: Dockerfile
ports:
- 1337:80
depends_on:
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
39 changes: 39 additions & 0 deletions greenwatch_key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-----BEGIN RSA PRIVATE KEY-----
MIIG4wIBAAKCAYEAyQxvcvLzx8xiqtoaKrC/bNeqPmujsCWM6s/28VCw03Y8zVNj
STitK3FodoaBWwl0g7+xn/tei7+qfaukfekFSzs9sLdDVHiNvaKCsnoamnaQQ4rD
sKWZ6V4YBXBmfX/mfK4haV2MXUrkA6CKNxtkMktcaEDMuJR4p5vpe2lBFLgkrzZ+
RYlVVr3GFINLjH6eKW4wPlRT0VVTm9wEUtzU0NDwwQPql69O8eUYY06XxrPpBLt2
1Aa+XJMzTn/B+b1H0+xUvJwctMKN2o8OkYZsjuvRB/SJDU2D/gX2SUPGci0D5B1u
BrL0uaf4DoSuV+D60nDGPrMdV2k85Bzq0YYLb7SZkoYN+4gAks6CAQEx7wpHq7C3
ncQM3VdDhYElNaexzXZ+HeghnI/3Yl1Fa5u3TsIhm+jK/qvae/MAcTrEdVBvwrgv
08C9ILIDuWfC8Di6LulKPAMXcP5xN5nFT6ouDWluff03PzAKOAppLBmn3d6tQGm+
3QTHYz114jJZDwI5AgMBAAECggGACbM55VfnfypTUF75iDnC4qN9KUnUOfl11RZ3
makE/w7jXBy725qKcGAmZCGVOCk6n+itz0Q3iZKTzUBszU5i3QBct5v/+aFfZVjj
6WyOwAkcaMXXLzyuMl5B1nFX4JI9MqLMJeNHlHC6k5o5UPNXYD3WlYka14UCVUPm
fffUFgXUb0e3y+8WWPsLBY5YJM0vIFyao0c6TA1L3Wc+JilVc0XBd+6ScQMaUf11
scTm+cVQZtQMpao1oQ3RGeZ8G2qQaLXhEQC3VmocdT5xJmQcXpacQh1mWHNm+9go
J4InA1L18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MAXegbmadCVgkB20S+gI76
+xr3RoNAS47Tq5taYgNweDEy9EuzQ/5szmmG55Ztx9QeFyTysMBUppuEarDvGS4l
AKhIqkdyDHaontaj8yvFtcNsaOH64ZicoPxdPEH36Ziw4D/XJA1YrMi04rgKcgCK
WuWqalRShkcvijDQ8xX/A4S6LH0KexNEX+P3BonJ4XMkJf2NAoHAJo3m2nQCMqod
sJArGeswIzT/OBAd5vb1FyUpHulexfBKG7hhDzXlwXXo7tkyf9DoKJvpaKSwEmPa
SS9SQTX3EiHSe5L5AFr2X93yFBOmOzSYXGerSeCUl7G1Gp4JYxfIid8b4muZ9xHS
LB4RLv0ZO55HIxGGPpaVa/M5CNQ5dzxSlS3qqFf+PlgkH4vOmat83r6bH1myXW3r
JVlB+N+ck+ijwTSFpJEfs9ZmS6Oy1MtPD5DqFziDUPvWkET+OUmb
-----END RSA PRIVATE KEY-----
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ flask-cors
requests
pyinstaller
psycopg2-binary
gunicorn
gunicorn
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
8 changes: 4 additions & 4 deletions services/nginx/nginx.conf → services/nginx/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ upstream greenwatch {
}

server {

listen 80;
server_name _;

location / {
proxy_pass http://greenwatch;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Prefix /;
}

}
1 change: 0 additions & 1 deletion testing/RouteTester.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ def test_routes(self, file:str, dump:bool=True, json_file_save:str=None) -> dict
test_results = test.test_routes(test_file_name, dump=True, json_file_save="test_results")

del test
#real = test.request("/login", "POST", {"username": "test", "password": "testtest"})



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 e2db401

Please sign in to comment.