From d36f0b05ca9804bd44502f8e5cfb1e065ca0bf3f Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 31 Dec 2023 15:50:28 +0800 Subject: [PATCH] init --- .dockerignore | 2 + .gitignore | 144 +++++++++++++++++++++++++++++++++++++++++++ .version | 1 + Dockerfile | 11 ++++ app/__init__.py | 2 + app/core/__init__.py | 2 + app/core/config.py | 36 +++++++++++ app/core/redis_.py | 15 +++++ main.py | 11 ++++ requirements.txt | 4 ++ utils/__init__.py | 2 + 11 files changed, 230 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 .version create mode 100644 Dockerfile create mode 100644 app/__init__.py create mode 100644 app/core/__init__.py create mode 100644 app/core/config.py create mode 100644 app/core/redis_.py create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 utils/__init__.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..389959e --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +.git +.idea \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eab0608 --- /dev/null +++ b/.gitignore @@ -0,0 +1,144 @@ +# 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 + +# 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/ + +*.db + +.idea +.vscode +log diff --git a/.version b/.version new file mode 100644 index 0000000..926f074 --- /dev/null +++ b/.version @@ -0,0 +1 @@ +testqw diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cc6869d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.10 + +COPY requirements.txt /tmp/requirements.txt + +RUN pip install -r /tmp/requirements.txt + +COPY . /app + +WORKDIR /app + +CMD ["python", "/app/main.py"] \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..c53f601 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python3 +# -*- coding:utf-8 -*- diff --git a/app/core/__init__.py b/app/core/__init__.py new file mode 100644 index 0000000..0f76362 --- /dev/null +++ b/app/core/__init__.py @@ -0,0 +1,2 @@ +from .config import settings +from .redis_ import kv_redis_client, redis_client diff --git a/app/core/config.py b/app/core/config.py new file mode 100644 index 0000000..4d3f292 --- /dev/null +++ b/app/core/config.py @@ -0,0 +1,36 @@ +import os + +from loguru import logger +from pydantic_settings import BaseSettings + + +class Settings(BaseSettings): + BASE_DIR: str = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + + VERSION: str | None = None + + redis_dsn: str = 'redis://:@localhost:6379/0' + kv_redis_dsn: str = 'redis://:password@localhost:6379/1' + mysql_dsn: str = 'mysql+pymysql://username:password@localhost:3306/data?charset=utf8mb4' + + @property + def version(self): + if self.VERSION: + return self.VERSION + if not os.path.exists(os.path.join(self.BASE_DIR, ".version")): + self.VERSION = "0.0.0" + return self.VERSION + with open(os.path.join(self.BASE_DIR, ".version"), "r") as f: + self.VERSION = f.read().strip() + return self.VERSION + + PROJECT_NAME: str = 'project_name' + + class Config: + case_sensitive = True + + +settings = Settings() + +if __name__ == '__main__': + logger.debug(settings.BASE_DIR) diff --git a/app/core/redis_.py b/app/core/redis_.py new file mode 100644 index 0000000..ba6b548 --- /dev/null +++ b/app/core/redis_.py @@ -0,0 +1,15 @@ +import redis +from loguru import logger + +from app.core.config import settings + +redis_client = redis.Redis.from_url(settings.redis_dsn) +kv_redis_client = redis.Redis.from_url(settings.kv_redis_dsn) + +if __name__ == '__main__': + kv_redis_client.set("name", "leo") + keys = kv_redis_client.keys("*") + logger.debug(keys) + kv_redis_client.delete("name") + keys = kv_redis_client.keys("*") + logger.debug(keys) diff --git a/main.py b/main.py new file mode 100644 index 0000000..649f624 --- /dev/null +++ b/main.py @@ -0,0 +1,11 @@ +from loguru import logger + +from app.core import settings + + +def main(): + logger.info(f"{settings.version=}") + + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ce8dd0f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +loguru +pydantic +redis +pydantic-settings \ No newline at end of file diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..c53f601 --- /dev/null +++ b/utils/__init__.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python3 +# -*- coding:utf-8 -*-