Skip to content

Commit

Permalink
feat: add the basic structure for the home page
Browse files Browse the repository at this point in the history
  • Loading branch information
allisson committed Dec 6, 2023
1 parent a6c4035 commit 82ac3bf
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ RUN poetry install --only main

# Copy app files to workdir
COPY secure_qrcode ./secure_qrcode
COPY templates ./templates
COPY static ./static

##### Final Stage #####
FROM python:3.12-slim-bookworm
Expand Down
102 changes: 94 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pycryptodome = "^3.19.0"
fastapi = "^0.104.1"
uvicorn = {extras = ["standard"], version = "^0.24.0.post1"}
pydantic-settings = "^2.1.0"
jinja2 = "^3.1.2"

[tool.poetry.group.test.dependencies]
pytest = "^7.4.3"
Expand Down
14 changes: 12 additions & 2 deletions secure_qrcode/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from base64 import b64encode

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

from secure_qrcode.config import settings
from secure_qrcode.crypto import decrypt, encrypt
Expand All @@ -19,6 +21,8 @@
title="Secure QR code",
description="Encrypt your data using the modern ChaCha20-Poly1305 cipher and export it into a secure QR code",
)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")


@app.exception_handler(DecryptError)
Expand All @@ -29,7 +33,12 @@ def decrypt_error_exception_handler(request: Request, exc: DecryptError):
)


@app.post("/v1/encode", status_code=201)
@app.get("/", response_class=HTMLResponse, tags=["home"])
def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})


@app.post("/v1/encode", status_code=201, tags=["api"])
def encode(request: EncodeRequest) -> EncodeResponse:
encrypted_data = encrypt(request.plaintext, request.key, settings.left_padding_char)
img_io = make(
Expand All @@ -45,6 +54,7 @@ def encode(request: EncodeRequest) -> EncodeResponse:
"/v1/decode",
status_code=201,
responses={400: {"model": DecryptErrorResponse, "description": "Incorrect decryption"}},
tags=["api"],
)
def decode(request: DecodeRequest) -> DecodeResponse:
decrypted_data = decrypt(request.encrypted_data, request.key, settings.left_padding_char)
Expand Down
3 changes: 3 additions & 0 deletions static/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
p {
color: red;
}
11 changes: 11 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello World</title>
<link href="{{ url_for('static', path='/styles.css') }}" rel="stylesheet">
</head>
<body>
<p>Hello World</p>
</body>
</html>
6 changes: 6 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
from secure_qrcode.models import DecodeRequest, EncodeRequest, EncryptedData


def test_index(client):
response = client.get("/")

assert response.status_code == 200


def test_encode(client, plaintext, key):
request = EncodeRequest(plaintext=plaintext, key=key)
response = client.post("/v1/encode", json=request.model_dump())
Expand Down

0 comments on commit 82ac3bf

Please sign in to comment.