diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..4f6e47f --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,26 @@ +name: Check + +on: + push: + branches: [main, dev/*, fix/*, feature/*] + pull_request: + branches: [main, dev/*, fix/*, feature/*] + +jobs: + test-lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Flake8 + run: | + docker compose run --rm api sh -c "flake8" + + - name: Black + run: | + docker compose run --rm api sh -c "black ." diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..46f155c --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,22 @@ +name: Tests + +on: + push: + branches: [main, dev/*, fix/*, feature/*] + pull_request: + branches: [main, dev/*, fix/*, feature/*] + +jobs: + tests: + name: Test + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Test + run: | + docker compose run --rm api sh -c "pytest -v" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b7faf40 --- /dev/null +++ b/.gitignore @@ -0,0 +1,207 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[codz] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py.cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# UV +# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +#uv.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock +#poetry.toml + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python. +# https://pdm-project.org/en/latest/usage/project/#working-with-version-control +#pdm.lock +#pdm.toml +.pdm-python +.pdm-build/ + +# pixi +# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control. +#pixi.lock +# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one +# in the .venv directory. It is recommended not to include this directory in version control. +.pixi + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.envrc +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +# Abstra +# Abstra is an AI-powered process automation framework. +# Ignore directories containing user credentials, local state, and settings. +# Learn more at https://abstra.io/docs +.abstra/ + +# Visual Studio Code +# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore +# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore +# and can be added to the global gitignore or merged into this file. However, if you prefer, +# you could uncomment the following to ignore the entire vscode folder +# .vscode/ + +# Ruff stuff: +.ruff_cache/ + +# PyPI configuration file +.pypirc + +# Cursor +# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to +# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data +# refer to https://docs.cursor.com/context/ignore-files +.cursorignore +.cursorindexingignore + +# Marimo +marimo/_static/ +marimo/_lsp/ +__marimo__/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..955598d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM python:3.13.9-slim +LABEL maintainer="luisgsilva26@gmail.com" + +ENV PYTHONUNBUFFERED=1 + +COPY ./requirements.txt /tmp/requirements.txt +COPY ./src /src +WORKDIR /src +EXPOSE 8000 + +RUN python -m venv /py && \ + /py/bin/pip install --upgrade pip && \ + apt-get update && apt-get install -y \ + postgresql-client \ + libjpeg-dev \ + zlib1g-dev \ + libpq-dev && \ + /py/bin/pip install -r /tmp/requirements.txt + +ENV PATH="/py/bin:$PATH" \ No newline at end of file diff --git a/README.md b/README.md index 596e4f3..3ee75dc 100644 --- a/README.md +++ b/README.md @@ -1 +1,129 @@ -# MiBook-Server \ No newline at end of file +# 📚 MiBook-Server + +A modern RESTful API server built with FastAPI for managing family recipes and cookbook collections. + +## 🚀 Tech Stack + +- **FastAPI** - Modern, fast web framework for building APIs +- **PostgreSQL** - Powerful relational database +- **Redis** - In-memory cache for improved performance +- **Docker** - Containerization for easy deployment +- **Python 3.13** - Latest Python version + +## 📋 Prerequisites + +Before running this project, make sure you have installed: + +- 🐳 [Docker](https://www.docker.com/get-started) (version 20.10 or higher) +- 🐳 [Docker Compose](https://docs.docker.com/compose/install/) (version 2.0 or higher) + +## 🛠️ Installation & Setup + +1. **Clone the repository** + ```bash + git clone + cd MiBook-Server + ``` + +## 🚀 Running the Project + +### Using Docker Compose (Recommended) + +1. **Start all services** + ```bash + docker-compose up --build + ``` + +2. **The API will be available at:** + - 🌐 API: http://localhost:8000 + - 📊 Interactive API Docs: http://localhost:8000/docs + - 📚 Alternative API Docs: http://localhost:8000/redoc + +3. **Stop the services** + ```bash + docker-compose down + ``` + +### Running in Development Mode + +The project is configured to run in development mode with hot-reload enabled. Any changes you make to the code will automatically restart the server. + +## 📦 Services + +The project includes three main services: + +| Service | Port | Description | +|---------|------|-------------| +| 🚀 **API** | 8000 | FastAPI application server | +| 🗄️ **PostgreSQL** | 5432 | Database server | +| ⚡ **Redis** | 6379 | Cache server | + +## 🗂️ Project Structure + +``` +MiBook-Server/ +├── src/ +│ ├── main.py # FastAPI application entry point +│ └── ... +├── db-data/ # PostgreSQL data (auto-generated) +├── docker-compose.yml # Docker services configuration +├── Dockerfile # API service container configuration +├── requirements.txt # Python dependencies +└── README.md # Project documentation +``` + +## 🔧 Development + +### Running Tests + +```bash +docker-compose exec api pytest +``` + +### Code Formatting + +```bash +docker-compose exec api black . +``` + +### Linting + +```bash +docker-compose exec api flake8 +``` + +## 🌍 Environment Variables + +| Variable | Default | Description | +|----------|---------|-------------| +| `DB_HOST` | db | Database host | +| `DB_NAME` | family_recipes | Database name | +| `DB_USER` | postgres | Database user | +| `DB_PASS` | postgres | Database password | +| `DB_PORT` | 5432 | Database port | +| `DEBUG` | - | Enable debug mode | +| `SECRET_KEY` | - | Application secret key | +| `ALLOWED_HOSTS` | - | Allowed hosts for CORS | + +## 📝 API Endpoints + +- `GET /` - Welcome message +- `GET /docs` - Swagger UI documentation +- `GET /redoc` - ReDoc documentation + +## 🤝 Contributing + +Contributions are welcome! Please feel free to submit a Pull Request. + +## 📄 License + +This project is licensed under the terms specified in the LICENSE file. + +## 👤 Author + +**Luis Silva** +- Email: luisgsilva26@gmail.com + +--- + +Made with ❤️ using FastAPI \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..21c5396 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,45 @@ +services: + api: + build: + context: . + dockerfile: Dockerfile + ports: + - "8000:8000" + volumes: + - ./src:/src + command: sh -c "fastapi dev main.py --host 0.0.0.0 --port 8000" + depends_on: + - db + - cache + environment: + - DB_HOST=db + - DB_NAME=family_recipes + - DB_USER=postgres + - DB_PASS=postgres + - DB_PORT=5432 + - DEBUG=${DEBUG} + - SECRET_KEY=${SECRET_KEY} + - ALLOWED_HOSTS=${ALLOWED_HOSTS} + + db: + image: postgres:15.1-alpine + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - POSTGRES_DB=family_recipes + ports: + - "5432:5432" + volumes: + - ./db-data:/var/lib/postgresql/data + + cache: + image: redis:7.0-alpine + environment: + - REDIS_PASSWORD=redis + ports: + - "6379:6379" + volumes: + - redis-data:/data + +volumes: + redis-data: \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b821dbf --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +psycopg2-binary==2.9.11 +flake8==7.3.0 +black==25.9.0 +fastapi[standard]==0.128.0 +pytest==9.0.2 \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..c4df9f4 --- /dev/null +++ b/src/main.py @@ -0,0 +1,8 @@ +from fastapi import FastAPI + +app = FastAPI() + + +@app.get("/") +def read_root(): + return {"message": "Hello, MiBook!"} diff --git a/src/start_test.py b/src/start_test.py new file mode 100644 index 0000000..69abcd4 --- /dev/null +++ b/src/start_test.py @@ -0,0 +1,6 @@ +def inc(a, b): + return a + b + + +def test_inc(): + assert inc(1, 2) == 3