From f86c91b6a00f39a0c0af648d14ac6f5bb47e44b8 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Sun, 14 May 2023 16:21:07 +0200 Subject: [PATCH] Use uvicorn --- .pre-commit-config.yaml | 7 ++- poetry.lock | 88 +++++++++++-------------------------- pyproject.toml | 24 +++++----- scripts/healthcheck.py | 1 + scripts/start_production.sh | 2 +- src/app/views.py | 1 + src/core/app.py | 2 + src/core/auth.py | 1 + src/core/db.py | 1 + src/main.py | 1 + 10 files changed, 47 insertions(+), 81 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1f2d22e..839fe4a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,18 +2,17 @@ exclude: 'docs|node_modules|migrations|.git|.tox' repos: - repo: https://github.com/ambv/black - rev: 22.12.0 + rev: 23.3.0 hooks: - id: black language_version: python3.11 - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: 'v0.0.216' + rev: 'v0.0.267' hooks: - id: ruff - args: ["--force-exclude"] - repo: https://github.com/crate-ci/typos - rev: v1.13.6 + rev: typos-dict-v0.9.26 hooks: - id: typos diff --git a/poetry.lock b/poetry.lock index 94ecdda..85e529e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2,14 +2,14 @@ [[package]] name = "alembic" -version = "1.10.2" +version = "1.10.4" description = "A database migration tool for SQLAlchemy." category = "main" optional = false python-versions = ">=3.7" files = [ - {file = "alembic-1.10.2-py3-none-any.whl", hash = "sha256:8b48368f6533c064b39c024e1daba15ae7f947eac84185c28c06bbe1301a5497"}, - {file = "alembic-1.10.2.tar.gz", hash = "sha256:457eafbdc0769d855c2c92cbafe6b7f319f916c80cf4ed02b8f394f38b51b89d"}, + {file = "alembic-1.10.4-py3-none-any.whl", hash = "sha256:43942c3d4bf2620c466b91c0f4fca136fe51ae972394a0cc8b90810d664e4f5c"}, + {file = "alembic-1.10.4.tar.gz", hash = "sha256:295b54bbb92c4008ab6a7dcd1e227e668416d6f84b98b3c4446a2bc6214a556b"}, ] [package.dependencies] @@ -172,14 +172,14 @@ files = [ [[package]] name = "fastapi" -version = "0.95.0" +version = "0.95.1" description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production" category = "main" optional = false python-versions = ">=3.7" files = [ - {file = "fastapi-0.95.0-py3-none-any.whl", hash = "sha256:daf73bbe844180200be7966f68e8ec9fd8be57079dff1bacb366db32729e6eb5"}, - {file = "fastapi-0.95.0.tar.gz", hash = "sha256:99d4fdb10e9dd9a24027ac1d0bd4b56702652056ca17a6c8721eec4ad2f14e18"}, + {file = "fastapi-0.95.1-py3-none-any.whl", hash = "sha256:a870d443e5405982e1667dfe372663abf10754f246866056336d7f01c21dab07"}, + {file = "fastapi-0.95.1.tar.gz", hash = "sha256:9569f0a381f8a457ec479d90fa01005cfddaae07546eb1f3fa035bc4797ae7d5"}, ] [package.dependencies] @@ -287,27 +287,6 @@ files = [ [package.extras] docs = ["Sphinx"] -[[package]] -name = "gunicorn" -version = "20.1.0" -description = "WSGI HTTP Server for UNIX" -category = "main" -optional = false -python-versions = ">=3.5" -files = [ - {file = "gunicorn-20.1.0-py3-none-any.whl", hash = "sha256:9dcc4547dbb1cb284accfb15ab5667a0e5d1881cc443e0677b4882a4067a807e"}, - {file = "gunicorn-20.1.0.tar.gz", hash = "sha256:e0a968b5ba15f8a328fdfd7ab1fcb5af4470c28aaf7e55df02a99bc13138e6e8"}, -] - -[package.dependencies] -setuptools = ">=3.0" - -[package.extras] -eventlet = ["eventlet (>=0.24.1)"] -gevent = ["gevent (>=1.4.0)"] -setproctitle = ["setproctitle"] -tornado = ["tornado (>=0.2)"] - [[package]] name = "h11" version = "0.12.0" @@ -398,25 +377,25 @@ test = ["Cython (>=0.29.24,<0.30.0)"] [[package]] name = "httpx" -version = "0.23.3" +version = "0.24.0" description = "The next generation HTTP client." category = "main" optional = false python-versions = ">=3.7" files = [ - {file = "httpx-0.23.3-py3-none-any.whl", hash = "sha256:a211fcce9b1254ea24f0cd6af9869b3d29aba40154e947d2a07bb499b3e310d6"}, - {file = "httpx-0.23.3.tar.gz", hash = "sha256:9818458eb565bb54898ccb9b8b251a28785dd4a55afbc23d0eb410754fe7d0f9"}, + {file = "httpx-0.24.0-py3-none-any.whl", hash = "sha256:447556b50c1921c351ea54b4fe79d91b724ed2b027462ab9a329465d147d5a4e"}, + {file = "httpx-0.24.0.tar.gz", hash = "sha256:507d676fc3e26110d41df7d35ebd8b3b8585052450f4097401c9be59d928c63e"}, ] [package.dependencies] certifi = "*" -httpcore = ">=0.15.0,<0.17.0" -rfc3986 = {version = ">=1.3,<2", extras = ["idna2008"]} +httpcore = ">=0.15.0,<0.18.0" +idna = "*" sniffio = "*" [package.extras] brotli = ["brotli", "brotlicffi"] -cli = ["click (>=8.0.0,<9.0.0)", "pygments (>=2.0.0,<3.0.0)", "rich (>=10,<13)"] +cli = ["click (>=8.0.0,<9.0.0)", "pygments (>=2.0.0,<3.0.0)", "rich (>=10,<14)"] http2 = ["h2 (>=3,<5)"] socks = ["socksio (>=1.0.0,<2.0.0)"] @@ -795,39 +774,21 @@ files = [ {file = "PyYAML-6.0.tar.gz", hash = "sha256:68fb519c14306fec9720a2a5b45bc9f0c8d1b9c72adf45c37baedfcd949c35a2"}, ] -[[package]] -name = "rfc3986" -version = "1.5.0" -description = "Validating URI References per RFC 3986" -category = "main" -optional = false -python-versions = "*" -files = [ - {file = "rfc3986-1.5.0-py2.py3-none-any.whl", hash = "sha256:a86d6e1f5b1dc238b218b012df0aa79409667bb209e58da56d0b94704e712a97"}, - {file = "rfc3986-1.5.0.tar.gz", hash = "sha256:270aaf10d87d0d4e095063c65bf3ddbc6ee3d0b226328ce21e036f946e421835"}, -] - -[package.dependencies] -idna = {version = "*", optional = true, markers = "extra == \"idna2008\""} - -[package.extras] -idna2008 = ["idna"] - [[package]] name = "sentry-sdk" -version = "1.17.0" +version = "1.22.2" description = "Python client for Sentry (https://sentry.io)" category = "main" optional = false python-versions = "*" files = [ - {file = "sentry-sdk-1.17.0.tar.gz", hash = "sha256:ad40860325c94d1a656da70fba5a7c4dbb2f6809d3cc2d00f74ca0b608330f14"}, - {file = "sentry_sdk-1.17.0-py2.py3-none-any.whl", hash = "sha256:3c4e898f7a3edf5a2042cd0dcab6ee124e2112189228c272c08ad15d3850c201"}, + {file = "sentry-sdk-1.22.2.tar.gz", hash = "sha256:5932c092c6e6035584eb74d77064e4bce3b7935dfc4a331349719a40db265840"}, + {file = "sentry_sdk-1.22.2-py2.py3-none-any.whl", hash = "sha256:cf89a5063ef84278d186aceaed6fb595bfe67d099298e537634a323664265669"}, ] [package.dependencies] certifi = "*" -urllib3 = {version = ">=1.26.11", markers = "python_version >= \"3.6\""} +urllib3 = {version = ">=1.26.11,<2.0.0", markers = "python_version >= \"3.6\""} [package.extras] aiohttp = ["aiohttp (>=3.5)"] @@ -840,6 +801,7 @@ django = ["django (>=1.8)"] falcon = ["falcon (>=1.4)"] fastapi = ["fastapi (>=0.79.0)"] flask = ["blinker (>=1.1)", "flask (>=0.11)"] +grpcio = ["grpcio (>=1.21.1)"] httpx = ["httpx (>=0.16.0)"] huey = ["huey (>=2)"] opentelemetry = ["opentelemetry-distro (>=0.35b0)"] @@ -858,7 +820,7 @@ tornado = ["tornado (>=5)"] name = "setuptools" version = "65.5.1" description = "Easily download, build, install, upgrade, and uninstall Python packages" -category = "main" +category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -939,7 +901,7 @@ greenlet = {version = "!=0.4.17", markers = "python_version >= \"3\" and (platfo [package.extras] aiomysql = ["aiomysql", "greenlet (!=0.4.17)"] -aiosqlite = ["aiosqlite", "greenlet (!=0.4.17)", "typing_extensions (!=3.10.0.1)"] +aiosqlite = ["aiosqlite", "greenlet (!=0.4.17)", "typing-extensions (!=3.10.0.1)"] asyncio = ["greenlet (!=0.4.17)"] asyncmy = ["asyncmy (>=0.2.3,!=0.2.4)", "greenlet (!=0.4.17)"] mariadb-connector = ["mariadb (>=1.0.1,!=1.1.2)"] @@ -949,14 +911,14 @@ mssql-pyodbc = ["pyodbc"] mypy = ["mypy (>=0.910)", "sqlalchemy2-stubs"] mysql = ["mysqlclient (>=1.4.0)", "mysqlclient (>=1.4.0,<2)"] mysql-connector = ["mysql-connector-python"] -oracle = ["cx_oracle (>=7)", "cx_oracle (>=7,<8)"] +oracle = ["cx-oracle (>=7)", "cx-oracle (>=7,<8)"] postgresql = ["psycopg2 (>=2.7)"] postgresql-asyncpg = ["asyncpg", "greenlet (!=0.4.17)"] postgresql-pg8000 = ["pg8000 (>=1.16.6,!=1.29.0)"] postgresql-psycopg2binary = ["psycopg2-binary"] postgresql-psycopg2cffi = ["psycopg2cffi"] pymysql = ["pymysql", "pymysql (<1)"] -sqlcipher = ["sqlcipher3_binary"] +sqlcipher = ["sqlcipher3-binary"] [[package]] name = "starlette" @@ -1007,14 +969,14 @@ socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"] [[package]] name = "uvicorn" -version = "0.21.1" +version = "0.22.0" description = "The lightning-fast ASGI server." category = "main" optional = false python-versions = ">=3.7" files = [ - {file = "uvicorn-0.21.1-py3-none-any.whl", hash = "sha256:e47cac98a6da10cd41e6fd036d472c6f58ede6c5dbee3dbee3ef7a100ed97742"}, - {file = "uvicorn-0.21.1.tar.gz", hash = "sha256:0fac9cb342ba099e0d582966005f3fdba5b0290579fed4a6266dc702ca7bb032"}, + {file = "uvicorn-0.22.0-py3-none-any.whl", hash = "sha256:e9434d3bbf05f310e762147f769c9f21235ee118ba2d2bf1155a7196448bd996"}, + {file = "uvicorn-0.22.0.tar.gz", hash = "sha256:79277ae03db57ce7d9aa0567830bbb51d7a612f54d6e1e3e92da3ef24c2c8ed8"}, ] [package.dependencies] @@ -1210,4 +1172,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "23e8cdff5719490c05a0aea9c3de6b7af02e0f812ff0affde0e8445d1f2fdf24" +content-hash = "1ed2ece47ef1d2f6775bf9fc46ecfe528e5a5658b45529080d3cc0918ba2c5dc" diff --git a/pyproject.toml b/pyproject.toml index 1f89cc2..ff1b258 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,14 +6,13 @@ authors = ["Kurbanov Bulat "] [tool.poetry.dependencies] python = "^3.11" -fastapi = "^0.95.0" -alembic = "^1.10.2" +fastapi = "^0.95.1" +alembic = "^1.10.4" ormar = {extras = ["postgresql"], version = "^0.12.1"} -uvicorn = {extras = ["standard"], version = "^0.21.1"} -httpx = "^0.23.3" +uvicorn = {extras = ["standard"], version = "^0.22.0"} +httpx = "^0.24.0" uvloop = "^0.17.0" -gunicorn = "^20.1.0" -sentry-sdk = "^1.17.0" +sentry-sdk = "^1.22.2" [tool.poetry.group.dev.dependencies] pre-commit = "^2.21.0" @@ -58,14 +57,13 @@ max-complexity = 15 [tool.ruff.isort] known-first-party = ["core", "app"] force-sort-within-sections = true +force-wrap-aliases = true +section-order = ["future", "standard-library", "base_framework", "framework_ext", "third-party", "first-party", "local-folder"] +lines-after-imports = 2 -# only_sections = true -# force_sort_within_sections = true -# lines_after_imports = 2 -# lexicographical = true -# sections = ["FUTURE", "STDLIB", "BASEFRAMEWORK", "FRAMEWORKEXT", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"] -# known_baseframework = ["fastapi",] -# known_frameworkext = ["starlette",] +[tool.ruff.isort.sections] +base_framework = ["fastapi",] +framework_ext = ["starlette"] [tool.ruff.pyupgrade] keep-runtime-typing = true diff --git a/scripts/healthcheck.py b/scripts/healthcheck.py index 72c1840..e21d83a 100644 --- a/scripts/healthcheck.py +++ b/scripts/healthcheck.py @@ -2,6 +2,7 @@ import os import httpx + response = httpx.get( "http://localhost:8080/healthcheck", headers={"Authorization": os.environ["API_KEY"]}, diff --git a/scripts/start_production.sh b/scripts/start_production.sh index f9ed2c4..9a18394 100644 --- a/scripts/start_production.sh +++ b/scripts/start_production.sh @@ -1,3 +1,3 @@ cd /app alembic -c ./app/alembic.ini upgrade head -gunicorn -k uvicorn.workers.UvicornWorker main:app --bind 0.0.0.0:8080 +uvicorn main:app --host 0.0.0.0 --port 8080 --loop uvloop diff --git a/src/app/views.py b/src/app/views.py index 8591ffa..3019669 100644 --- a/src/app/views.py +++ b/src/app/views.py @@ -4,6 +4,7 @@ from app.depends import check_token from app.models import CachePrivileges, Service, Statuses from app.serializers import ServiceCreate, ServiceDetail + # TODO: add redis cache diff --git a/src/core/app.py b/src/core/app.py index d6cb477..6aec677 100644 --- a/src/core/app.py +++ b/src/core/app.py @@ -1,10 +1,12 @@ from fastapi import FastAPI + import sentry_sdk from app.views import router from core.config import env_config from core.db import database + sentry_sdk.init( env_config.SENTRY_DSN, ) diff --git a/src/core/auth.py b/src/core/auth.py index 61bfbb1..7cc07b5 100644 --- a/src/core/auth.py +++ b/src/core/auth.py @@ -1,3 +1,4 @@ from fastapi.security import APIKeyHeader + default_security = APIKeyHeader(name="Authorization") diff --git a/src/core/db.py b/src/core/db.py index 0f4b553..0879e69 100644 --- a/src/core/db.py +++ b/src/core/db.py @@ -5,6 +5,7 @@ from sqlalchemy import MetaData from core.config import env_config + DATABASE_URL = ( f"postgresql://{env_config.POSTGRES_USER}:{quote(env_config.POSTGRES_PASSWORD)}@" f"{env_config.POSTGRES_HOST}:{env_config.POSTGRES_PORT}/{env_config.POSTGRES_DB}" diff --git a/src/main.py b/src/main.py index 2739482..0a4385b 100644 --- a/src/main.py +++ b/src/main.py @@ -1,3 +1,4 @@ from core.app import start_app + app = start_app()