DBTex API

May 27, 2025 ยท View on GitHub

A FastAPI-based backend service for managing users and conversations, backed by PostgreSQL, Docker, and GitHub Actions CI/CD.

Platform Demo


๐Ÿš€ Features

    • User Management: Secure user registration & retrieval with hashed passwords.
    • JWT Authentication: OAuth2 password flow using jose.
    • Conversations & Memories: CRUD endpoints for storing user messages and LLM responses.
    • Voting System: Upvote/downvote conversations.
    • Dockerized: Development & production Docker Compose configurations.
    • CI/CD: Automated tests, Docker image build & push, Heroku & Ubuntu deployments via GitHub Actions.
    • Thematic README: This file!

๐Ÿ“ฆ Project Structure

/home/siisi/dbtex-api
โ”œโ”€โ”€ app
โ”‚   โ”œโ”€โ”€ config.py         # Pydantic settings
โ”‚   โ”œโ”€โ”€ database.py       # SQLAlchemy engine & session
โ”‚   โ”œโ”€โ”€ main.py           # FastAPI app creation & startup
โ”‚   โ”œโ”€โ”€ models.py         # SQLAlchemy ORM models
โ”‚   โ”œโ”€โ”€ routers
โ”‚   โ”‚   โ”œโ”€โ”€ auth.py
โ”‚   โ”‚   โ”œโ”€โ”€ conversation.py
โ”‚   โ”‚   โ”œโ”€โ”€ user.py       # User endpoints (with unique-email handling)
โ”‚   โ”‚   โ””โ”€โ”€ vote.py
โ”‚   โ”œโ”€โ”€ schemas.py        # Pydantic request/response models
โ”‚   โ””โ”€โ”€ utils.py          # Hashing & verification helpers
โ”œโ”€โ”€ docker-compose-dev.yml # Dev compose (hot-reload, local Postgres)
โ”œโ”€โ”€ docker-compose-prod.yml# Prod compose (image pull, nginx proxy via systemd)
โ”œโ”€โ”€ Dockerfile            # Production image definition
โ”œโ”€โ”€ .env                  # Dev environment variables
โ”œโ”€โ”€ .env.prod             # Prod environment variables
โ”œโ”€โ”€ README.md             # โ† this file
โ””โ”€โ”€ .github/workflows
    โ””โ”€โ”€ build_deploy.yml  # CI: test, build, push, deploy

๐Ÿ”ง Local Development

  1. Clone

    git clone git@github.com:YOUR_USERNAME/dbtex-api.git
    cd dbtex-api
    
  2. Dev environment

    • Copy .env.example โ†’ .env and fill in your credentials.

    • Start services:

      docker compose -f docker-compose-dev.yml up --build
      
    • FastAPI available at http://localhost:8009 (mapped port)

    • Swagger UI: http://localhost:8009/docs

  3. Database reset (dev only):

    docker compose -f docker-compose-dev.yml down -v
    docker compose -f docker-compose-dev.yml up --build
    

โš™๏ธ Production Testing

  1. Prepare .env.prod

    cp .env.prod.example .env.prod
    # Fill in your production values
    
  2. Run compose

    docker compose -f docker-compose-prod.yml up -d --build
    
  3. Verify logs

    docker compose -f docker-compose-prod.yml logs -f
    
  4. Access via Nginx

    • Domain: https://dbtex-api.siisi.online
    • Ensure Nginx proxy targets the container port (e.g. 8001)

๐Ÿ›  CI/CD Pipeline

    • On push or PR:
    1. Run pytest
    2. Lint & type-check (optional)
    3. Build & push Docker image to Docker Hub
    4. Deploy to Heroku & Ubuntu via SSH

Edit secrets in GitHub settings: OPENAI_API_KEY, ADMIN_DATABASE_URL, DOCKER_HUB_USERNAME, etc.


๐Ÿ“ API Endpoints

MethodPathDescription
POST/users/Create user (409 if email exists)
GET/users/{id}Retrieve user by ID
POST/loginObtain JWT token
GET/conversation/List conversations
POST/conversation/Create a conversation memory
PUT/conversation/{id}Update a memory
DELETE/conversation/{id}Delete a memory
POST/vote/Upvote/downvote a memory

Refer to the automatic docs at /docs for full schema details.


๐Ÿ“š Further Improvements

  • Add pagination to list endpoints.
  • Integrate Redis for caching heavy queries.
  • Secure cookies & CSRF for browser-based clients.
  • Add OpenAPI tags grouping and descriptions.

๐Ÿ“„ License

MIT ยฉ 2025 Siisi