Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* remove golang

* Init fastapi (#11)

* inited fastapi

* updated .gitignore and added template .env file

* remove unused script in poetry

* add simple Readme

* update Readme

* added read different .env files based on command line args & update dockerfile
  • Loading branch information
owenowenisme authored Oct 27, 2024
1 parent ea97c23 commit 30bb5a1
Show file tree
Hide file tree
Showing 37 changed files with 1,201 additions and 425 deletions.
51 changes: 0 additions & 51 deletions backend/.air.toml

This file was deleted.

3 changes: 3 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
__pycache__
.venv
6 changes: 6 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.venv
.vscode
.DS_Store
__pycache__
db_data
.ruff_cache
12 changes: 12 additions & 0 deletions backend/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.6.9
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
36 changes: 19 additions & 17 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
FROM golang:1.23 AS builder
# Use an official Python runtime as a parent image
FROM python:3.11.10

# Set the working directory in the container
WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
gcc \
&& rm -rf /var/lib/apt/lists/*

COPY go.mod go.sum ./
RUN go mod tidy
# Install Poetry
RUN pip install poetry

# Copy only requirements to cache them in docker layer
COPY pyproject.toml poetry.lock* /app/

COPY . .
# Project initialization:
RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-ansi

RUN go build -o main .

FROM alpine:latest

WORKDIR /app

COPY --from=builder /app/main .

RUN apk add libc6-compat

EXPOSE 8080

CMD ["./main"]
# Copy project
COPY . /app

EXPOSE 8000

# Run the application
CMD ["poetry", "run", "python3", "main.py", "--mode", "prod"]
94 changes: 94 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# FastAPI Backend Project

## Table of Contents

- [FastAPI Backend Project](#fastapi-backend-project)
- [Table of Contents](#table-of-contents)
- [Prerequisites](#prerequisites)
- [Setup](#setup)
- [Running the Application](#running-the-application)
- [API Endpoints](#api-endpoints)
- [Development](#development)
- [Linting and Formatting](#linting-and-formatting)
- [Pre-commit Hooks](#pre-commit-hooks)

## Prerequisites
- Python=3.11.10
- Poetry [Installation Guide](https://python-poetry.org/docs/#installing-with-the-official-installer)
- Docker and Docker Compose

## Setup

1. Clone the repository:

2. Install dependencies using Poetry:
```
poetry install
```

3. Copy `template.env` to `.env` and fill in the required environment variables:

4. Edit the `.env` file with your specific configuration:
```
MINIO_ACCESS_KEY=your_minio_access_key
MINIO_SECRET_KEY=your_minio_secret_key
POSTGRES_USER=your_postgres_user
POSTGRES_PASSWORD=your_postgres_password
POSTGRES_DB=your_database_name
POSTGRES_IP=localhost
POSTGRES_PORT=5432
```
also add `.env.dev`
```
MINIO_ACCESS_KEY=your_minio_access_key
MINIO_SECRET_KEY=your_minio_secret_key
POSTGRES_USER=your_postgres_user
POSTGRES_PASSWORD=your_postgres_password
POSTGRES_DB=your_database_name
POSTGRES_IP=localhost
POSTGRES_PORT=5432
MODE= dev
```

## Running the Application

1. Start the PostgreSQL database using Docker Compose: (Assume that docker compose is installed)

```docker compose up postgres -d ```

2. Run the FastAPI application:
```
poetry run python3 main.py --mode dev
```

The application will be available at `http://localhost:8000`.

3. Access the API documentation at `http://localhost:8000/docs`.

## API Endpoints

For detailed API documentation, refer to the Swagger UI at `/docs` when the application is running.

## Development

### Linting and Formatting

This project uses Ruff for linting and formatting. To run the linter:

```
ruff check
```

To format the code:

```
ruff format
```

### Pre-commit Hooks

The project is configured with pre-commit hooks to ensure code quality. Install the hooks:

```
poetry run pre-commit install
```
33 changes: 24 additions & 9 deletions backend/compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
services:
minio:
image: minio/minio:latest
ports:
- "9090:9090"
- "9000:9000"
postgres:
image: postgres:latest
container_name: MyPostgres
volumes:
- ./minio:/data
- ./db_data:/var/lib/postgresql/data
ports:
- ${POSTGRES_PORT}:5432
environment:
- MINIO_ACCESS_KEY=${MINIO_ACCESS_KEY}
- MINIO_SECRET_KEY=${MINIO_SECRET_KEY}
command: minio server /data --console-address ":9000" --address ":9090"
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
networks:
- backend-network
backend:
build:
context: .
dockerfile: Dockerfile
depends_on:
- postgres
ports:
- 8000:8000
networks:
- backend-network

networks:
backend-network:
driver: bridge
Empty file added backend/core/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions backend/core/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os
from functools import lru_cache

from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=f'.env.{os.getenv("MODE","dev")}', extra='allow')


@lru_cache
def get_settings():
return Settings(_env_file=f'.env.{os.getenv("MODE","dev")}')
Empty file added backend/crud/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions backend/crud/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from db.db import get_db
from models.file import File
from schemas.file import File as FileSchema


class FileCRUD:
def create_file(file: FileSchema):
db = get_db()
db_file = File(filename=file.filename, uploader_id=file.uploader_id)
db.add(db_file)
db.commit()
db.close()
return {'status': 'success'}

def read_all_file():
db = get_db()
files = db.query(File).all()
db.close()
return {'status': 'success', 'files': files}
17 changes: 17 additions & 0 deletions backend/crud/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from core.config import get_settings


def get_env_info():
settings = get_settings()
return {
'minio_access_key': settings.minio_access_key,
'minio_secret_key': settings.minio_secret_key,
'postgres_user': settings.postgres_user,
'postgres_password': settings.postgres_password,
'postgres_db': settings.postgres_db,
'postgres_ip': settings.postgres_ip,
}


def health():
return {'status': 'ready'}
19 changes: 19 additions & 0 deletions backend/crud/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from db.db import get_db
from models.user import User
from schemas.user import User as UserSchema


class UserCRUD:
def create_user(user: UserSchema):
db = get_db()
db_user = User(username=user.username, password=user.password, email=user.email)
db.add(db_user)
db.commit()
db.close()
return {'status': 'success'}

def read_all_user():
db = get_db()
users = db.query(User).all()
db.close()
return {'status': 'success', 'users': users}
Empty file added backend/db/__init__.py
Empty file.
29 changes: 29 additions & 0 deletions backend/db/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from sqlalchemy import URL, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from core.config import get_settings
from models.file import File
from models.user import User


def get_db():
db = SessionLocal()
return db


def init_db():
settings = get_settings()
DATABASE_URL = URL.create(
'postgresql',
username=settings.postgres_user,
password=settings.postgres_password,
host=settings.postgres_ip,
port=settings.postgres_port,
database=settings.postgres_db,
)
engine = create_engine(DATABASE_URL)
global SessionLocal
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
Base.metadata.create_all(bind=engine, tables=[User.__table__, File.__table__])
Loading

0 comments on commit 30bb5a1

Please sign in to comment.