Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Dockerfile +39 -0
- app.py +88 -0
- requirements.txt +3 -0
Dockerfile
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use a base image
|
2 |
+
FROM ubuntu:20.04
|
3 |
+
|
4 |
+
# Set environment variable to non-interactive mode
|
5 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
6 |
+
|
7 |
+
# Install PostgreSQL and dependencies
|
8 |
+
RUN apt-get update && apt-get install -y \
|
9 |
+
postgresql postgresql-contrib \
|
10 |
+
python3 python3-pip \
|
11 |
+
tzdata && \
|
12 |
+
rm -rf /var/lib/apt/lists/*
|
13 |
+
|
14 |
+
# Reset DEBIAN_FRONTEND
|
15 |
+
ENV DEBIAN_FRONTEND=interactive
|
16 |
+
|
17 |
+
# Set environment variables for PostgreSQL
|
18 |
+
ENV POSTGRES_DB=mydatabase
|
19 |
+
ENV POSTGRES_USER=myuser
|
20 |
+
ENV POSTGRES_PASSWORD=mypassword
|
21 |
+
|
22 |
+
# Install application dependencies
|
23 |
+
COPY requirements.txt /app/requirements.txt
|
24 |
+
RUN pip3 install -r /app/requirements.txt
|
25 |
+
|
26 |
+
# Copy FastAPI application code
|
27 |
+
COPY app.py /app/app.py
|
28 |
+
|
29 |
+
# Set up the PostgreSQL database and user
|
30 |
+
RUN service postgresql start && \
|
31 |
+
su - postgres -c "psql -c \"CREATE USER myuser WITH PASSWORD 'mypassword';\"" && \
|
32 |
+
su - postgres -c "createdb mydatabase" && \
|
33 |
+
su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;\""
|
34 |
+
|
35 |
+
# Expose PostgreSQL and FastAPI ports
|
36 |
+
EXPOSE 5432 8080
|
37 |
+
|
38 |
+
# Start PostgreSQL and FastAPI app together
|
39 |
+
CMD service postgresql start && cd app && uvicorn app:app --host 0.0.0.0 --port 8080
|
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
import psycopg2
|
3 |
+
from psycopg2.extras import RealDictCursor
|
4 |
+
import os
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
# Database connection details
|
9 |
+
DB_NAME = os.getenv("POSTGRES_DB", "mydatabase")
|
10 |
+
DB_USER = os.getenv("POSTGRES_USER", "myuser")
|
11 |
+
DB_PASSWORD = os.getenv("POSTGRES_PASSWORD", "mypassword")
|
12 |
+
DB_HOST = os.getenv("POSTGRES_HOST", "localhost")
|
13 |
+
DB_PORT = os.getenv("POSTGRES_PORT", "5432")
|
14 |
+
|
15 |
+
def get_db_connection():
|
16 |
+
try:
|
17 |
+
conn = psycopg2.connect(
|
18 |
+
dbname=DB_NAME,
|
19 |
+
user=DB_USER,
|
20 |
+
password=DB_PASSWORD,
|
21 |
+
host=DB_HOST,
|
22 |
+
port=DB_PORT,
|
23 |
+
cursor_factory=RealDictCursor # So we can return rows as dictionaries
|
24 |
+
)
|
25 |
+
return conn
|
26 |
+
except Exception as e:
|
27 |
+
print(f"Error connecting to the database: {e}")
|
28 |
+
raise HTTPException(status_code=500, detail="Database connection error")
|
29 |
+
|
30 |
+
def create_table():
|
31 |
+
conn = get_db_connection()
|
32 |
+
cursor = conn.cursor()
|
33 |
+
cursor.execute("""
|
34 |
+
CREATE TABLE IF NOT EXISTS items (
|
35 |
+
id SERIAL PRIMARY KEY,
|
36 |
+
name VARCHAR(50) NOT NULL,
|
37 |
+
description TEXT
|
38 |
+
);
|
39 |
+
""")
|
40 |
+
conn.commit()
|
41 |
+
cursor.close()
|
42 |
+
conn.close()
|
43 |
+
|
44 |
+
|
45 |
+
@app.get("/")
|
46 |
+
def read_root():
|
47 |
+
return {"message": "Welcome to FastAPI with PostgreSQL!"}
|
48 |
+
|
49 |
+
@app.get("/items/")
|
50 |
+
def read_items():
|
51 |
+
conn = get_db_connection()
|
52 |
+
cursor = conn.cursor()
|
53 |
+
try:
|
54 |
+
cursor.execute("SELECT * FROM items;")
|
55 |
+
except Exception as e:
|
56 |
+
print(e)
|
57 |
+
create_table()
|
58 |
+
cursor.execute("SELECT * FROM items;")
|
59 |
+
items = cursor.fetchall()
|
60 |
+
cursor.close()
|
61 |
+
conn.close()
|
62 |
+
|
63 |
+
return items
|
64 |
+
|
65 |
+
@app.post("/items/")
|
66 |
+
def create_item(name: str, description: str = "description"):
|
67 |
+
conn = get_db_connection()
|
68 |
+
cursor = conn.cursor()
|
69 |
+
create_table()
|
70 |
+
|
71 |
+
try:
|
72 |
+
cursor.execute(
|
73 |
+
"INSERT INTO items (name, description) VALUES (%s, %s) RETURNING id;",
|
74 |
+
(name, description)
|
75 |
+
)
|
76 |
+
except Exception as e:
|
77 |
+
print(e)
|
78 |
+
create_table()
|
79 |
+
cursor.execute(
|
80 |
+
"INSERT INTO items (name, description) VALUES (%s, %s) RETURNING id;",
|
81 |
+
(name, description)
|
82 |
+
)
|
83 |
+
item_id = cursor.fetchone()['id']
|
84 |
+
conn.commit()
|
85 |
+
cursor.close()
|
86 |
+
conn.close()
|
87 |
+
|
88 |
+
return {"id": item_id, "name": name, "description": description}
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
psycopg2-binary
|