mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2025-09-11 22:30:41 +03:00
initial commit
This commit is contained in:
commit
4a5dfbff3a
71
.github/workflows/check-lint.yml
vendored
Normal file
71
.github/workflows/check-lint.yml
vendored
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
name: lint
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches-ignore:
|
||||||
|
- test
|
||||||
|
tags-ignore:
|
||||||
|
- "*"
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- 'release/**'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
#----------------------------------------------
|
||||||
|
# check-out repo and set-up python
|
||||||
|
#----------------------------------------------
|
||||||
|
- name: Check out repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Set up python
|
||||||
|
id: setup-python
|
||||||
|
uses: actions/setup-python@v3
|
||||||
|
with:
|
||||||
|
python-version: '3.11.3'
|
||||||
|
#----------------------------------------------
|
||||||
|
# ----- install & configure poetry -----
|
||||||
|
#----------------------------------------------
|
||||||
|
- name: Install poetry
|
||||||
|
env: # Keep in sync with `POETRY_VERSION` in `Dockerfile`
|
||||||
|
POETRY_VERSION: "1.4.2"
|
||||||
|
run: |
|
||||||
|
curl -sSL "https://install.python-poetry.org" | python -
|
||||||
|
# Adding `poetry` to `$PATH`:
|
||||||
|
echo "$HOME/.local/bin" >> $GITHUB_PATH
|
||||||
|
- name: Set up cache
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: .venv
|
||||||
|
key: venv-${{ matrix.python-version }}-${{ hashFiles('poetry.lock') }}
|
||||||
|
#----------------------------------------------
|
||||||
|
# load cached venv if cache exists
|
||||||
|
#----------------------------------------------
|
||||||
|
- name: Load cached venv
|
||||||
|
id: cached-poetry-dependencies
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: .venv
|
||||||
|
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
|
||||||
|
#----------------------------------------------
|
||||||
|
# install dependencies if cache does not exist
|
||||||
|
#----------------------------------------------
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
poetry config virtualenvs.in-project true
|
||||||
|
poetry install
|
||||||
|
poetry run pip install -U pip
|
||||||
|
#----------------------------------------------
|
||||||
|
# run test suite
|
||||||
|
#----------------------------------------------
|
||||||
|
- name: Analysing the code with mypy
|
||||||
|
run: |
|
||||||
|
source .venv/bin/activate
|
||||||
|
poetry run mypy app
|
||||||
|
- name: Analysing the code with flake8
|
||||||
|
run: |
|
||||||
|
poetry run flake8 app
|
||||||
|
- name: Analysing code with isort
|
||||||
|
run: |
|
||||||
|
poetry run isort --check-only app
|
76
.github/workflows/poetry-test.yml
vendored
Normal file
76
.github/workflows/poetry-test.yml
vendored
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
name: test
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches-ignore:
|
||||||
|
- test
|
||||||
|
tags-ignore:
|
||||||
|
- "*"
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- 'release/**'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
#----------------------------------------------
|
||||||
|
# check-out repo and set-up python
|
||||||
|
#----------------------------------------------
|
||||||
|
- name: Check out repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Set up python
|
||||||
|
id: setup-python
|
||||||
|
uses: actions/setup-python@v3
|
||||||
|
with:
|
||||||
|
python-version: '3.11.3'
|
||||||
|
#----------------------------------------------
|
||||||
|
# ----- install & configure poetry -----
|
||||||
|
#----------------------------------------------
|
||||||
|
- name: Install poetry
|
||||||
|
env: # Keep in sync with `POETRY_VERSION` in `Dockerfile`
|
||||||
|
POETRY_VERSION: "1.4.2"
|
||||||
|
run: |
|
||||||
|
curl -sSL "https://install.python-poetry.org" | python -
|
||||||
|
# Adding `poetry` to `$PATH`:
|
||||||
|
echo "$HOME/.local/bin" >> $GITHUB_PATH
|
||||||
|
|
||||||
|
- name: Set up cache
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: .venv
|
||||||
|
key: venv-${{ matrix.python-version }}-${{ hashFiles('poetry.lock') }}
|
||||||
|
#----------------------------------------------
|
||||||
|
# load cached venv if cache exists
|
||||||
|
#----------------------------------------------
|
||||||
|
- name: Load cached venv
|
||||||
|
id: cached-poetry-dependencies
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: .venv
|
||||||
|
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
|
||||||
|
#----------------------------------------------
|
||||||
|
# install dependencies if cache does not exist
|
||||||
|
#----------------------------------------------
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
poetry config virtualenvs.in-project true
|
||||||
|
poetry install
|
||||||
|
poetry run pip install -U pip
|
||||||
|
#----------------------------------------------
|
||||||
|
# run test suite
|
||||||
|
#----------------------------------------------
|
||||||
|
- name: Run tests
|
||||||
|
run: |
|
||||||
|
source .venv/bin/activate
|
||||||
|
poetry run pytest -vv --exitfirst
|
||||||
|
- name: Coverage report
|
||||||
|
run: |
|
||||||
|
poetry run coverage run -m pytest
|
||||||
|
poetry run coverage report
|
||||||
|
- name: Extended checks
|
||||||
|
run: |
|
||||||
|
poetry run poetry check
|
||||||
|
poetry run pip check
|
||||||
|
poetry run safety check --full-report
|
||||||
|
poetry run pip-audit
|
147
.gitignore
vendored
Normal file
147
.gitignore
vendored
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
### Python template
|
||||||
|
|
||||||
|
.idea/
|
||||||
|
.vscode/
|
||||||
|
# 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
|
||||||
|
*.db
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
||||||
|
__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/
|
||||||
|
|
||||||
|
# my staff
|
||||||
|
delete/
|
||||||
|
geckodriver
|
63
Makefile
Normal file
63
Makefile
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
# COLORS
|
||||||
|
GREEN := $(shell tput -Txterm setaf 2)
|
||||||
|
WHITE := $(shell tput -Txterm setaf 7)
|
||||||
|
YELLOW := $(shell tput -Txterm setaf 3)
|
||||||
|
RESET := $(shell tput -Txterm sgr0)
|
||||||
|
|
||||||
|
.DEFAULT_GOAL := help
|
||||||
|
.PHONY: help app format lint check-style check-import-sorting lint-typing lint-imports lint-complexity lint-deps
|
||||||
|
|
||||||
|
PY_TARGET_DIRS=app settings tests
|
||||||
|
PORT=8000
|
||||||
|
|
||||||
|
## Запустить приложение
|
||||||
|
app:
|
||||||
|
poetry run uvicorn --host 0.0.0.0 --factory app.main:create_app --port $(PORT) --reload
|
||||||
|
|
||||||
|
## Отформатировать код
|
||||||
|
format:
|
||||||
|
autoflake --recursive $(PY_TARGET_DIRS) --in-place --remove-unused-variables --remove-all-unused-imports --ignore-init-module-imports --remove-duplicate-keys --ignore-pass-statements
|
||||||
|
pyup_dirs --py311-plus $(PY_TARGET_DIRS) | true
|
||||||
|
isort --color --quiet $(PY_TARGET_DIRS)
|
||||||
|
black $(PY_TARGET_DIRS)
|
||||||
|
|
||||||
|
## Проверить стилистику кода
|
||||||
|
check-style:
|
||||||
|
black --check $(PY_TARGET_DIRS)
|
||||||
|
|
||||||
|
## Проверить сортировку импортов
|
||||||
|
check-import-sorting:
|
||||||
|
isort --check-only $(PY_TARGET_DIRS)
|
||||||
|
|
||||||
|
## Проверить типизацию
|
||||||
|
lint-typing:
|
||||||
|
mypy $(PY_TARGET_DIRS)
|
||||||
|
|
||||||
|
## Проверить код на сложность
|
||||||
|
lint-complexity:
|
||||||
|
flake8 $(PY_TARGET_DIRS)
|
||||||
|
|
||||||
|
## Запустить все линтеры
|
||||||
|
lint: lint-typing lint-complexity check-import-sorting
|
||||||
|
|
||||||
|
## Проверить зависимостей
|
||||||
|
lint-deps:
|
||||||
|
safety check --full-report && pip-audit
|
||||||
|
|
||||||
|
## Show help
|
||||||
|
help:
|
||||||
|
@echo ''
|
||||||
|
@echo 'Usage:'
|
||||||
|
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
|
||||||
|
@echo ''
|
||||||
|
@echo 'Targets:'
|
||||||
|
@awk '/^[a-zA-Z\-_0-9]+:/ { \
|
||||||
|
helpMessage = match(lastLine, /^## (.*)/); \
|
||||||
|
if (helpMessage) { \
|
||||||
|
helpCommand = $$1; sub(/:$$/, "", helpCommand); \
|
||||||
|
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
|
||||||
|
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)25s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
{ lastLine = $$0 }' $(MAKEFILE_LIST)
|
||||||
|
@echo ''
|
62
README.md
Normal file
62
README.md
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
# MosGotTrans bot
|
||||||
|
Бот для получения расписания конкретных автобусов для конкретных остановок
|
||||||
|
|
||||||
|
Использует **Selenium** для парсинга сайта "яндекс карты"
|
||||||
|
|
||||||
|
## Install & Update
|
||||||
|
|
||||||
|
install service
|
||||||
|
|
||||||
|
sudo cp scripts/mosgortrans.service /etc/systemd/system
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd ~/PycharmProjects/mosgortrans
|
||||||
|
sudo systemctl stop healthcheck_bot.service
|
||||||
|
git pull balshgit master
|
||||||
|
udo rsync -a --delete --progress ~/mosgortrans/* /opt/mosgortrans/ --exclude .git
|
||||||
|
sudo systemctl start healthcheck_bot.service
|
||||||
|
```
|
||||||
|
|
||||||
|
## Local start
|
||||||
|
```bash
|
||||||
|
python main.py
|
||||||
|
```
|
||||||
|
|
||||||
|
- set `START_WITH_WEBHOOK` to blank
|
||||||
|
|
||||||
|
## Delete or set webhook manually
|
||||||
|
|
||||||
|
url: https://api.telegram.org/bot{TELEGRAM_TOKEN}/{method}Webhook?url={WEBHOOK_URL}
|
||||||
|
|
||||||
|
methods:
|
||||||
|
- delete
|
||||||
|
- set
|
||||||
|
|
||||||
|
|
||||||
|
## Local development clean:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
killall geckodriver
|
||||||
|
killall firefox
|
||||||
|
killall python
|
||||||
|
```
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd tests
|
||||||
|
SELENOIDTEST=1 docker-compose run test-bot python -m pytest tests/bot/test_bot_selenoid.py::test_selenoid_text -vv
|
||||||
|
```
|
||||||
|
|
||||||
|
## Help article
|
||||||
|
|
||||||
|
[Пишем асинхронного Телеграм-бота](https://habr.com/ru/company/kts/blog/598575/)
|
||||||
|
|
||||||
|
[fast_api_aiogram](https://programtalk.com/vs4/python/daya0576/he-weather-bot/telegram_bot/dependencies.py/)
|
||||||
|
|
||||||
|
## TODO
|
||||||
|
|
||||||
|
- [x] Добавить очередь сообщений
|
||||||
|
- [x] Исправить запуск локально
|
||||||
|
- [x] Добавить тестов
|
||||||
|
- [x] Close connection
|
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
58
app/main.py
Normal file
58
app/main.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.responses import UJSONResponse
|
||||||
|
|
||||||
|
from app.routers import api_router
|
||||||
|
from settings.config import Settings, get_settings
|
||||||
|
|
||||||
|
|
||||||
|
class Application:
|
||||||
|
def __init__(self, settings: Settings) -> None:
|
||||||
|
self.app = FastAPI(
|
||||||
|
title='Health check bot',
|
||||||
|
description='Bot which check all services are working',
|
||||||
|
version='0.0.1',
|
||||||
|
docs_url=None,
|
||||||
|
redoc_url=None,
|
||||||
|
openapi_url=f'{settings.WEBHOOK_PATH}/api/openapi.json',
|
||||||
|
default_response_class=UJSONResponse,
|
||||||
|
)
|
||||||
|
self.app.state.settings = settings
|
||||||
|
self.settings = settings
|
||||||
|
self.app.include_router(api_router)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def fastapi_app(self) -> FastAPI:
|
||||||
|
return self.app
|
||||||
|
|
||||||
|
def configure_hooks(self) -> None:
|
||||||
|
self.app.add_event_handler("startup", self.connect_databases) # type: ignore
|
||||||
|
self.app.add_event_handler("startup", self.create_redis_cluster) # type: ignore
|
||||||
|
|
||||||
|
self.app.add_event_handler("shutdown", self.disconnect_databases) # type: ignore
|
||||||
|
self.app.add_event_handler("shutdown", self.close_redis_cluster) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
def create_app(settings: Settings | None = None) -> FastAPI:
|
||||||
|
settings = settings or get_settings()
|
||||||
|
|
||||||
|
return Application(settings=settings).fastapi_app
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
import uvicorn
|
||||||
|
|
||||||
|
app = create_app()
|
||||||
|
|
||||||
|
"""Entrypoint of the application."""
|
||||||
|
uvicorn.run(
|
||||||
|
"app.main:create_app",
|
||||||
|
workers=app.state.settings.WORKERS_COUNT,
|
||||||
|
host=app.state.settings.APP_HOST,
|
||||||
|
port=app.state.settings.APP_PORT,
|
||||||
|
reload=app.state.settings.RELOAD,
|
||||||
|
factory=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
4
app/routers.py
Normal file
4
app/routers.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from fastapi import APIRouter
|
||||||
|
from fastapi.responses import ORJSONResponse
|
||||||
|
|
||||||
|
api_router = APIRouter(prefix="/api", default_response_class=ORJSONResponse)
|
66
deploy/Dockerfile
Normal file
66
deploy/Dockerfile
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
FROM python:3.11.3
|
||||||
|
|
||||||
|
ARG USER
|
||||||
|
|
||||||
|
ENV USER=${USER} \
|
||||||
|
PYTHONFAULTHANDLER=1 \
|
||||||
|
PYTHONUNBUFFERED=1 \
|
||||||
|
PYTHONHASHSEED=random \
|
||||||
|
PYTHONDONTWRITEBYTECODE=1 \
|
||||||
|
# pip:
|
||||||
|
PIP_NO_CACHE_DIR=off \
|
||||||
|
PIP_DISABLE_PIP_VERSION_CHECK=on \
|
||||||
|
PIP_DEFAULT_TIMEOUT=100 \
|
||||||
|
POETRY_VIRTUALENVS_CREATE=false \
|
||||||
|
POETRY_CACHE_DIR='/var/cache/pypoetry' \
|
||||||
|
PATH="$PATH:/root/.poetry/bin" \
|
||||||
|
DOCKER_CONTAINER=1 \
|
||||||
|
POETRY_VERSION=1.4.2
|
||||||
|
|
||||||
|
|
||||||
|
RUN printf "================\n\nStart build app. USER is: "${USER}"\n\n===============\n" \
|
||||||
|
&& apt-get update \
|
||||||
|
&& apt-get install --no-install-recommends -y \
|
||||||
|
bash \
|
||||||
|
build-essential \
|
||||||
|
curl \
|
||||||
|
iputils-ping \
|
||||||
|
gettext \
|
||||||
|
git \
|
||||||
|
libpq-dev \
|
||||||
|
nano \
|
||||||
|
&& pip install --upgrade pip \
|
||||||
|
# Installing `poetry` package manager:
|
||||||
|
&& pip install poetry==$POETRY_VERSION wheel \
|
||||||
|
# Cleaning cache:
|
||||||
|
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
|
||||||
|
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN if [ "$USER" != "root" ]; then \
|
||||||
|
mkdir /home/"$USER" \
|
||||||
|
&& groupadd -r "$USER" && useradd -d /home/"$USER" -r -g "$USER" "$USER" \
|
||||||
|
&& chown "$USER":"$USER" -R /home/"$USER"; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
COPY --chown="$USER":"$USER" ./poetry.lock ./pyproject.toml /app/
|
||||||
|
|
||||||
|
# Installing requirements
|
||||||
|
RUN poetry --version \
|
||||||
|
&& poetry run pip install -U pip \
|
||||||
|
&& poetry install \
|
||||||
|
$(if [ "$USER" != 'root' ]; then echo '--only main'; fi) \
|
||||||
|
--no-interaction --no-ansi \
|
||||||
|
# Cleaning poetry installation's cache for production:
|
||||||
|
&& if [ "$USER" != 'root' ]; then rm -rf "$POETRY_CACHE_DIR"; fi
|
||||||
|
|
||||||
|
COPY --chown="$USER":"$USER" ./app /app/
|
||||||
|
COPY ./scripts/start-bot.sh .
|
||||||
|
RUN chmod +x ./start-bot.sh
|
||||||
|
|
||||||
|
USER "$USER"
|
||||||
|
|
||||||
|
# Copying actuall application
|
||||||
|
COPY --chown="$USER":"$USER" . /app/
|
48
docker-compose.yml
Normal file
48
docker-compose.yml
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
version: '3.9'
|
||||||
|
|
||||||
|
networks:
|
||||||
|
healthcheck_bot_network:
|
||||||
|
name:
|
||||||
|
"healthcheck_bot_network"
|
||||||
|
|
||||||
|
services:
|
||||||
|
|
||||||
|
bot:
|
||||||
|
container_name: "healthcheck_bot"
|
||||||
|
hostname: "healthcheck_bot"
|
||||||
|
image: "healthcheck_bot:latest"
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: deploy/Dockerfile
|
||||||
|
args:
|
||||||
|
USER: ${USER}
|
||||||
|
restart: unless-stopped
|
||||||
|
env_file:
|
||||||
|
- settings/.env
|
||||||
|
volumes:
|
||||||
|
- /etc/localtime:/etc/localtime:ro
|
||||||
|
networks:
|
||||||
|
- healthcheck_bot_network
|
||||||
|
expose:
|
||||||
|
- "8080"
|
||||||
|
command: bash start-bot.sh
|
||||||
|
|
||||||
|
caddy:
|
||||||
|
image: "caddy:2.6.4"
|
||||||
|
container_name: healthcheck_bot_caddy
|
||||||
|
hostname: healthcheck_bot_caddy
|
||||||
|
restart: unless-stopped
|
||||||
|
env_file:
|
||||||
|
- app/config/.env
|
||||||
|
ports:
|
||||||
|
- '8084:8084'
|
||||||
|
depends_on:
|
||||||
|
- bot
|
||||||
|
- selenoid
|
||||||
|
volumes:
|
||||||
|
- ./deploy/Caddyfile:/etc/caddy/Caddyfile:ro
|
||||||
|
- healthcheck_bot_caddy-data:/data
|
||||||
|
- healthcheck_bot_caddy-config:/config
|
||||||
|
networks:
|
||||||
|
healthcheck_bot_network:
|
||||||
|
ipv4_address: 200.20.0.12
|
67
lefthook.yml
Normal file
67
lefthook.yml
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
# Refer for explanation to following link:
|
||||||
|
# https://github.com/evilmartians/lefthook/blob/master/docs/full_guide.md
|
||||||
|
|
||||||
|
# NOTE: use `lefthook install` when you make change in stages (groups like pre-commit, format, etc.)
|
||||||
|
|
||||||
|
pre-commit:
|
||||||
|
commands:
|
||||||
|
format:
|
||||||
|
run: lefthook run format
|
||||||
|
lint:
|
||||||
|
run: lefthook run lint
|
||||||
|
check-format:
|
||||||
|
run: lefthook run check-format
|
||||||
|
|
||||||
|
pre-push:
|
||||||
|
parallel: true
|
||||||
|
commands:
|
||||||
|
isort:
|
||||||
|
glob: "*.py"
|
||||||
|
run: isort --check-only {all_files}
|
||||||
|
black:
|
||||||
|
glob: "*.py"
|
||||||
|
run: black --check -S {all_files}
|
||||||
|
mypy:
|
||||||
|
glob: "*.py"
|
||||||
|
run: mypy {all_files} --namespace-packages
|
||||||
|
flake8:
|
||||||
|
glob: "*.py"
|
||||||
|
run: flake8 {all_files}
|
||||||
|
|
||||||
|
format:
|
||||||
|
piped: true
|
||||||
|
parallel: false
|
||||||
|
commands:
|
||||||
|
# number prefix used to save ordering
|
||||||
|
1_autoflake:
|
||||||
|
glob: "*.py"
|
||||||
|
run: autoflake --recursive --ignore-init-module-imports --remove-all-unused-imports --remove-unused-variables --in-place {staged_files}
|
||||||
|
2_isort:
|
||||||
|
glob: "*.py"
|
||||||
|
run: isort --color --quiet {staged_files}
|
||||||
|
3_black:
|
||||||
|
glob: "*.py"
|
||||||
|
run: black -S {staged_files}
|
||||||
|
4_black_check:
|
||||||
|
glob: "*.py"
|
||||||
|
run: black --check -S {staged_files}
|
||||||
|
|
||||||
|
lint:
|
||||||
|
parallel: true
|
||||||
|
commands:
|
||||||
|
mypy:
|
||||||
|
glob: "*.py"
|
||||||
|
run: mypy {staged_files} --namespace-packages
|
||||||
|
flake8:
|
||||||
|
glob: "*.py"
|
||||||
|
run: flake8 {staged_files}
|
||||||
|
|
||||||
|
check-format:
|
||||||
|
parallel: true
|
||||||
|
commands:
|
||||||
|
isort:
|
||||||
|
glob: "*.py"
|
||||||
|
run: isort --check-only {staged_files}
|
||||||
|
black:
|
||||||
|
glob: "*.py"
|
||||||
|
run: black --check -S {staged_files}
|
2799
poetry.lock
generated
Normal file
2799
poetry.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
105
pyproject.toml
Normal file
105
pyproject.toml
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
[tool.poetry]
|
||||||
|
name = "mosgortrans"
|
||||||
|
version = "0.7.1"
|
||||||
|
description = "Bot to help with mosgortans"
|
||||||
|
authors = ["Dmitry Afanasyev <Balshbox@gmail.com>"]
|
||||||
|
|
||||||
|
[tool.poetry.dependencies]
|
||||||
|
|
||||||
|
fastapi = "^0.95"
|
||||||
|
python-telegram-bot="^20.2"
|
||||||
|
|
||||||
|
python = "^3.11"
|
||||||
|
python-dotenv = "^1.0"
|
||||||
|
httpx = "^0.23"
|
||||||
|
loguru = "^0.7"
|
||||||
|
pydantic = "^1.10.7"
|
||||||
|
gunicorn = "^20.1"
|
||||||
|
uvicorn = "^0.21"
|
||||||
|
wheel = "^0.40"
|
||||||
|
orjson = "^3.8"
|
||||||
|
|
||||||
|
[tool.poetry.dev-dependencies]
|
||||||
|
|
||||||
|
factory-boy = "^3.2"
|
||||||
|
Faker = "^18.3.0"
|
||||||
|
|
||||||
|
ipython = "^8.12.0"
|
||||||
|
|
||||||
|
safety = "^2.3"
|
||||||
|
pip-audit = "^2.5"
|
||||||
|
yamllint = "^1.30"
|
||||||
|
tomlkit = "^0.11.7"
|
||||||
|
bandit = "^1.7"
|
||||||
|
aresponses = "^2.1"
|
||||||
|
|
||||||
|
pyupgrade = "^3.3"
|
||||||
|
isort = "^5.12"
|
||||||
|
black = "^23.3"
|
||||||
|
|
||||||
|
mypy = "^1.2"
|
||||||
|
types-PyMySQL = "^1.0"
|
||||||
|
types-python-dateutil = "^2.8"
|
||||||
|
|
||||||
|
pytest = "^7.2"
|
||||||
|
pytest-asyncio = "^0.21"
|
||||||
|
pytest-deadfixtures = "^2.2"
|
||||||
|
pytest-testmon = "^2.0"
|
||||||
|
pytest-mock = "^3.10"
|
||||||
|
pytest-cov = "^4.0"
|
||||||
|
pytest-timeout = "^2.1"
|
||||||
|
pytest-sugar = "^0.9"
|
||||||
|
pytest-clarity = "^1.0"
|
||||||
|
pytest-env = "^0.8"
|
||||||
|
nest-asyncio = "^1.5"
|
||||||
|
|
||||||
|
autoflake = "^1.7"
|
||||||
|
flake8 = "^5.0.4"
|
||||||
|
flake8-logging-format = "^0.9"
|
||||||
|
flake8-comprehensions = "^3.11"
|
||||||
|
flake8-eradicate = "^1.4"
|
||||||
|
flake8-pytest-style = "^1.7"
|
||||||
|
flake8-aaa = "^0.14"
|
||||||
|
flake8-bugbear = "^23.3"
|
||||||
|
flake8-debugger = "^4.1"
|
||||||
|
flake8-expression-complexity = "^0.0.11"
|
||||||
|
flake8-fixme = "^1.1"
|
||||||
|
flake8-simplify = "^0.20"
|
||||||
|
flake8-variables-names = "^0.0.5"
|
||||||
|
flake8-bandit = "^4.1"
|
||||||
|
flake8-tidy-imports = "^4.8"
|
||||||
|
|
||||||
|
|
||||||
|
[tool.isort]
|
||||||
|
profile = "black"
|
||||||
|
multi_line_output = 3
|
||||||
|
src_paths = ["app", "settings", "tests"]
|
||||||
|
combine_as_imports = true
|
||||||
|
|
||||||
|
|
||||||
|
[tool.mypy]
|
||||||
|
python_version = "3.11"
|
||||||
|
strict = true
|
||||||
|
ignore_missing_imports = true
|
||||||
|
allow_subclassing_any = true
|
||||||
|
allow_untyped_calls = true
|
||||||
|
pretty = true
|
||||||
|
show_error_codes = true
|
||||||
|
implicit_reexport = true
|
||||||
|
allow_untyped_decorators = true
|
||||||
|
warn_return_any = false
|
||||||
|
|
||||||
|
|
||||||
|
[tool.coverage.run]
|
||||||
|
relative_files = true
|
||||||
|
|
||||||
|
[tool.pytest.ini_options]
|
||||||
|
filterwarnings = [
|
||||||
|
"error",
|
||||||
|
"ignore::DeprecationWarning",
|
||||||
|
]
|
||||||
|
|
||||||
|
[tool.black]
|
||||||
|
line-length = 120
|
||||||
|
skip-string-normalization = true
|
||||||
|
target-version = ['py311']
|
11
scripts/healthcheck_bot.service
Normal file
11
scripts/healthcheck_bot.service
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Healthcheck bot
|
||||||
|
Wants=network-online.target
|
||||||
|
After=network-online.target
|
||||||
|
[Service]
|
||||||
|
Restart=always
|
||||||
|
WorkingDirectory=/opt/healthcheck_bot
|
||||||
|
ExecStart=/usr/local/bin/docker-compose -f /opt/healthcheck_bot/docker-compose.yml up
|
||||||
|
ExecStop=/usr/local/bin/docker-compose -f /opt/healthcheck_bot/docker-compose.yml down
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
17
scripts/start-bot.sh
Normal file
17
scripts/start-bot.sh
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
echo "starting the bot"
|
||||||
|
|
||||||
|
if [[ "${START_WITH_WEBHOOK}" == "true" ]]
|
||||||
|
then
|
||||||
|
echo "Starting bot in webhook mode..."
|
||||||
|
gunicorn main:create_app \
|
||||||
|
--bind ${WEBAPP_HOST}:${WEBAPP_PORT} \
|
||||||
|
--worker-class aiohttp.GunicornWebWorker \
|
||||||
|
--timeout 150 \
|
||||||
|
--max-requests 2000 \
|
||||||
|
--max-requests-jitter 400
|
||||||
|
else
|
||||||
|
echo "Starting bot in polling mode..."
|
||||||
|
python main.py
|
||||||
|
fi
|
10
settings/.env.ci.runtests
Normal file
10
settings/.env.ci.runtests
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
STAGE="runtests"
|
||||||
|
|
||||||
|
APP_HOST="0.0.0.0"
|
||||||
|
APP_PORT="8000"
|
||||||
|
|
||||||
|
POSTGRES_HOST="postgres"
|
||||||
|
POSTGRES_PORT="5432"
|
||||||
|
POSTGRES_DB="relevancer"
|
||||||
|
POSTGRES_USER="user"
|
||||||
|
POSTGRES_PASSWORD="postgrespwd"
|
1
settings/.env.local.runtests
Normal file
1
settings/.env.local.runtests
Normal file
@ -0,0 +1 @@
|
|||||||
|
STAGE="runtests"
|
8
settings/.env.staging
Normal file
8
settings/.env.staging
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
APP_HOST="0.0.0.0"
|
||||||
|
APP_PORT="8000"
|
||||||
|
|
||||||
|
POSTGRES_HOST="postgres"
|
||||||
|
POSTGRES_PORT="5432"
|
||||||
|
POSTGRES_DB="relevancer"
|
||||||
|
POSTGRES_USER="user"
|
||||||
|
POSTGRES_PASSWORD="postgrespwd"
|
22
settings/.env.template
Normal file
22
settings/.env.template
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
STAGE="dev"
|
||||||
|
|
||||||
|
APP_HOST="0.0.0.0"
|
||||||
|
APP_PORT="8000"
|
||||||
|
|
||||||
|
USER="web"
|
||||||
|
|
||||||
|
TELEGRAM_API_TOKEN="123456789:AABBCCDDEEFFaabbccddeeff-1234567890"
|
||||||
|
|
||||||
|
# webhook settings
|
||||||
|
WEBHOOK_HOST="https://mydomain.com"
|
||||||
|
WEBHOOK_PATH="/healthcheck"
|
||||||
|
|
||||||
|
# set to true to start with webhook. Else bot will start on polling method
|
||||||
|
START_WITH_WEBHOOK="false"
|
||||||
|
|
||||||
|
# quantity of workers for uvicorn
|
||||||
|
WORKERS_COUNT=1
|
||||||
|
# Enable uvicorn reloading
|
||||||
|
RELOAD="true"
|
||||||
|
DEBUG="true"
|
||||||
|
|
0
settings/__init__.py
Normal file
0
settings/__init__.py
Normal file
50
settings/config.py
Normal file
50
settings/config.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
from os import environ
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from pydantic import BaseSettings
|
||||||
|
|
||||||
|
BASE_DIR = Path(__file__).parent.parent
|
||||||
|
SHARED_DIR = BASE_DIR.resolve().joinpath('shared')
|
||||||
|
SHARED_DIR.mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
SHARED_DIR.joinpath('logs').mkdir(exist_ok=True)
|
||||||
|
DIR_LOGS = SHARED_DIR.joinpath('logs')
|
||||||
|
|
||||||
|
env_path = f"{BASE_DIR}/settings/.env"
|
||||||
|
|
||||||
|
if environ.get("STAGE") == "runtests":
|
||||||
|
if "LOCALTEST" in environ:
|
||||||
|
env_path = f"{BASE_DIR}/settings/.env.local.runtests"
|
||||||
|
else:
|
||||||
|
env_path = f"{BASE_DIR}/settings/.env.ci.runtests"
|
||||||
|
|
||||||
|
load_dotenv(env_path, override=True)
|
||||||
|
|
||||||
|
|
||||||
|
class Settings(BaseSettings):
|
||||||
|
"""Application settings."""
|
||||||
|
|
||||||
|
PROJECT_NAME: str = "healthcheck bot"
|
||||||
|
APP_HOST: str = "0.0.0.0"
|
||||||
|
APP_PORT: int = 8082
|
||||||
|
STAGE: str = "dev"
|
||||||
|
DEBUG: bool = False
|
||||||
|
|
||||||
|
TELEGRAM_API_TOKEN: str = "123456789:AABBCCDDEEFFaabbccddeeff-1234567890"
|
||||||
|
START_WITH_WEBHOOK: bool = False
|
||||||
|
# webhook settings
|
||||||
|
WEBHOOK_HOST: str = "https://mydomain.com"
|
||||||
|
WEBHOOK_PATH: str = "/healthcheck"
|
||||||
|
|
||||||
|
# quantity of workers for uvicorn
|
||||||
|
WORKERS_COUNT: int = 1
|
||||||
|
# Enable uvicorn reloading
|
||||||
|
RELOAD: bool = False
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
case_sensitive = True
|
||||||
|
|
||||||
|
|
||||||
|
def get_settings() -> Settings:
|
||||||
|
return Settings()
|
54
setup.cfg
Normal file
54
setup.cfg
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
[flake8]
|
||||||
|
statistics = False
|
||||||
|
inline-quotes = single
|
||||||
|
max-line-length = 120
|
||||||
|
max-expression-complexity = 10
|
||||||
|
max-complexity = 10
|
||||||
|
ban-relative-imports = parents
|
||||||
|
nested-classes-whitelist = Config, Meta
|
||||||
|
docstring_style=reStructuredText
|
||||||
|
exclude =
|
||||||
|
tests/*,
|
||||||
|
./.git,
|
||||||
|
./venv,
|
||||||
|
ignore =
|
||||||
|
; use isort instead
|
||||||
|
I,
|
||||||
|
; use black style
|
||||||
|
E203, W
|
||||||
|
G004,
|
||||||
|
VNE003,
|
||||||
|
; conflict with fastapi di
|
||||||
|
B008,
|
||||||
|
per-file-ignores =
|
||||||
|
; too complex queries
|
||||||
|
./app/tests/*: TAE001, S101, S311
|
||||||
|
tests/*/factories/*: S5720
|
||||||
|
app/main.py: E402
|
||||||
|
settings/config.py: S104
|
||||||
|
|
||||||
|
|
||||||
|
[mypy]
|
||||||
|
# Mypy configuration:
|
||||||
|
# https://mypy.readthedocs.io/en/latest/config_file.html
|
||||||
|
python_version = 3.10
|
||||||
|
allow_redefinition = False
|
||||||
|
check_untyped_defs = True
|
||||||
|
disallow_untyped_decorators = False
|
||||||
|
disallow_any_explicit = False
|
||||||
|
disallow_any_generics = True
|
||||||
|
disallow_untyped_calls = True
|
||||||
|
disallow_untyped_defs = True
|
||||||
|
ignore_errors = False
|
||||||
|
ignore_missing_imports = True
|
||||||
|
implicit_reexport = False
|
||||||
|
local_partial_types = True
|
||||||
|
strict_optional = True
|
||||||
|
strict_equality = True
|
||||||
|
show_error_codes = True
|
||||||
|
no_implicit_optional = True
|
||||||
|
warn_unused_ignores = True
|
||||||
|
warn_redundant_casts = True
|
||||||
|
warn_unused_configs = True
|
||||||
|
warn_unreachable = True
|
||||||
|
warn_no_return = True
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
Loading…
x
Reference in New Issue
Block a user