Skip to content

Commit

Permalink
project structure modified
Browse files Browse the repository at this point in the history
  • Loading branch information
suriya-mca committed Apr 30, 2024
1 parent b8a965d commit a053143
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 96 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/env
/app/__pycache__
/app/router/__pycache__
/app/schema/__pycache__
/app/schema/__pycache__
/app/service/__pycache__
/conf/__pycache__
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ WORKDIR /app

# Copy current directory contents into the container at /app
COPY ./app /app
COPY ./conf/gunicorn.conf.py /etc/gunicorn/gunicorn.conf.py
COPY ./requirements.txt /app

# Install dependencies
Expand All @@ -21,5 +22,5 @@ RUN pip install --upgrade pip && \
# Expose ports and define startup commands based on selected framework
EXPOSE 8000

# Specify the entry point (Replace "mail" with your actual file name)
CMD ["uvicorn", "main:app", "--host=0.0.0.0", "--reload"]
# Specify the entry point
CMD ["gunicorn", "main:app", "-c", "/etc/gunicorn/gunicorn.conf.py"]
7 changes: 4 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from router import qr_code_generator
from fastapi.responses import UJSONResponse

from router import qr_code_router


app = FastAPI(default_response_class=UJSONResponse)

origins = [
'*',
"*",
]

app.add_middleware(
Expand All @@ -19,6 +20,6 @@
)

app.include_router(
qr_code_generator.router,
qr_code_router.router,
prefix="/api/v1"
)
85 changes: 0 additions & 85 deletions app/router/qr_code_generator.py

This file was deleted.

50 changes: 50 additions & 0 deletions app/router/qr_code_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from fastapi import APIRouter, Request
from fastapi.responses import StreamingResponse

from schema.schemas import UrlData, WifiData, ContactData, GeoLocationData, EmailData, HealthCheck
from service.qr_code_service import url_to_qr_service, wifi_to_qr_service, contact_to_qr_service, geo_to_qr_service, email_to_qr_service


router = APIRouter()

@router.post("/url_to_qr")
async def url_to_qr(request: Request,
info: UrlData) -> StreamingResponse:

return await url_to_qr_service(request, info)


@router.post("/wifi_to_qr")
async def wifi_to_qr(request: Request,
info: WifiData) -> StreamingResponse:

return await wifi_to_qr_service(request, info)


@router.post("/contact_to_qr")
async def contact_to_qr(request: Request,
info: ContactData) -> StreamingResponse:

return await contact_to_qr_service(request, info)


@router.post("/geo_to_qr")
async def geo_to_qr(request: Request,
info: GeoLocationData) -> StreamingResponse:

return await geo_to_qr_service(request, info)


@router.post("/email_to_qr")
async def email_to_qr(request: Request,
info: EmailData) -> StreamingResponse:

return await email_to_qr_service(request, info)


@router.get("/health")
async def get_health() -> HealthCheck:

return HealthCheck(status="OK")

# return templates.TemplateResponse("home.html", {"request": request, "qr_data": qr_data})
5 changes: 4 additions & 1 deletion app/schema/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ class EmailData(BaseModel):
to: str
subject: str
body: str
cc: str = None
cc: str = None

class HealthCheck(BaseModel):
status: str = "OK"
87 changes: 87 additions & 0 deletions app/service/qr_code_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import io
import segno
from fastapi import Request, status
from fastapi.responses import StreamingResponse
from segno import helpers

from schema.schemas import UrlData, WifiData, ContactData, GeoLocationData, EmailData


async def url_to_qr_service(request: Request,
info: UrlData) -> StreamingResponse:

data: str = info.url
qr_data: bytes = segno.make_qr(data, error = 'H')
qr_buffer = io.BytesIO()
qr_data.save(qr_buffer, kind='png', scale=10)
qr_buffer.seek(0)
return StreamingResponse(status_code=status.HTTP_201_CREATED,
content=qr_buffer,
media_type="image/png")


async def wifi_to_qr_service(request: Request,
info: WifiData) -> StreamingResponse:

name: str = info.wifi_name
password: str = info.wifi_password
data: bytes = helpers.make_wifi_data(ssid=name, password=password, security='WPA')
qr_data: bytes = segno.make(data, error = 'H')
qr_buffer = io.BytesIO()
qr_data.save(qr_buffer, kind='png', scale=10)
qr_buffer.seek(0)
return StreamingResponse(status_code=status.HTTP_201_CREATED,
content=qr_buffer,
media_type="image/png")


async def contact_to_qr_service(request: Request,
info: ContactData) -> StreamingResponse:

data: bytes = helpers.make_vcard_data(
displayname = info.name,
name = info.name,
email = info.email,
phone = info.phone,
city = info.city,
org = info.org,
title = info.title,
url = info.url
)
qr_data: bytes = segno.make(data, error = 'H')
qr_buffer = io.BytesIO()
qr_data.save(qr_buffer, kind='png', scale=10)
qr_buffer.seek(0)
return StreamingResponse(status_code=status.HTTP_201_CREATED,
content=qr_buffer,
media_type="image/png")


async def geo_to_qr_service(request: Request,
info: GeoLocationData) -> StreamingResponse:

data: bytes = helpers.make_geo_data(info.latitude, info.longitude)
qr_data: bytes = segno.make(data, error = 'H')
qr_buffer = io.BytesIO()
qr_data.save(qr_buffer, kind='png', scale=10)
qr_buffer.seek(0)
return StreamingResponse(status_code=status.HTTP_201_CREATED,
content=qr_buffer,
media_type="image/png")


async def email_to_qr_service(request: Request,
info: EmailData) -> StreamingResponse:

qr_data: bytes = helpers.make_email(
to = info.to,
subject = info.subject,
body = info.body,
cc = info.cc
)
qr_buffer = io.BytesIO()
qr_data.save(qr_buffer, kind='png', scale=10)
qr_buffer.seek(0)
return StreamingResponse(status_code=status.HTTP_201_CREATED,
content=qr_buffer,
media_type="image/png")
6 changes: 6 additions & 0 deletions conf/gunicorn.conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import multiprocessing
import uvicorn

workers = multiprocessing.cpu_count() * 2 + 1
bind = '0.0.0.0:8000'
worker_class = 'uvicorn.workers.UvicornWorker'
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.3'
version: '3.8'

services:
app:
Expand Down
4 changes: 1 addition & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ anyio==4.3.0
click==8.1.7
colorama==0.4.6
fastapi==0.110.2
gunicorn==22.0.0
h11==0.14.0
httptools==0.6.1
idna==3.7
Expand All @@ -13,9 +14,6 @@ packaging==24.0
pillow==10.3.0
pydantic==2.7.1
pydantic_core==2.18.2
pypng==0.20220715.0
pytz==2024.1
PyYAML==6.0.1
segno==1.6.1
sniffio==1.3.1
starlette==0.37.2
Expand Down

0 comments on commit a053143

Please sign in to comment.