First commit. Added starter code for assignment.
This commit is contained in:
commit
5714a402a7
51
app/main.py
Normal file
51
app/main.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
from pydantic import BaseModel
|
||||||
|
import sqlite3
|
||||||
|
import os
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
DB_PATH = "/data/app.db"
|
||||||
|
|
||||||
|
class Item(BaseModel):
|
||||||
|
name: str
|
||||||
|
|
||||||
|
def init_db():
|
||||||
|
conn = sqlite3.connect(DB_PATH)
|
||||||
|
try:
|
||||||
|
c = conn.cursor()
|
||||||
|
c.execute("CREATE TABLE IF NOT EXISTS items (name TEXT)")
|
||||||
|
conn.commit()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
@app.on_event("startup")
|
||||||
|
def startup():
|
||||||
|
os.makedirs("/data", exist_ok=True)
|
||||||
|
init_db()
|
||||||
|
|
||||||
|
@app.get("/api/health")
|
||||||
|
def health():
|
||||||
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
@app.post("/api/items")
|
||||||
|
def add_item(item: Item):
|
||||||
|
conn = sqlite3.connect(DB_PATH)
|
||||||
|
try:
|
||||||
|
c = conn.cursor()
|
||||||
|
c.execute("INSERT INTO items VALUES (?)", (item.name,))
|
||||||
|
conn.commit()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
return {"message": "added"}
|
||||||
|
|
||||||
|
@app.get("/api/items")
|
||||||
|
def get_items():
|
||||||
|
conn = sqlite3.connect(DB_PATH)
|
||||||
|
try:
|
||||||
|
c = conn.cursor()
|
||||||
|
c.execute("SELECT name FROM items")
|
||||||
|
rows = c.fetchall()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
return {"items": [r[0] for r in rows]}
|
||||||
2
app/requirements.txt
Normal file
2
app/requirements.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
fastapi==0.115.6
|
||||||
|
uvicorn==0.30.6
|
||||||
24
docker-compose.yml
Normal file
24
docker-compose.yml
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
services:
|
||||||
|
api:
|
||||||
|
build:
|
||||||
|
context: ./app
|
||||||
|
container_name: fastapi_api
|
||||||
|
volumes:
|
||||||
|
- appdata:/data
|
||||||
|
expose:
|
||||||
|
- "8000"
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
nginx:
|
||||||
|
image: nginx:1.27-alpine
|
||||||
|
container_name: fastapi_nginx
|
||||||
|
depends_on:
|
||||||
|
- api
|
||||||
|
ports:
|
||||||
|
- "80:80"
|
||||||
|
volumes:
|
||||||
|
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
appdata:
|
||||||
27
nginx/nginx.conf
Normal file
27
nginx/nginx.conf
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
events {}
|
||||||
|
|
||||||
|
http {
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
|
||||||
|
# Optional: basic sanity endpoint at /
|
||||||
|
location / {
|
||||||
|
default_type text/plain;
|
||||||
|
return 200 "nginx ok\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
# Proxy all /api/* to the FastAPI service (service name DNS)
|
||||||
|
location /api/ {
|
||||||
|
proxy_pass http://api:8000;
|
||||||
|
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
|
||||||
|
# Keep typical proxy defaults sane
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_buffering off;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user