Initial commit

This commit is contained in:
urec56 2024-04-05 16:23:27 +05:00
commit 804564d6fa
22 changed files with 768 additions and 0 deletions

5
.dockerignore Normal file
View file

@ -0,0 +1,5 @@
.venv
venv
.idea
.gitignore
.git

19
.env_template Normal file
View file

@ -0,0 +1,19 @@
MODE=
DB_HOST=
DB_PORT=
DB_USER=
DB_PASS=
DB_NAME=
TEST_DB_HOST=
TEST_DB_PORT=
TEST_DB_USER=
TEST_DB_PASS=
TEST_DB_NAME=
REDIS_HOST=
REDIS_PORT=
REDIS_DB=
SHORTENER_HOST=

162
.gitignore vendored Normal file
View file

@ -0,0 +1,162 @@
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

15
Dockerfile Normal file
View file

@ -0,0 +1,15 @@
FROM python:3.11
RUN mkdir /url_shortener
WORKDIR /url_shortener
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
RUN alembic upgrade head
CMD ["gunicorn", "app.main:app", "--workers", "1", "--worker-class", "uvicorn.workers.UvicornWorker", "--bind=0.0.0.0:8000"]

9
LICENSE Normal file
View file

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2024 urec
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

32
README.md Normal file
View file

@ -0,0 +1,32 @@
# Сокращатель ссылок
Данный проект предоставляет `API` для сокращения ссылок и отслеживания переходов по
этим ссылкам.
Для работы данного сервиса необходима готовя БД `PostgreSQL`(продакшн и тестовая) и `Redis`.
## Начало работы
В корне проекта создаём файл `.env` и заполняем его по шаблону из файла `.env_template`. В поле `MODE` указываем `DEV`, а в поле `SHORTENER_HOST` - домен сервера, где будет хоститься сервис.
Собираем докер образ
```shell
docker compose build
```
Запускаем докер контейнер
```shell
docker compose up
```
По умолчанию сервис использует порт `8000` в контейнере и порт `8000` вне контейнера.
## Работа с `API`
Для получения сокращённой ссылки необходимо отправить `POST` запрос на эндпоинт `/zip_url`. Он вернёт новую ссылку в `JSON` объекте формата `{"new_url_path": <URL>}`.
При обращении по этому адресу будет выполнен редирект на оригинальный `URL` и в редис добавится таймстемп с указанием количества секунд от начала эпохи, когда произошёл редирект.
Получить данные счётчика редиректов можно при обращении к эндпоинту `/stats/<URL_ID>`, где `URL_ID` это айдишник, который указан в укороченном `URL`. Этот эндпоинт возвращает `JSON` объект формата `{"visit_times": <redirected_times>, "timestamps": [<redirect_timestamp>]}`.

116
alembic.ini Normal file
View file

@ -0,0 +1,116 @@
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = app/migrations
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
# for all available tokens
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory.
prepend_sys_path = .
# timezone to use when rendering the date within the migration file
# as well as the filename.
# If specified, requires the python>=3.9 or backports.zoneinfo library.
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
# string value is passed to ZoneInfo()
# leave blank for localtime
# timezone =
# max length of characters to apply to the
# "slug" field
# truncate_slug_length = 40
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false
# version location specification; This defaults
# to migrations/versions. When using multiple version
# directories, initial revisions must be specified with --version-path.
# The path separator used here should be the separator specified by "version_path_separator" below.
# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions
# version path separator; As mentioned above, this is the character used to split
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
# Valid values for version_path_separator are:
#
# version_path_separator = :
# version_path_separator = ;
# version_path_separator = space
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
# set to 'true' to search source files recursively
# in each "version_locations" directory
# new in Alembic version 1.10
# recursive_version_locations = false
# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8
sqlalchemy.url = driver://user:pass@localhost/dbname
[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples
# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks = black
# black.type = console_scripts
# black.entrypoint = black
# black.options = -l 79 REVISION_SCRIPT_FILENAME
# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
# hooks = ruff
# ruff.type = exec
# ruff.executable = %(here)s/.venv/bin/ruff
# ruff.options = --fix REVISION_SCRIPT_FILENAME
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

30
app/config.py Normal file
View file

@ -0,0 +1,30 @@
from typing import Literal
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=".env")
MODE: Literal["DEV", "TEST"]
DB_HOST: str
DB_PORT: int
DB_USER: str
DB_PASS: str
DB_NAME: str
TEST_DB_HOST: str
TEST_DB_PORT: int
TEST_DB_USER: str
TEST_DB_PASS: str
TEST_DB_NAME: str
REDIS_HOST: str
REDIS_PORT: int
REDIS_DB: int
SHORTENER_HOST: str
settings = Settings()

