Skip to content

Commit 4404899

Browse files
committed
June 17th update group chat
1 parent 14edd67 commit 4404899

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
__pycache__

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
FROM python:3.9-slim-bullseye
22

3-
RUN python -m pip install fastapi "uvicorn[standard]" jinja2 aiosql
3+
RUN apt update -y && apt install build-essential libpq-dev -y
4+
5+
RUN python -m pip install fastapi "uvicorn[standard]" jinja2 aiosql psycopg2-binary --no-binary psycopg2-binary python-dotenv
46

57
WORKDIR /workspace
68

queries.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- name: get-all-companies
2+
-- Get a list of all of the companies.
3+
SELECT company_name FROM companies;

templates/companies.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
<head>
3+
<title>HOME</title>
4+
</head>
5+
<body>
6+
<ul>
7+
{% for company in payload1 %}
8+
<li>{{ company }}</li>
9+
{% endfor %}
10+
</ul>
11+
</body>
12+
</html>

website/main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from fastapi import FastAPI
22
from fastapi.templating import Jinja2Templates
33
from fastapi.requests import Request
4+
from dotenv import load_dotenv
5+
import psycopg2
6+
import aiosql
7+
import os
8+
49

510
app = FastAPI()
611
templates = Jinja2Templates(directory='templates/')
@@ -17,3 +22,23 @@ async def root(request: Request):
1722
response = templates.TemplateResponse("index.html", {"request": request, "payload1": hello_world})
1823
return response
1924

25+
@app.get("/companies")
26+
async def get_companies(request: Request):
27+
load_dotenv()
28+
29+
conn = psycopg2.connect(f"dbname=tdm_main user=rdonly password={os.getenv('PG_PASSWORD')} host=db.tdm.geddes.rcac.purdue.edu")
30+
queries = aiosql.from_path("queries.sql", "psycopg2")
31+
32+
companies = [v[0] for v in queries.get_all_companies(conn)]
33+
print(type(companies))
34+
print(companies)
35+
36+
accept = request.headers.get("accept")
37+
hello_world = {"message": "hello world"}
38+
39+
if accept.split("/")[1] == 'json':
40+
return hello_world
41+
42+
if len(accept.split(",")) > 1 or accept.split("/")[1] == 'html':
43+
response = templates.TemplateResponse("companies.html", {"request": request, "payload1": companies})
44+
return response

0 commit comments

Comments
 (0)