diff --git a/docker/production.dockerfile b/docker/production.dockerfile index dc648ef..b86cdbb 100644 --- a/docker/production.dockerfile +++ b/docker/production.dockerfile @@ -1,5 +1,9 @@ FROM ghcr.io/kurbezz/base_docker_images:3.10-postgres-asyncpg-poetry-buildtime as build-image +RUN apt-get update \ + && apt-get install git -y --no-install-recommends \ + && rm -rf /var/cache/* + WORKDIR /root/poetry COPY pyproject.toml poetry.lock /root/poetry/ diff --git a/poetry.lock b/poetry.lock index f8ec5c8..6e99b40 100644 --- a/poetry.lock +++ b/poetry.lock @@ -82,16 +82,16 @@ python-versions = ">=3.6" [[package]] name = "asyncpg" -version = "0.25.0" +version = "0.26.0" description = "An asyncio PostgreSQL driver" category = "main" optional = false python-versions = ">=3.6.0" [package.extras] -dev = ["Cython (>=0.29.24,<0.30.0)", "pytest (>=6.0)", "Sphinx (>=4.1.2,<4.2.0)", "sphinxcontrib-asyncio (>=0.3.0,<0.4.0)", "sphinx-rtd-theme (>=0.5.2,<0.6.0)", "pycodestyle (>=2.7.0,<2.8.0)", "flake8 (>=3.9.2,<3.10.0)", "uvloop (>=0.15.3)"] -docs = ["Sphinx (>=4.1.2,<4.2.0)", "sphinxcontrib-asyncio (>=0.3.0,<0.4.0)", "sphinx-rtd-theme (>=0.5.2,<0.6.0)"] -test = ["pycodestyle (>=2.7.0,<2.8.0)", "flake8 (>=3.9.2,<3.10.0)", "uvloop (>=0.15.3)"] +test = ["uvloop (>=0.15.3)", "flake8 (>=3.9.2,<3.10.0)", "pycodestyle (>=2.7.0,<2.8.0)"] +docs = ["sphinx-rtd-theme (>=0.5.2,<0.6.0)", "sphinxcontrib-asyncio (>=0.3.0,<0.4.0)", "Sphinx (>=4.1.2,<4.2.0)"] +dev = ["uvloop (>=0.15.3)", "flake8 (>=3.9.2,<3.10.0)", "pycodestyle (>=2.7.0,<2.8.0)", "sphinx-rtd-theme (>=0.5.2,<0.6.0)", "sphinxcontrib-asyncio (>=0.3.0,<0.4.0)", "Sphinx (>=4.1.2,<4.2.0)", "pytest (>=6.0)", "Cython (>=0.29.24,<0.30.0)"] [[package]] name = "certifi" @@ -122,21 +122,24 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" [[package]] name = "databases" -version = "0.5.5" +version = "0.6.1" description = "Async database support for Python." category = "main" optional = false -python-versions = ">=3.6" +python-versions = ">=3.7" [package.dependencies] sqlalchemy = ">=1.4,<1.5" [package.extras] -mysql = ["aiomysql"] -mysql_asyncmy = ["asyncmy"] -postgresql = ["asyncpg"] -postgresql_aiopg = ["aiopg"] sqlite = ["aiosqlite"] +postgresql = ["asyncpg"] +mysql = ["aiomysql"] +asyncpg = ["asyncpg"] +asyncmy = ["asyncmy"] +aiosqlite = ["aiosqlite"] +aiopg = ["aiopg"] +aiomysql = ["aiomysql"] [[package]] name = "deprecated" @@ -287,24 +290,31 @@ version = "0.11.2" description = "A simple async ORM with fastapi in mind and pydantic validation." category = "main" optional = false -python-versions = ">=3.7.0,<4.0.0" +python-versions = "^3.7.0" +develop = false [package.dependencies] -asyncpg = {version = ">=0.24,<0.26", optional = true, markers = "extra == \"postgresql\" or extra == \"postgres\" or extra == \"all\""} -databases = ">=0.3.2,<0.5.0 || >0.5.0,<0.5.1 || >0.5.1,<0.5.2 || >0.5.2,<0.5.3 || >0.5.3,<0.6.1" -psycopg2-binary = {version = ">=2.9.1,<3.0.0", optional = true, markers = "extra == \"postgresql\" or extra == \"postgres\" or extra == \"aiopg\" or extra == \"all\""} -pydantic = ">=1.6.1,<1.7 || >1.7,<1.7.1 || >1.7.1,<1.7.2 || >1.7.2,<1.7.3 || >1.7.3,<1.8 || >1.8,<1.8.1 || >1.8.1,<=1.9.1" -SQLAlchemy = ">=1.3.18,<1.4.39" +asyncpg = {version = ">=0.24,<0.27", optional = true} +databases = ">=0.3.2,!=0.5.0,!=0.5.1,!=0.5.2,!=0.5.3,<0.6.2" +psycopg2-binary = {version = "^2.9.1", optional = true} +pydantic = ">=1.6.1,!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<1.9.3" +SQLAlchemy = ">=1.3.18,<1.4.41" [package.extras] -crypto = ["cryptography (>=35,<38)"] -all = ["cryptography (>=35,<38)", "aiosqlite (>=0.17.0,<0.18.0)", "aiomysql (>=0.0.21,<0.0.23)", "aiopg (>=1.3.3,<2.0.0)", "asyncpg (>=0.24,<0.26)", "psycopg2-binary (>=2.9.1,<3.0.0)", "mysqlclient (>=2.1.0,<3.0.0)", "PyMySQL (>=0.9,<=0.9.3)", "orjson (>=3.6.4)"] -sqlite = ["aiosqlite (>=0.17.0,<0.18.0)"] -mysql = ["aiomysql (>=0.0.21,<0.0.23)", "PyMySQL (>=0.9,<=0.9.3)"] +postgresql = ["asyncpg (>=0.24,<0.27)", "psycopg2-binary (>=2.9.1,<3.0.0)"] +postgres = ["asyncpg (>=0.24,<0.27)", "psycopg2-binary (>=2.9.1,<3.0.0)"] aiopg = ["aiopg (>=1.3.3,<2.0.0)", "psycopg2-binary (>=2.9.1,<3.0.0)"] -postgresql = ["asyncpg (>=0.24,<0.26)", "psycopg2-binary (>=2.9.1,<3.0.0)"] -postgres = ["asyncpg (>=0.24,<0.26)", "psycopg2-binary (>=2.9.1,<3.0.0)"] +mysql = ["aiomysql (>=0.0.21,<0.0.23)", "PyMySQL (>=0.9,<=0.9.3)"] +sqlite = ["aiosqlite (>=0.17.0,<0.18.0)"] orjson = ["orjson (>=3.6.4)"] +crypto = ["cryptography (>=35,<38)"] +all = ["aiosqlite (>=0.17.0,<0.18.0)", "asyncpg (>=0.24,<0.27)", "aiopg (>=1.3.3,<2.0.0)", "psycopg2-binary (>=2.9.1,<3.0.0)", "aiomysql (>=0.0.21,<0.0.23)", "mysqlclient (>=2.1.0,<3.0.0)", "PyMySQL (>=0.9,<=0.9.3)", "orjson (>=3.6.4)", "cryptography (>=35,<38)"] + +[package.source] +type = "git" +url = "https://github.com/collerek/ormar.git" +reference = "master" +resolved_reference = "a27d7673a5354429bef4158297d76a58522c1579" [[package]] name = "packaging" @@ -551,7 +561,7 @@ python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "356f308465ad702a5feb94c6c8341b5cfa4d986ebc71a4d9c9e4b0149485467d" +content-hash = "9cca4e42c068a72cbe05239e1b3117a0a329bfcc1d959f7d8e70d6812d31cd3f" [metadata.files] aioredis = [ @@ -579,32 +589,32 @@ async-timeout = [ {file = "async_timeout-4.0.2-py3-none-any.whl", hash = "sha256:8ca1e4fcf50d07413d66d1a5e416e42cfdf5851c981d679a09851a6853383b3c"}, ] asyncpg = [ - {file = "asyncpg-0.25.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:bf5e3408a14a17d480f36ebaf0401a12ff6ae5457fdf45e4e2775c51cc9517d3"}, - {file = "asyncpg-0.25.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:2bc197fc4aca2fd24f60241057998124012469d2e414aed3f992579db0c88e3a"}, - {file = "asyncpg-0.25.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:1a70783f6ffa34cc7dd2de20a873181414a34fd35a4a208a1f1a7f9f695e4ec4"}, - {file = "asyncpg-0.25.0-cp310-cp310-win32.whl", hash = "sha256:43cde84e996a3afe75f325a68300093425c2f47d340c0fc8912765cf24a1c095"}, - {file = "asyncpg-0.25.0-cp310-cp310-win_amd64.whl", hash = "sha256:56d88d7ef4341412cd9c68efba323a4519c916979ba91b95d4c08799d2ff0c09"}, - {file = "asyncpg-0.25.0-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:a84d30e6f850bac0876990bcd207362778e2208df0bee8be8da9f1558255e634"}, - {file = "asyncpg-0.25.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:beaecc52ad39614f6ca2e48c3ca15d56e24a2c15cbfdcb764a4320cc45f02fd5"}, - {file = "asyncpg-0.25.0-cp36-cp36m-musllinux_1_1_x86_64.whl", hash = "sha256:6f8f5fc975246eda83da8031a14004b9197f510c41511018e7b1bedde6968e92"}, - {file = "asyncpg-0.25.0-cp36-cp36m-win32.whl", hash = "sha256:ddb4c3263a8d63dcde3d2c4ac1c25206bfeb31fa83bd70fd539e10f87739dee4"}, - {file = "asyncpg-0.25.0-cp36-cp36m-win_amd64.whl", hash = "sha256:bf6dc9b55b9113f39eaa2057337ce3f9ef7de99a053b8a16360395ce588925cd"}, - {file = "asyncpg-0.25.0-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:acb311722352152936e58a8ee3c5b8e791b24e84cd7d777c414ff05b3530ca68"}, - {file = "asyncpg-0.25.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:0a61fb196ce4dae2f2fa26eb20a778db21bbee484d2e798cb3cc988de13bdd1b"}, - {file = "asyncpg-0.25.0-cp37-cp37m-musllinux_1_1_x86_64.whl", hash = "sha256:2633331cbc8429030b4f20f712f8d0fbba57fa8555ee9b2f45f981b81328b256"}, - {file = "asyncpg-0.25.0-cp37-cp37m-win32.whl", hash = "sha256:863d36eba4a7caa853fd7d83fad5fd5306f050cc2fe6e54fbe10cdb30420e5e9"}, - {file = "asyncpg-0.25.0-cp37-cp37m-win_amd64.whl", hash = "sha256:fe471ccd915b739ca65e2e4dbd92a11b44a5b37f2e38f70827a1c147dafe0fa8"}, - {file = "asyncpg-0.25.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:72a1e12ea0cf7c1e02794b697e3ca967b2360eaa2ce5d4bfdd8604ec2d6b774b"}, - {file = "asyncpg-0.25.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:4327f691b1bdb222df27841938b3e04c14068166b3a97491bec2cb982f49f03e"}, - {file = "asyncpg-0.25.0-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:739bbd7f89a2b2f6bc44cb8bf967dab12c5bc714fcbe96e68d512be45ecdf962"}, - {file = "asyncpg-0.25.0-cp38-cp38-win32.whl", hash = "sha256:18d49e2d93a7139a2fdbd113e320cc47075049997268a61bfbe0dde680c55471"}, - {file = "asyncpg-0.25.0-cp38-cp38-win_amd64.whl", hash = "sha256:191fe6341385b7fdea7dbdcf47fd6db3fd198827dcc1f2b228476d13c05a03c6"}, - {file = "asyncpg-0.25.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:52fab7f1b2c29e187dd8781fce896249500cf055b63471ad66332e537e9b5f7e"}, - {file = "asyncpg-0.25.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:a738f1b2876f30d710d3dc1e7858160a0afe1603ba16bf5f391f5316eb0ed855"}, - {file = "asyncpg-0.25.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:5e4105f57ad1e8fbc8b1e535d8fcefa6ce6c71081228f08680c6dea24384ff0e"}, - {file = "asyncpg-0.25.0-cp39-cp39-win32.whl", hash = "sha256:f55918ded7b85723a5eaeb34e86e7b9280d4474be67df853ab5a7fa0cc7c6bf2"}, - {file = "asyncpg-0.25.0-cp39-cp39-win_amd64.whl", hash = "sha256:649e2966d98cc48d0646d9a4e29abecd8b59d38d55c256d5c857f6b27b7407ac"}, - {file = "asyncpg-0.25.0.tar.gz", hash = "sha256:63f8e6a69733b285497c2855464a34de657f2cccd25aeaeeb5071872e9382540"}, + {file = "asyncpg-0.26.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:2ed3880b3aec8bda90548218fe0914d251d641f798382eda39a17abfc4910af0"}, + {file = "asyncpg-0.26.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e5bd99ee7a00e87df97b804f178f31086e88c8106aca9703b1d7be5078999e68"}, + {file = "asyncpg-0.26.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:868a71704262834065ca7113d80b1f679609e2df77d837747e3d92150dd5a39b"}, + {file = "asyncpg-0.26.0-cp310-cp310-win32.whl", hash = "sha256:838e4acd72da370ad07243898e886e93d3c0c9413f4444d600ba60a5cc206014"}, + {file = "asyncpg-0.26.0-cp310-cp310-win_amd64.whl", hash = "sha256:a254d09a3a989cc1839ba2c34448b879cdd017b528a0cda142c92fbb6c13d957"}, + {file = "asyncpg-0.26.0-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:3ecbe8ed3af4c739addbfbd78f7752866cce2c4e9cc3f953556e4960349ae360"}, + {file = "asyncpg-0.26.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f3ce7d8c0ab4639bbf872439eba86ef62dd030b245ad0e17c8c675d93d7a6b2d"}, + {file = "asyncpg-0.26.0-cp36-cp36m-musllinux_1_1_x86_64.whl", hash = "sha256:7129bd809990fd119e8b2b9982e80be7712bb6041cd082be3e415e60e5e2e98f"}, + {file = "asyncpg-0.26.0-cp36-cp36m-win32.whl", hash = "sha256:03f44926fa7ff7ccd59e98f05c7e227e9de15332a7da5bbcef3654bf468ee597"}, + {file = "asyncpg-0.26.0-cp36-cp36m-win_amd64.whl", hash = "sha256:b1f7b173af649b85126429e11a628d01a5b75973d2a55d64dba19ad8f0e9f904"}, + {file = "asyncpg-0.26.0-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:efe056fd22fc6ed5c1ab353b6510808409566daac4e6f105e2043797f17b8dad"}, + {file = "asyncpg-0.26.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d96cf93e01df9fb03cef5f62346587805e6c0ca6f654c23b8d35315bdc69af59"}, + {file = "asyncpg-0.26.0-cp37-cp37m-musllinux_1_1_x86_64.whl", hash = "sha256:235205b60d4d014921f7b1cdca0e19669a9a8978f7606b3eb8237ca95f8e716e"}, + {file = "asyncpg-0.26.0-cp37-cp37m-win32.whl", hash = "sha256:0de408626cfc811ef04f372debfcdd5e4ab5aeb358f2ff14d1bdc246ed6272b5"}, + {file = "asyncpg-0.26.0-cp37-cp37m-win_amd64.whl", hash = "sha256:f92d501bf213b16fabad4fbb0061398d2bceae30ddc228e7314c28dcc6641b79"}, + {file = "asyncpg-0.26.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:9acb22a7b6bcca0d80982dce3d67f267d43e960544fb5dd934fd3abe20c48014"}, + {file = "asyncpg-0.26.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e550d8185f2c4725c1e8d3c555fe668b41bd092143012ddcc5343889e1c2a13d"}, + {file = "asyncpg-0.26.0-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:050e339694f8c5d9aebcf326ca26f6622ef23963a6a3a4f97aeefc743954afd5"}, + {file = "asyncpg-0.26.0-cp38-cp38-win32.whl", hash = "sha256:b0c3f39ebfac06848ba3f1e280cb1fada7cc1229538e3dad3146e8d1f9deb92a"}, + {file = "asyncpg-0.26.0-cp38-cp38-win_amd64.whl", hash = "sha256:49fc7220334cc31d14866a0b77a575d6a5945c0fa3bb67f17304e8b838e2a02b"}, + {file = "asyncpg-0.26.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:d156e53b329e187e2dbfca8c28c999210045c45ef22a200b50de9b9e520c2694"}, + {file = "asyncpg-0.26.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4b4051012ca75defa9a1dc6b78185ca58cdc3a247187eb76a6bcf55dfaa2fad4"}, + {file = "asyncpg-0.26.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:6d60f15a0ac18c54a6ca6507c28599c06e2e87a0901e7b548f15243d71905b18"}, + {file = "asyncpg-0.26.0-cp39-cp39-win32.whl", hash = "sha256:ede1a3a2c377fe12a3930f4b4dd5340e8b32929541d5db027a21816852723438"}, + {file = "asyncpg-0.26.0-cp39-cp39-win_amd64.whl", hash = "sha256:8e1e79f0253cbd51fc43c4d0ce8804e46ee71f6c173fdc75606662ad18756b52"}, + {file = "asyncpg-0.26.0.tar.gz", hash = "sha256:77e684a24fee17ba3e487ca982d0259ed17bae1af68006f4cf284b23ba20ea2c"}, ] certifi = [ {file = "certifi-2021.10.8-py2.py3-none-any.whl", hash = "sha256:d62a0163eb4c2344ac042ab2bdf75399a71a2d8c7d47eac2e2ee91b9d6339569"}, @@ -619,8 +629,8 @@ colorama = [ {file = "colorama-0.4.4.tar.gz", hash = "sha256:5941b2b48a20143d2267e95b1c2a7603ce057ee39fd88e7329b0c292aa16869b"}, ] databases = [ - {file = "databases-0.5.5-py3-none-any.whl", hash = "sha256:97d9b9647216d1ab53ca61c059412b5c7b6e1f0bf8ce985477982ebcc7f278f3"}, - {file = "databases-0.5.5.tar.gz", hash = "sha256:02c6b016c1c951c21cca281dc8e2e002c60dc44026c0084aabbd8c37514aeb37"}, + {file = "databases-0.6.1-py3-none-any.whl", hash = "sha256:47fae85d82d8227049f08b154019913c3ad2f6057ceb0b5ebb36703be6f5666b"}, + {file = "databases-0.6.1.tar.gz", hash = "sha256:0a69c6983a27e10a5b75ffa094486f1febadd9d5a8db016e69b8c2f6a354dc30"}, ] deprecated = [ {file = "Deprecated-1.2.13-py2.py3-none-any.whl", hash = "sha256:64756e3e14c8c5eea9795d93c524551432a0be75629f8f29e67ab8caf076c76d"}, diff --git a/pyproject.toml b/pyproject.toml index 355bdbc..691d583 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,6 @@ python = "^3.9" fastapi = ">=0.77.1" httpx = ">=0.23.0" alembic = "^1.7.7" -ormar = {extras = ["postgresql"], version = "^0.11.0"} uvicorn = {extras = ["standart"], version = "^0.17.6"} arq = "^0.23a1" prometheus-fastapi-instrumentator = "^5.8.1" @@ -18,6 +17,7 @@ gunicorn = "^20.1.0" orjson = "^3.6.8" sentry-sdk = "^1.5.12" aioredis = "^2.0.1" +ormar = {git = "https://github.com/collerek/ormar.git", rev = "master", extras = ["postgresql"]} [tool.poetry.dev-dependencies] diff --git a/src/app/utils.py b/src/app/utils.py new file mode 100644 index 0000000..1e6ed57 --- /dev/null +++ b/src/app/utils.py @@ -0,0 +1,3 @@ +class DummyWriter: + def write(self, line): + return line diff --git a/src/app/views.py b/src/app/views.py index e91ad2c..79d22fa 100644 --- a/src/app/views.py +++ b/src/app/views.py @@ -1,5 +1,7 @@ import asyncio import base64 +import csv +from typing import AsyncIterator from fastapi import APIRouter, Depends, HTTPException, status, Request from fastapi.responses import StreamingResponse @@ -17,6 +19,7 @@ from app.services.caption_getter import get_caption from app.services.downloader import get_filename from app.services.files_client import download_file as download_file_from_cache from app.services.library_client import get_book +from app.utils import DummyWriter router = APIRouter( @@ -25,13 +28,15 @@ router = APIRouter( @router.get("/{object_id}/{object_type}", response_model=CachedFile) -async def get_cached_file(object_id: int, object_type: str): +async def get_cached_file(request: Request, object_id: int, object_type: str): cached_file = await CachedFileDB.objects.get_or_none( object_id=object_id, object_type=object_type ) if not cached_file: - cached_file = await cache_file_by_book_id({}, object_id, object_type) + cached_file = await cache_file_by_book_id( + {"redis": request.app.state.redis_client}, object_id, object_type + ) if not cached_file: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) @@ -40,13 +45,15 @@ async def get_cached_file(object_id: int, object_type: str): @router.get("/download/{object_id}/{object_type}") -async def download_cached_file(object_id: int, object_type: str): +async def download_cached_file(request: Request, object_id: int, object_type: str): cached_file = await CachedFileDB.objects.get_or_none( object_id=object_id, object_type=object_type ) if not cached_file: - cached_file = await cache_file_by_book_id({}, object_id, object_type) + cached_file = await cache_file_by_book_id( + {"redis": request.app.state.redis_client}, object_id, object_type + ) if not cached_file: raise HTTPException(status_code=status.HTTP_204_NO_CONTENT) @@ -117,6 +124,22 @@ async def update_cache(request: Request): return "Ok!" +@router.get("/download_dump") +async def download_dump(): + async def get_data() -> AsyncIterator[str]: + writer = csv.writer(DummyWriter()) + + async for c_file in CachedFileDB.objects.iterate(): + yield writer.writerow([c_file.object_id, c_file.object_type, c_file.data]) + + return StreamingResponse( + get_data(), + headers={ + "Content-Disposition": "attachment; filename=dump.csv", + }, + ) + + healthcheck_router = APIRouter( tags=["healthcheck"], ) diff --git a/src/core/app.py b/src/core/app.py index 5f44c47..a87274f 100644 --- a/src/core/app.py +++ b/src/core/app.py @@ -6,6 +6,7 @@ from prometheus_fastapi_instrumentator import Instrumentator from app.views import router, healthcheck_router from core.arq_pool import get_arq_pool from core.db import database +from core.redis_client import get_client import core.sentry # noqa: F401 @@ -24,6 +25,7 @@ def start_app() -> FastAPI: await database_.connect() app.state.arq_pool = await get_arq_pool() + app.state.redis_client = get_client() @app.on_event("shutdown") async def shutdown() -> None: diff --git a/src/core/redis_client.py b/src/core/redis_client.py new file mode 100644 index 0000000..609d9d4 --- /dev/null +++ b/src/core/redis_client.py @@ -0,0 +1,9 @@ +import aioredis + +from core.config import env_config + + +def get_client() -> aioredis.Redis: + return aioredis.Redis( + host=env_config.REDIS_HOST, port=env_config.REDIS_PORT, db=env_config.REDIS_DB + ) diff --git a/src/core/setup_arq.py b/src/core/setup_arq.py index 0fe9d57..07aad87 100644 --- a/src/core/setup_arq.py +++ b/src/core/setup_arq.py @@ -1,13 +1,11 @@ -import aioredis - from app.services.cache_updater import ( check_books, cache_file_by_book_id, check_books_page, ) from core.arq_pool import get_redis_settings, get_arq_pool -from core.config import env_config from core.db import database +from core.redis_client import get_client import core.sentry # noqa: F401 @@ -16,9 +14,7 @@ async def startup(ctx): await database.connect() ctx["arc_pool"] = await get_arq_pool() - ctx["redis"] = aioredis.Redis( - host=env_config.REDIS_HOST, port=env_config.REDIS_PORT, db=env_config.REDIS_DB - ) + ctx["redis"] = get_client() async def shutdown(ctx):