4
app/conftest.py Normal file
View file

@ -0,0 +1,4 @@
import os
os.environ["MODE"] = "TEST"

22
app/dao.py Normal file
View file

@ -0,0 +1,22 @@
from sqlalchemy import select, insert
from app.database import async_session_maker
from app.models import URLs
class URLsDAO:
@staticmethod
async def add_new_url(original_url: str) -> int:
async with async_session_maker() as session:
query = insert(URLs).values(original_url=original_url).returning(URLs.url_hash)
result = await session.execute(query)
await session.commit()
result = result.scalar()
return result
@staticmethod
async def find_by_hash(url_hash: int) -> URLs:
async with async_session_maker() as session:
query = select(URLs).filter_by(url_hash=url_hash)
result = await session.execute(query)
return result.scalar_one_or_none()

25
app/database.py Normal file
View file

@ -0,0 +1,25 @@
from sqlalchemy import NullPool
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import DeclarativeBase, sessionmaker
from app.config import settings
if settings.MODE == "TEST":
DATABASE_URL = f"""postgresql+asyncpg://{settings.TEST_DB_USER}:
{settings.TEST_DB_PASS}@{settings.TEST_DB_HOST}:
{settings.TEST_DB_PORT}/{settings.TEST_DB_NAME}"""
DATABASE_PARAMS = {"poolclass": NullPool}
else:
DATABASE_URL = f"""postgresql+asyncpg://{settings.DB_USER}:
{settings.DB_PASS}@{settings.DB_HOST}:
{settings.DB_PORT}/{settings.DB_NAME}"""
DATABASE_PARAMS = {}
engine = create_async_engine(DATABASE_URL, **DATABASE_PARAMS)
async_session_maker = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
class Base(DeclarativeBase):
pass

57
app/main.py Normal file
View file

@ -0,0 +1,57 @@
import time
from urllib.parse import urljoin
from fastapi import FastAPI, status, HTTPException
from fastapi.responses import RedirectResponse
import redis
from app.config import settings
from app.dao import URLsDAO
from app.migrations.shemas import SURL, SNewUrl, SGetStats
app = FastAPI(
title="Сокращатель ссылок",
)
r = redis.Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DB)
@app.post("/zip_url", response_model=SNewUrl)
async def zip_url(url: SURL):
url = str(url.url)
url_hash = await URLsDAO.add_new_url(original_url=url)
new_url_path = urljoin(settings.SHORTENER_HOST, str(url_hash))
return {"new_url_path": new_url_path}
@app.get("/{url_hash}", response_class=RedirectResponse, status_code=status.HTTP_307_TEMPORARY_REDIRECT)
async def redirect(url_hash: int):
if r.exists(str(url_hash)):
url_bytes = r.get(str(url_hash))
url = url_bytes.decode('utf-8')
else:
urls = await URLsDAO.find_by_hash(url_hash)
if not urls:
raise HTTPException(status_code=404, detail="Этой ссылки ещё не существует")
url = urls.original_url
r.set(str(url_hash), url)
r.expire(str(url_hash), 60)
list_key = f"visit_times:{url_hash}"
timestamp = int(time.time())
r.lpush(list_key, timestamp)
return RedirectResponse(url=url)
@app.get("/stats/{url_hash}", response_model=SGetStats)
async def get_stats(url_hash: int):
list_key = f"visit_times:{url_hash}"
if r.exists(list_key):
timestamps = r.lrange(list_key, 0, -1)
visit_times = len(timestamps)
return {"visit_times": visit_times, "timestamps": timestamps}
return {"visit_times": 0, "timestamps": []}

