Skip to content

Commit

Permalink
Synchronize backend/frontend (#251)
Browse files Browse the repository at this point in the history
* Synchronizing the backend and frontend so that the frontend can be served on port 8000

* Adding new docker file

* Changing github actions

* Changing the installation script

* Formatting

* Making changes to move npm commands to Dockerfile

* Changing the ci_container.yml file

* Adding build directory

* Removing the build directory

* Adding an if statement to check the existence of the mounting directory

* Removing a comment

* Formatting

* Adding unit test

* Writing unit tests and setting up the static folder

* Formatting
  • Loading branch information
sylviamclaughlin authored Sep 13, 2023
1 parent 820bdbe commit 858f0a1
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
uses: aws-actions/amazon-ecr-login@2fc7aceee09e9e4a7105c0d060c656fad0b4f63d # v1.7.0

- name: Build container
working-directory: ./app
working-directory: ./
run: |
docker build \
--build-arg git_sha=$GITHUB_SHA \
Expand Down Expand Up @@ -70,6 +70,6 @@ jobs:
uses: cds-snc/security-tools/.github/actions/generate-sbom@cfec0943e40dbb78cee115bbbe89dc17f07b7a0f # v2.1.3
with:
docker_image: "${{ env.REGISTRY }}/sre-bot:latest"
dockerfile_path: "app/Dockerfile"
dockerfile_path: "./Dockerfile"
sbom_name: "sre-bot"
token: "${{ secrets.GITHUB_TOKEN }}"
2 changes: 1 addition & 1 deletion .github/workflows/ci_container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0

- name: Build container
working-directory: ./app
working-directory: ./
run: |
docker build \
--build-arg git_sha=$GITHUB_SHA \
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker_vulnerability_scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
uses: cds-snc/security-tools/.github/actions/docker-scan@cfec0943e40dbb78cee115bbbe89dc17f07b7a0f # v2.1.3
with:
docker_image: "${{ env.REGISTRY }}/sre-bot:latest"
dockerfile_path: "app/Dockerfile"
dockerfile_path: "Dockerfile"
token: "${{ secrets.GITHUB_TOKEN }}"

- name: Logout of Amazon ECR
Expand Down
16 changes: 12 additions & 4 deletions app/Dockerfile → Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ FROM python:3.11.5-slim

RUN apt-get update \
&& apt-get install -y wget \
&& apt-get install -y nodejs \
npm \
&& rm -rf /var/lib/apt/lists/*

WORKDIR frontend/
COPY frontend/ .

RUN npm install
RUN npm run build

WORKDIR /app

# Set build variables
ARG git_sha
ENV GIT_SHA=$git_sha

COPY requirements.txt ./
COPY app/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .
COPY app/ .

ARG LICENSE_KEY

Expand All @@ -24,6 +32,6 @@ RUN cp /app/geodb/GeoLite2-City_*/GeoLite2-City.mmdb /app/geodb/GeoLite2-City.mm
RUN rm -rf /app/geodb/GeoLite2-City_*
RUN rm /app/geodb/GeoLite2-City.tar.gz

COPY bin/entry.sh /app/entry.sh
COPY app/bin/entry.sh /app/entry.sh

ENTRYPOINT [ "/app/entry.sh" ]
ENTRYPOINT [ "/app/entry.sh" ]
1 change: 1 addition & 0 deletions app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ geoip2==4.7.0
google-api-python-client==2.97.0
google-auth-httplib2==0.1.0
google-auth-oauthlib==0.8.0
Jinja2==3.1.2
PyYaml==5.3.1 # need to pin down the PyYaml version since the newest version is currently broken - https://github.com/yaml/pyyaml/issues/724
python-dotenv==0.21.1
python-i18n==0.3.9
Expand Down
22 changes: 22 additions & 0 deletions app/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel, Extra
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from models import webhooks
from commands.utils import log_ops_message, log_to_sentinel
from integrations import maxmind
Expand Down Expand Up @@ -63,6 +65,19 @@ class Config:

handler = FastAPI()

# Set up the templates directory and static folder for the frontend with the build folder for production
if os.path.exists("../frontend/build"):
# Sets the templates directory to the React build folder
templates = Jinja2Templates(directory="../frontend/build")
# Mounts the static folder within the build forlder to the /static route.
handler.mount(
"/static", StaticFiles(directory="../frontend/build/static"), "static"
)
else:
# Sets the templates directory to the React public folder for local dev
templates = Jinja2Templates(directory="../frontend/public")
handler.mount("/static", StaticFiles(directory="../frontend/public"), "static")


@handler.get("/geolocate/{ip}")
def geolocate(ip):
Expand Down Expand Up @@ -215,3 +230,10 @@ def append_incident_buttons(payload, webhook_id):
}
]
return payload


# Defines a route handler for `/*` essentially.
# NOTE: this needs to be the last route defined b/c it's a catch all route
@handler.get("/{rest_of_path:path}")
async def react_app(req: Request, rest_of_path: str):
return templates.TemplateResponse("index.html", {"request": req})
8 changes: 8 additions & 0 deletions app/tests/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,11 @@ def test_append_incident_buttons():
]
),
]


# Unit test the react app
def test_react_app():
# test the react app
response = client.get("/some/path")
assert response.status_code == 200
assert "text/html" in response.headers["content-type"]
3 changes: 2 additions & 1 deletion frontend/src/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import sre_bot_logo from './sre_bot_logo.png';
import './App.css';

function App() {
return (
<div className="App">
<header className="App-header">
<img src = "sre_bot_logo.png" alt="sre_bot"></img>
<img src = {sre_bot_logo} alt="sre_bot"></img>
<h1>SRE Bot Admin</h1>
</header>
</div>
Expand Down
File renamed without changes

0 comments on commit 858f0a1

Please sign in to comment.