Initial commit.
This commit is contained in:
parent
0e24ea3f9f
commit
0f57fff0aa
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,3 +1,7 @@
|
||||
.vscode
|
||||
.history
|
||||
*.db*
|
||||
|
||||
# ---> Python
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
|
31
Makefile
Normal file
31
Makefile
Normal file
@ -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
|
||||
|
3
pyproject.toml
Normal file
3
pyproject.toml
Normal file
@ -0,0 +1,3 @@
|
||||
[build-system]
|
||||
requires = ["setuptools", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
25
setup.cfg
Normal file
25
setup.cfg
Normal file
@ -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
|
0
src/random_name/__init__.py
Normal file
0
src/random_name/__init__.py
Normal file
34
src/random_name/__main__.py
Normal file
34
src/random_name/__main__.py
Normal file
@ -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()
|
34
src/random_name/app.py
Normal file
34
src/random_name/app.py
Normal file
@ -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"]
|
||||
)
|
Loading…
Reference in New Issue
Block a user