1
app/migrations/README Normal file
View file

@ -0,0 +1 @@
Generic single-database configuration.

87
app/migrations/env.py Normal file
View file

@ -0,0 +1,87 @@
import sys
from os.path import abspath, dirname
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
sys.path.insert(0, dirname(dirname(abspath(__file__))))
from app.database import DATABASE_URL, Base
from app.models import URLs # noqa
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
config.set_main_option('sqlalchemy.url', f'{DATABASE_URL}?async_fallback=True')
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

View file

@ -0,0 +1,26 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

14
app/migrations/shemas.py Normal file
View file

@ -0,0 +1,14 @@
from pydantic import BaseModel, HttpUrl
class SURL(BaseModel):
url: HttpUrl
class SNewUrl(BaseModel):
new_url_path: HttpUrl
class SGetStats(BaseModel):
visit_times: int
timestamps: list[int]

View file

@ -0,0 +1,34 @@
"""Database Creation
Revision ID: 6bf2a57525f2
Revises:
Create Date: 2024-04-03 21:18:04.796131
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '6bf2a57525f2'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('urls',
sa.Column('url_hash', sa.Integer(), nullable=False),
sa.Column('original_url', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('url_hash')
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('urls')
# ### end Alembic commands ###

10
app/models.py Normal file
View file

@ -0,0 +1,10 @@
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class URLs(Base):
__tablename__ = 'urls'
url_hash: Mapped[int] = mapped_column(primary_key=True)
original_url: Mapped[str]

41
app/tests/conftest.py Normal file
View file

@ -0,0 +1,41 @@
import json
import pytest
from sqlalchemy import insert
from httpx import AsyncClient
from app.config import settings
from app.database import Base, async_session_maker, engine
from app.models import URLs
from app.main import app as fastapi_app
@pytest.fixture(autouse=True, scope='module')
async def prepare_database():
assert settings.MODE == "TEST"
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
def open_mock_json(model: str):
with open(f"app/tests/mock_{model}.json", 'r', encoding='utf8') as file:
return json.load(file)
urls = open_mock_json("urls")
async with async_session_maker() as session:
add_users = insert(URLs).values(urls)
await session.execute(add_users)
await session.commit()
@pytest.fixture(scope="function")
async def ac():
async with AsyncClient(app=fastapi_app, base_url="http://test") as ac:
yield ac

12
docker-compose.yml Normal file
View file

@ -0,0 +1,12 @@
version: '3.8'
services:
url_shortener:
image: url_shortener
build:
context: .
container_name: url_shortener
ports:
- 8000:8000

4
pytest.ini Normal file
View file

@ -0,0 +1,4 @@
[pytest]
pythonpath = . app
asyncio_mode = auto
python_files = *_test.py *_tests.py test_*.py

43
requirements.txt Normal file
View file

@ -0,0 +1,43 @@
alembic==1.13.1
annotated-types==0.6.0
anyio==4.3.0
async-timeout==4.0.3
asyncpg==0.29.0
certifi==2024.2.2
click==8.1.7
dnspython==2.6.1
email_validator==2.1.1
fastapi==0.110.1
greenlet==3.0.3
gunicorn==21.2.0
h11==0.14.0
httpcore==1.0.5
httptools==0.6.1
httpx==0.27.0
idna==3.6
iniconfig==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.3
Mako==1.3.2
MarkupSafe==2.1.5
orjson==3.10.0
packaging==24.0
pluggy==1.4.0
pydantic==2.6.4
pydantic-extra-types==2.6.0
pydantic-settings==2.2.1
pydantic_core==2.16.3
pytest==8.1.1
python-dotenv==1.0.1
python-multipart==0.0.9
PyYAML==6.0.1
redis==5.0.3
sniffio==1.3.1
SQLAlchemy==2.0.29
starlette==0.37.2
typing_extensions==4.10.0
ujson==5.9.0
uvicorn==0.29.0
uvloop==0.19.0
watchfiles==0.21.0
websockets==12.0