Skip to content

draft: Adding S3 dependency #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .appcd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: 0.0.1
name: dogeapi
imageRegistry: ghcr.io/appcd-dev/dogeapi/dogeapi
imageTag: latest
dockerFile: Dockerfile
6 changes: 0 additions & 6 deletions .appcd.yml

This file was deleted.

32 changes: 32 additions & 0 deletions .appcd/aug-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"metadata": {
"repository": "some-repo-url",
"commit": "some-commit-hash",
"branch": "branch-name"
},
"projects": [
{
"metadata": {
"languages": [
"python"
],
"frameworks": [
"fastapi"
]
},
"services": [
{
"name": "dogeapi",
"image_registry": "ghcr.io/appcd-dev/dogeapi/dogeapi",
"image_tag": "latest",
"server_port": 8000,
"ports": [
{
"listen": 8000
}
]
}
]
}
]
}
142 changes: 118 additions & 24 deletions .appcd/manifest.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,126 @@
{
"metadata": {
"repository": "some-repo-url",
"commit": "some-commit-hash",
"branch": "branch-name"
"version": "0.0.1",
"graph": {
"dogeapi": [
"dogeapi-presign"
],
"dogeapi-presign": []
},
"projects": [
{
"metadata": {
"languages": [
"python"
"components": {
"dogeapi": {
"image_registry": "ghcr.io/appcd-dev/dogeapi/dogeapi",
"image_tag": "latest",
"server_port": 8000,
"ports": [
{
"listen": 8000
}
],
"http_egress": [
{
"endpoint": "${PRESIGN_SERVICE_URL}/presign",
"operations": [
"GET"
]
}
],
"http_ingress": [
{
"endpoint": "/login",
"operations": [
"POST"
]
},
{
"endpoint": "/blog",
"operations": [
"GET",
"POST"
]
},
{
"endpoint": "/blog/{id}",
"operations": [
"GET",
"PUT",
"DELETE"
]
},
{
"endpoint": "/users",
"operations": [
"GET",
"POST"
]
},
{
"endpoint": "/users/{id}",
"operations": [
"GET"
]
},
{
"endpoint": "/",
"operations": [
"GET"
]
}
],
"dependencies": {
"s3": [
{
"bucket": "${S3_BUCKET_NAME}",
"methods": [
"GetObject",
"PutObject"
]
}
],
"frameworks": [
"fastapi"
"database": [
{
"methods:": [
"READ",
"WRITE"
],
"dsn": "${CONNECTION_STRING}",
"db_engine": "postgres"
}
],
"env": [
{
"name": "DEVELOPER_FLAG_1"
},
{
"name": "DEBUG"
}
]
},
"services": [
{
"name": "dogeapi",
"image_registry": "ghcr.io/appcd-dev/dogeapi/dogeapi",
"image_tag": "latest",
"server_port": 8000,
"ports": [
{
"listen": 8000
}
}
},
"dogeapi-presign": {
"image_registry": "ghcr.io/appcd-dev/dogeapi-presign/presign",
"image_tag": "latest",
"server_port": 5000,
"ports": [
{
"listen": 5000
}
],
"http_ingress": [
{
"endpoint": "/presign/{s3_key}",
"operations": [
"GET"
]
}
],
"dependencies": {
"s3": {
"bucket": "${S3_BUCKET}",
"methods": [
"GetObject"
]
}
]
}
}
]
}
}
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ USER 1000
ENV ACCESS_LOG=${ACCESS_LOG:-/proc/1/fd/1}
ENV ERROR_LOG=${ERROR_LOG:-/proc/1/fd/2}

EXPOSE 8443
EXPOSE 8000

# Define the Uvicorn command to run our application
CMD ["uvicorn", "main:app", "--reload", "--workers", "1", "--host", "0.0.0.0", "--port", "8443"]
CMD ["uvicorn", "main:app", "--reload", "--workers", "1", "--host", "0.0.0.0", "--port", "8000"]
36 changes: 35 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,38 @@ build:
docker-compose build

lint:
docker-compose run --rm backend pre-commit run --all-files
docker-compose run --rm backend pre-commit run --all-files

configure:
echo "Creating user"
curl -X 'POST' \
'http://0.0.0.0:8000/users/' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{ "name": "cesar", "email": "cesar@cesar.com", "password": "cesar" }'
echo "login"
export BEARER_TOKEN=$(shell curl -X 'POST' \
'http://0.0.0.0:8000/login/' \
-H 'accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=&username=cesar%40cesar.com&password=cesar' | jq -r '.access_token')

create-blog:
curl -X 'POST' \
'http://0.0.0.0:8000/blog/' \
-H 'accept: application/json' \
-H 'Authorization: Bearer ${BEARER_TOKEN}' \
-H 'Content-Type: application/json' \
-d '{"title": "Test blog title", "body": "Test blog body"}'

get-blogs:
curl -X 'GET' \
'http://0.0.0.0:8000/blog/' \
-H 'accept: application/json' \
-H 'Authorization: Bearer ${BEARER_TOKEN}'

get-presigned-url:
curl -X 'GET' \
'http://localhost:8000/blog/2/download' \
-H 'accept: application/json' \
-H 'Authorization: Bearer ${BEARER_TOKEN}'
8 changes: 7 additions & 1 deletion api/blog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/python3

import uuid

from fastapi import HTTPException, status
from sqlalchemy.orm import Session

Expand Down Expand Up @@ -31,7 +33,11 @@ def create(request: schemas.Blog, db: Session):
Returns:
models.Blog: Blog object
"""
new_blog = models.Blog(title=request.title, body=request.body, user_id=1)
s3_key = str(uuid.uuid4()) # Generating a unique UUID for the S3 key

new_blog = models.Blog(s3_key=s3_key, user_id=1, title=request.title)
new_blog.body = request.body

db.add(new_blog)
db.commit()
db.refresh(new_blog)
Expand Down
55 changes: 53 additions & 2 deletions core/blog.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/python3

from typing import List
import os

from fastapi import APIRouter, Depends, Response, status
from fastapi import APIRouter, Depends, Response, status, HTTPException
from sqlalchemy.orm import Session
import httpx

from api import blog
from database import configuration
Expand All @@ -12,7 +14,7 @@

router = APIRouter(tags=["Blogs"], prefix="/blog")
get_db = configuration.get_db

PRESIGN_SERVICE_URL = os.getenv("PRESIGN_SERVICE_URL", "http://presign:5000")

@router.get("/", response_model=List[schemas.ShowBlog])
def get_all_blogs(
Expand Down Expand Up @@ -114,3 +116,52 @@ def update_blog(
schemas.Blog: Updated blog
"""
return blog.update(id, request, db)


async def get_presigned_url_from_microservice(s3_key: str) -> str:
# Using Docker Compose service discovery to get presign service by its name.
url = f"{PRESIGN_SERVICE_URL}/presign/{s3_key}"

async with httpx.AsyncClient() as client:
response = await client.get(url)

if response.status_code != 200:
# Handle unexpected response or error from the microservice
raise HTTPException(
status_code=500, detail="Failed to obtain presigned URL"
)

return response.text


@router.get("/{id}/download", status_code=status.HTTP_200_OK, response_model=str)
async def get_download_url(
id: int,
db: Session = Depends(get_db),
current_user: schemas.User = Depends(get_current_user),
):
"""
Get a pre-signed download URL for a blog's associated S3 object.

Args:
id (int): Blog id
db (Session, optional): Database session. Defaults to None.
current_user (schemas.User, optional): Current user. Defaults to None.

Returns:
str: Pre-signed download URL
"""
fetched_blog = blog.show(id, db)

if not fetched_blog:
raise HTTPException(status_code=404, detail="Blog not found")

if not fetched_blog.s3_key:
raise HTTPException(
status_code=404, detail="S3 key not found for the specified blog"
)

s3_key = fetched_blog.s3_key
presigned_url = await get_presigned_url_from_microservice(s3_key)

return presigned_url
18 changes: 18 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ services:
#- DATABASE_URL=sqlite:////var/run/dogeapi/dogeapi.sqlite
- SECRET_KEY=dev
- ACCESS_TOKEN_EXPIRE_MINUTES=30
- S3_BUCKET_NAME=appcd-demo-dogeapi
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN}
depends_on:
- db
- presign
db:
image: postgres
restart: always
Expand All @@ -26,3 +31,16 @@ services:
POSTGRES_DB: postgres
ports:
- "5432:5432"
presign:
#build:
#context: ../DogeFlaskAPI
#dockerfile: Dockerfile
image: ghcr.io/appcd-dev/dogeapi-presign/presign:latest
ports:
- 5000:5000
environment:
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN}
- S3_BUCKET=appcd-demo-dogeapi

Loading