diff --git a/.gitignore b/.gitignore index 5d381cc..8cb2598 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +.vscode +.history +*.db* + # ---> Python # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1369e34 --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +BIN = ./.venv/bin/ +PYTHON = ./.venv/bin/python +PIP = ./.venv/bin/pip +APP_NAME=random-name + +all: install build + +ensure_repo: + -@git init + +ensure_env: ensure_repo + -@python3 -m venv .venv + +install: ensure_env + $(PIP) install -e . + +format: ensure_env + $(PIP) install shed + . $(BIN)/activate && shed + +build: ensure_env + $(MAKE) format + $(PIP) install build + $(PYTHON) -m build + +serve: ensure_env + $(BIN)$(APP_NAME) --port=3028 + +run: ensure_env + $(BIN)$(APP_NAME) --port=3028 + diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..07de284 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" \ No newline at end of file diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..2d52a1e --- /dev/null +++ b/setup.cfg @@ -0,0 +1,25 @@ +[metadata] +name = random-name +version = 1.0.0 +description = Micro service for creating random names +author = retoor +author_email = retoor@molodetz.nl +license = MIT +long_description = file: README.md +long_description_content_type = text/markdown + +[options] +packages = find: +package_dir = + = src +python_requires = >=3.7 +install_requires = + app @ git+https://retoor.molodetz.nl/retoor/app + faker + +[options.packages.find] +where = src + +[options.entry_points] +console_scripts = + random-name = random_name.__main__:main diff --git a/src/random_name/__init__.py b/src/random_name/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/random_name/__main__.py b/src/random_name/__main__.py new file mode 100644 index 0000000..db712b4 --- /dev/null +++ b/src/random_name/__main__.py @@ -0,0 +1,34 @@ +import argparse + +from random_name.app import Application + + +def parse_args(): + + parser = argparse.ArgumentParser( + description="Micro service for creating unique names." + ) + + parser.add_argument( + "--host", + required=False, + help="Host to serve on.", + type=str, + default="127.0.0.1", + ) + + parser.add_argument( + "--port", required=False, help="Port to serve on.", type=int, default=3028 + ) + + return parser.parse_args() + + +def main(): + args = parse_args() + app = Application(db_path="sqlite:///random-name.db") + app.run(host=args.host, port=args.port) + + +if __name__ == "__main__": + main() diff --git a/src/random_name/app.py b/src/random_name/app.py new file mode 100644 index 0000000..1d78a52 --- /dev/null +++ b/src/random_name/app.py @@ -0,0 +1,34 @@ +from aiohttp import web +from app.app import Application as BaseApplication, get_timestamp +from faker import Faker + + +class Application(BaseApplication): + + def __init__(self, *args, **kwargs): + + super().__init__(*args, **kwargs) + + self.router.add_get("/", self.index_handler) + self.fake = Faker() + + async def index_handler(self, request): + + name = None + while True: + name = self.fake.name() + if await self.is_name_used(name): + continue + break + await self.register_name_used(name) + return web.json_response(name, content_type="application/json") + + async def is_name_used(self, name): + return ( + len(list(await self.find("used_names", {"name": name}))) and True or False + ) + + async def register_name_used(self, name): + await self.upsert( + "used_names", {"name": name, "created": get_timestamp()}, ["name"] + )