diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ea7f2ce --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.git* +**/*.pyc +.venv/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e3fbed3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +# syntax=docker/dockerfile:1 + +FROM python:3.11 + +WORKDIR /code + +COPY requirements.txt . + +RUN pip install --no-cache-dir --upgrade -r requirements.txt + +COPY . . + +EXPOSE 3100 + +CMD ["gunicorn", "--bind", "0.0.0.0:3100", "main:app", "--worker-class", "uvicorn.workers.UvicornWorker"] \ No newline at end of file diff --git a/app/database.py b/app/database.py new file mode 100644 index 0000000..33a305c --- /dev/null +++ b/app/database.py @@ -0,0 +1,10 @@ +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +DATABASE_URL = "sqlite:///./data.db" # Cambia por tu URL del PostgreSQL + +engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) +SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False) + +Base = declarative_base() \ No newline at end of file diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..51a5e44 --- /dev/null +++ b/app/main.py @@ -0,0 +1,22 @@ +from fastapi import FastAPI, Depends, HTTPException +from sqlalchemy.orm import Session +from . import models, schemas, database + +app = FastAPI() + +models.Base.metadata.create_all(bind=database.engine) + +def get_db(): + db = database.SessionLocal() + try: + yield db + finally: + db.close() + +@app.post("/registro") +def crear_registro(registro: schemas.RegistroCreate, db: Session = Depends(get_db)): + db_registro = models.Registro(name=registro.name) + db.add(db_registro) + db.commit() + db.refresh(db_registro) + return {"mensaje": f"Registro guardado para {registro.name}"} diff --git a/app/models.py b/app/models.py new file mode 100644 index 0000000..89062c4 --- /dev/null +++ b/app/models.py @@ -0,0 +1,8 @@ +from sqlalchemy import Column, Integer, String +from .database import Base + +class Registro(Base): + __tablename__ = "registros" + + id = Column(Integer, primary_key=True, index=True) + name = Column(String, index=True) \ No newline at end of file diff --git a/app/schemas.py b/app/schemas.py new file mode 100644 index 0000000..fa55a7c --- /dev/null +++ b/app/schemas.py @@ -0,0 +1,4 @@ +from pydantic import BaseModel + +class RegistroCreate(BaseModel): + name: str \ No newline at end of file diff --git a/data.db b/data.db new file mode 100644 index 0000000..d1038a3 Binary files /dev/null and b/data.db differ diff --git a/gunicorn.conf.py b/gunicorn.conf.py new file mode 100644 index 0000000..ed0c96c --- /dev/null +++ b/gunicorn.conf.py @@ -0,0 +1,12 @@ +# Gunicorn configuration file +import multiprocessing + +max_requests = 1000 +max_requests_jitter = 50 + +log_file = "-" + +bind = "0.0.0.0:3100" + +worker_class = "uvicorn.workers.UvicornWorker" +workers = (multiprocessing.cpu_count() * 2) + 1 \ No newline at end of file diff --git a/main.py b/main.py deleted file mode 100644 index 195f261..0000000 --- a/main.py +++ /dev/null @@ -1,34 +0,0 @@ -from fastapi import FastAPI, Form, Request, status -from fastapi.responses import HTMLResponse, FileResponse, RedirectResponse -from fastapi.staticfiles import StaticFiles -from fastapi.templating import Jinja2Templates -import uvicorn - - -app = FastAPI() -app.mount("/static", StaticFiles(directory="static"), name="static") -templates = Jinja2Templates(directory="templates") - -@app.get("/", response_class=HTMLResponse) -async def index(request: Request): - print('Request for index page received') - return templates.TemplateResponse('index.html', {"request": request}) - -@app.get('/favicon.ico') -async def favicon(): - file_name = 'favicon.ico' - file_path = './static/' + file_name - return FileResponse(path=file_path, headers={'mimetype': 'image/vnd.microsoft.icon'}) - -@app.post('/hello', response_class=HTMLResponse) -async def hello(request: Request, name: str = Form(...)): - if name: - print('Request for hello page received with name=%s' % name) - return templates.TemplateResponse('hello.html', {"request": request, 'name':name}) - else: - print('Request for hello page received with no name or blank name -- redirecting') - return RedirectResponse(request.url_for("index"), status_code=status.HTTP_302_FOUND) - -if __name__ == '__main__': - uvicorn.run('main:app', host='0.0.0.0', port=8000) - diff --git a/requirements.txt b/requirements.txt index 990070a..e30164c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,5 @@ fastapi uvicorn jinja2 python-multipart +gunicorn +sqlalchemy