Refactors Project for More Declarative Package Name
This commit is contained in:
@@ -4,7 +4,7 @@ version = "0.1.0"
|
||||
description = ""
|
||||
authors = ["Vitor Hideyoshi <vitor.h.n.batista@gmail.com>"]
|
||||
readme = "README.md"
|
||||
packages = [{include = "resize_image_service"}]
|
||||
packages = [{include = "storage_service"}]
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.8"
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
from resize_image_service.controller.s3_controller import s3_router
|
||||
|
||||
from fastapi import FastAPI
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(s3_router, tags=["s3"], prefix="/s3")
|
||||
@@ -1,48 +0,0 @@
|
||||
from resize_image_service.depends.depend_queue import dependency_queue
|
||||
from resize_image_service.depends.depend_s3_service import (
|
||||
dependency_storage_service,
|
||||
)
|
||||
from resize_image_service.service.storage_service import StorageService
|
||||
from resize_image_service.utils.enums.file_type import FileType
|
||||
from resize_image_service.utils.file_name_hash import file_name_hash
|
||||
from resize_image_service.worker.storage_file_worker import storage_file_worker
|
||||
|
||||
from fastapi import Body, Depends, Form
|
||||
from fastapi_utils.cbv import cbv
|
||||
from fastapi_utils.inferring_router import InferringRouter
|
||||
from rq import Queue
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
s3_router = InferringRouter()
|
||||
|
||||
|
||||
@cbv(s3_router)
|
||||
class S3Controller:
|
||||
queue: Queue = Depends(dependency_queue, use_cache=True)
|
||||
storage_service: StorageService = Depends(dependency_storage_service, use_cache=True)
|
||||
|
||||
@s3_router.get("/new_file_url/", status_code=200)
|
||||
def new_file_url(
|
||||
self,
|
||||
username: Annotated[str, Body(embed=True)],
|
||||
file_postfix: Annotated[str, Body(embed=True)],
|
||||
file_type: Annotated[FileType, Body(embed=True)],
|
||||
) -> dict[str, str]:
|
||||
return self.storage_service.get_temp_upload_link(
|
||||
file_name_hash(username, file_postfix), file_type
|
||||
)
|
||||
|
||||
@s3_router.get("/file_url/", status_code=200)
|
||||
def file_url(
|
||||
self,
|
||||
username: Annotated[str, Body(embed=True)],
|
||||
file_postfix: Annotated[str, Body(embed=True)],
|
||||
) -> dict[str, str]:
|
||||
return self.storage_service.get_temp_read_link(
|
||||
file_name_hash(username, file_postfix)
|
||||
)
|
||||
|
||||
@s3_router.post("/process_file/", status_code=200)
|
||||
def process_file(self, string_url: Annotated[str, Body(embed=True)]):
|
||||
self.queue.enqueue(storage_file_worker, string_url)
|
||||
@@ -1,7 +0,0 @@
|
||||
from resize_image_service.depends.depend_s3_service import (
|
||||
dependency_storage_service,
|
||||
)
|
||||
|
||||
|
||||
def storage_file_worker(string_url: str) -> None:
|
||||
dependency_storage_service().process_image(string_url)
|
||||
@@ -1,5 +1,5 @@
|
||||
from resize_image_service.config.config_server import get_config_server
|
||||
from resize_image_service.controller import app
|
||||
from storage_service.config.config_server import get_config_server
|
||||
from storage_service.controller import app
|
||||
|
||||
import uvicorn
|
||||
|
||||
14
storage_service/config/config_allowed_origins.py
Normal file
14
storage_service/config/config_allowed_origins.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
|
||||
def get_allowed_origins():
|
||||
load_dotenv()
|
||||
|
||||
origins = os.environ.get("ALLOWED_ORIGINS", None)
|
||||
|
||||
if origins is None:
|
||||
return []
|
||||
|
||||
return origins.split(",")
|
||||
@@ -1,4 +1,4 @@
|
||||
from resize_image_service.utils.enums.storage_type import StorageType
|
||||
from storage_service.utils.enums.storage_type import StorageType
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -7,5 +7,5 @@ def get_config_server():
|
||||
load_dotenv()
|
||||
return {
|
||||
"host": os.environ.get("SERVER_HOST", "0.0.0.0"),
|
||||
"port": os.environ.get("SERVER_PORT", 8000),
|
||||
"port": int(os.environ.get("SERVER_PORT", 8000)),
|
||||
}
|
||||
19
storage_service/controller/__init__.py
Normal file
19
storage_service/controller/__init__.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from storage_service.config.config_allowed_origins import get_allowed_origins
|
||||
from storage_service.controller.storage_controller import s3_router
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=get_allowed_origins(),
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.include_router(s3_router)
|
||||
46
storage_service/controller/storage_controller.py
Normal file
46
storage_service/controller/storage_controller.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from storage_service.depends.depend_queue import dependency_queue
|
||||
from storage_service.depends.depend_s3_service import (
|
||||
dependency_storage_service,
|
||||
)
|
||||
from storage_service.service.storage_service import StorageService
|
||||
from storage_service.utils.enums.file_type import FileType
|
||||
from storage_service.utils.file_name_hash import file_name_hash
|
||||
from storage_service.worker.storage_file_worker import storage_file_worker
|
||||
|
||||
from fastapi import Body, Depends, Form
|
||||
from fastapi_utils.cbv import cbv
|
||||
from fastapi_utils.inferring_router import InferringRouter
|
||||
from rq import Queue
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
s3_router = InferringRouter()
|
||||
|
||||
|
||||
@cbv(s3_router)
|
||||
class StorageController:
|
||||
queue: Queue = Depends(dependency_queue, use_cache=True)
|
||||
storage_service: StorageService = Depends(dependency_storage_service, use_cache=True)
|
||||
|
||||
@s3_router.post("/new_file_url/", status_code=200)
|
||||
def new_file_url(
|
||||
self,
|
||||
username: Annotated[str, Body(embed=True)],
|
||||
file_postfix: Annotated[str, Body(embed=True)],
|
||||
file_type: Annotated[FileType, Body(embed=True)],
|
||||
) -> dict[str, str]:
|
||||
return self.storage_service.get_temp_upload_link(
|
||||
file_name_hash(username, file_postfix), file_type
|
||||
)
|
||||
|
||||
@s3_router.get("/file_url/", status_code=200)
|
||||
def file_url(self, username: str, file_postfix: str) -> dict[str, str]:
|
||||
return self.storage_service.get_temp_read_link(
|
||||
file_name_hash(username, file_postfix)
|
||||
)
|
||||
|
||||
@s3_router.post("/process_file/", status_code=200)
|
||||
def process_file(self,
|
||||
username: Annotated[str, Body(embed=True)],
|
||||
file_postfix: Annotated[str, Body(embed=True)]):
|
||||
self.queue.enqueue(storage_file_worker, username, file_postfix)
|
||||
@@ -1,4 +1,4 @@
|
||||
from resize_image_service.config.config_redis import get_config_redis
|
||||
from storage_service.config.config_redis import get_config_redis
|
||||
|
||||
from redis import Redis
|
||||
from rq import Queue
|
||||
@@ -1,7 +1,7 @@
|
||||
from resize_image_service.config.config_s3 import get_config_s3
|
||||
from resize_image_service.service.amazon_s3_service import AmazonS3Service
|
||||
from resize_image_service.service.storage_service import StorageService
|
||||
from resize_image_service.utils.enums.storage_type import StorageType
|
||||
from storage_service.config.config_s3 import get_config_s3
|
||||
from storage_service.service.amazon_s3_service import AmazonS3Service
|
||||
from storage_service.service.storage_service import StorageService
|
||||
from storage_service.utils.enums.storage_type import StorageType
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from resize_image_service.service.storage_service import StorageService
|
||||
from resize_image_service.utils.enums.file_type import FileType
|
||||
from resize_image_service.utils.file_handler import FILE_HANDLER
|
||||
from storage_service.service.storage_service import StorageService
|
||||
from storage_service.utils.enums.file_type import FileType
|
||||
from storage_service.utils.file_handler import FILE_HANDLER
|
||||
|
||||
import boto3
|
||||
from PIL import Image
|
||||
@@ -40,7 +40,7 @@ class AmazonS3Service(StorageService):
|
||||
def get_temp_read_link(self, file_name) -> dict[str, str | Any]:
|
||||
return {"presigned_url": self._get_presigned_read_url(file_name)}
|
||||
|
||||
def process_file(self, file_name: str, file_type: FileType) -> None:
|
||||
def process_file(self, file_name: str, file_type: FileType = FileType.PNG) -> None:
|
||||
file_bytes = self._get_file_obj(file_name)
|
||||
handler = FILE_HANDLER[file_type]["handler"]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from resize_image_service.utils.enums.file_type import FileType
|
||||
from storage_service.utils.enums.file_type import FileType
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
@@ -21,5 +21,5 @@ class StorageService(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def process_file(self, file_name) -> None:
|
||||
def process_file(self, file_name: str, file_type: FileType) -> None:
|
||||
pass
|
||||
@@ -1,5 +1,5 @@
|
||||
from resize_image_service.utils.enums.file_type import FileType
|
||||
from resize_image_service.utils.file_handler.handlers.image_handler import (
|
||||
from storage_service.utils.enums.file_type import FileType
|
||||
from storage_service.utils.file_handler.handlers.image_handler import (
|
||||
image_handler,
|
||||
)
|
||||
|
||||
11
storage_service/worker/storage_file_worker.py
Normal file
11
storage_service/worker/storage_file_worker.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from storage_service.depends.depend_s3_service import (
|
||||
dependency_storage_service,
|
||||
)
|
||||
from storage_service.utils.enums.file_type import FileType
|
||||
from storage_service.utils.file_name_hash import file_name_hash
|
||||
|
||||
|
||||
def storage_file_worker(username: str, file_postfix: str) -> None:
|
||||
dependency_storage_service().process_file(
|
||||
file_name_hash(username, file_postfix)
|
||||
)
|
||||
Reference in New Issue
Block a user