mirror of
https://github.com/Bryanthelol/namekoplus
synced 2025-09-14 00:26:03 +08:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
0777788609 | |||
dd23c91192 | |||
cb5762720f | |||
920e204756 | |||
9ac4a39c0d | |||
37bdf56862 |
@@ -1,3 +1,5 @@
|
|||||||
include *.py *.md
|
include *.py *.md *.mako
|
||||||
recursive-include namekoplus/chassis *.py
|
recursive-include namekoplus/chassis *.py *.mako
|
||||||
recursive-include namekoplus/templates *.py *.yml
|
recursive-include namekoplus/templates *.py *.yml *.mako
|
||||||
|
recursive-include namekoplus/chassis-agent *.py *.yml *.mako
|
||||||
|
recursive-include namekoplus/tests *.py *.mako
|
18
README.md
18
README.md
@@ -2,10 +2,6 @@
|
|||||||
|
|
||||||
A lightweight Python distributed microservice solution
|
A lightweight Python distributed microservice solution
|
||||||
|
|
||||||
## Document
|
|
||||||
|
|
||||||
[中文文档](https://doc.bearcatlog.com/)
|
|
||||||
|
|
||||||
## Command Line Tool Usage
|
## Command Line Tool Usage
|
||||||
|
|
||||||
### Checkout Command
|
### Checkout Command
|
||||||
@@ -14,8 +10,22 @@ A lightweight Python distributed microservice solution
|
|||||||
namekoplus --help
|
namekoplus --help
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Start a middleware that nameko depends on
|
||||||
|
|
||||||
|
```shell
|
||||||
|
namekoplus start -m rabbitmq
|
||||||
|
```
|
||||||
|
|
||||||
### Initialize a nameko service from templates
|
### Initialize a nameko service from templates
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
namekoplus init --directory <dir_name> --type <template_type>
|
namekoplus init --directory <dir_name> --type <template_type>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Detailed Usage
|
||||||
|
|
||||||
|
See Documents:
|
||||||
|
|
||||||
|
- [中文](https://doc.bearcatlog.com/)
|
||||||
|
- [English](https://legendary-sopapillas-e2626d.netlify.app/)
|
0
namekoplus/chassis-agent/__init__.py
Normal file
0
namekoplus/chassis-agent/__init__.py
Normal file
@@ -7,6 +7,9 @@ from python_on_whales import DockerException, ClientNotFoundError, DockerClient,
|
|||||||
|
|
||||||
|
|
||||||
def check_docker():
|
def check_docker():
|
||||||
|
"""
|
||||||
|
Check if docker and docker compose are installed and running.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
docker_testing.ps()
|
docker_testing.ps()
|
||||||
except ClientNotFoundError:
|
except ClientNotFoundError:
|
||||||
@@ -23,6 +26,9 @@ def check_docker():
|
|||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def status(status_msg: str, newline: bool = False, quiet: bool = False):
|
def status(status_msg: str, newline: bool = False, quiet: bool = False):
|
||||||
|
"""
|
||||||
|
Show status message and yield.
|
||||||
|
"""
|
||||||
msg_suffix = ' ...' if not newline else ' ...\n'
|
msg_suffix = ' ...' if not newline else ' ...\n'
|
||||||
click.echo(status_msg + msg_suffix)
|
click.echo(status_msg + msg_suffix)
|
||||||
try:
|
try:
|
||||||
@@ -36,24 +42,25 @@ def status(status_msg: str, newline: bool = False, quiet: bool = False):
|
|||||||
click.echo(' Done\n')
|
click.echo(' Done\n')
|
||||||
|
|
||||||
|
|
||||||
def get_template_directory() -> str:
|
def get_directory(dir_name: str) -> str:
|
||||||
"""
|
"""
|
||||||
Return the directory where nameko_plus setup templates are found.
|
Return the directory path of the given nameko-plus directory name.
|
||||||
"""
|
"""
|
||||||
import namekoplus
|
import namekoplus
|
||||||
|
|
||||||
package_dir = os.path.abspath(os.path.dirname(namekoplus.__file__))
|
package_dir = os.path.abspath(os.path.dirname(namekoplus.__file__))
|
||||||
return os.path.join(package_dir, 'templates')
|
return os.path.join(package_dir, dir_name)
|
||||||
|
|
||||||
|
|
||||||
def get_agent_directory() -> str:
|
def copy_files(src_dir, dest_dir):
|
||||||
"""
|
for file_ in os.listdir(src_dir):
|
||||||
Return the directory where nameko_plus setup agent are found.
|
if file_ == '__pycache__':
|
||||||
"""
|
continue
|
||||||
import namekoplus
|
|
||||||
|
|
||||||
package_dir = os.path.abspath(os.path.dirname(namekoplus.__file__))
|
src_file_path = os.path.join(src_dir, file_)
|
||||||
return os.path.join(package_dir, 'chassis-agent')
|
output_file = os.path.join(dest_dir, file_)
|
||||||
|
with status(f'Generating {os.path.abspath(output_file)}'):
|
||||||
|
shutil.copy(src_file_path, output_file)
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
@click.group()
|
||||||
@@ -68,7 +75,7 @@ def cli():
|
|||||||
@click.option('-t', '--type', '_type',
|
@click.option('-t', '--type', '_type',
|
||||||
default='all',
|
default='all',
|
||||||
show_default=True,
|
show_default=True,
|
||||||
type=click.Choice(['all', 'rpc', 'event', 'http', 'timer'], case_sensitive=False),
|
type=click.Choice(['all', 'rpc', 'event', 'http', 'timer', 'demo'], case_sensitive=False),
|
||||||
help='The template type of nameko service')
|
help='The template type of nameko service')
|
||||||
def init(directory, _type):
|
def init(directory, _type):
|
||||||
"""
|
"""
|
||||||
@@ -78,24 +85,16 @@ def init(directory, _type):
|
|||||||
click.echo('Directory {} already exists and is not empty'.format(directory), err=True)
|
click.echo('Directory {} already exists and is not empty'.format(directory), err=True)
|
||||||
return
|
return
|
||||||
|
|
||||||
template_dir = os.path.join(get_template_directory(), _type)
|
template_dir = os.path.join(get_directory('templates'), _type)
|
||||||
if not os.access(template_dir, os.F_OK):
|
if not os.access(template_dir, os.F_OK):
|
||||||
click.echo('No such template type {}'.format(_type), err=True)
|
click.echo('No such template type {}'.format(_type), err=True)
|
||||||
return
|
return
|
||||||
|
|
||||||
# 创建目录
|
|
||||||
if not os.access(directory, os.F_OK):
|
if not os.access(directory, os.F_OK):
|
||||||
with status(f'Creating directory {os.path.abspath(directory)!r}'):
|
with status(f'Creating directory {os.path.abspath(directory)!r}'):
|
||||||
os.makedirs(directory)
|
os.makedirs(directory)
|
||||||
|
|
||||||
# 把 templates 放入新建的目录
|
copy_files(template_dir, directory)
|
||||||
for file_ in os.listdir(template_dir):
|
|
||||||
if file_ == '__pycache__':
|
|
||||||
continue
|
|
||||||
src_file_path = os.path.join(template_dir, file_)
|
|
||||||
output_file = os.path.join(directory, file_)
|
|
||||||
with status(f'Generating {os.path.abspath(output_file)}'):
|
|
||||||
shutil.copy(src_file_path, output_file)
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@@ -119,7 +118,7 @@ def start(middleware, user, password):
|
|||||||
os.environ['RABBITMQ_DEFAULT_USER'] = user
|
os.environ['RABBITMQ_DEFAULT_USER'] = user
|
||||||
os.environ['RABBITMQ_DEFAULT_PASS'] = password
|
os.environ['RABBITMQ_DEFAULT_PASS'] = password
|
||||||
|
|
||||||
docker_compose_file_dir = os.path.join(get_agent_directory(), middleware)
|
docker_compose_file_dir = os.path.join(get_directory('chassis-agent'), middleware)
|
||||||
for file_ in os.listdir(docker_compose_file_dir):
|
for file_ in os.listdir(docker_compose_file_dir):
|
||||||
compose_file_path = os.path.join(docker_compose_file_dir, file_)
|
compose_file_path = os.path.join(docker_compose_file_dir, file_)
|
||||||
with status(f'Starting {middleware}'):
|
with status(f'Starting {middleware}'):
|
||||||
@@ -138,13 +137,38 @@ def stop(middleware):
|
|||||||
"""
|
"""
|
||||||
check_docker()
|
check_docker()
|
||||||
|
|
||||||
docker_compose_file_dir = os.path.join(get_agent_directory(), middleware)
|
docker_compose_file_dir = os.path.join(get_directory('chassis-agent'), middleware)
|
||||||
for file_ in os.listdir(docker_compose_file_dir):
|
for file_ in os.listdir(docker_compose_file_dir):
|
||||||
compose_file_path = os.path.join(docker_compose_file_dir, file_)
|
compose_file_path = os.path.join(docker_compose_file_dir, file_)
|
||||||
with status(f'Stoping {middleware}'):
|
with status(f'Stopping {middleware}'):
|
||||||
docker = DockerClient(compose_files=[compose_file_path])
|
docker = DockerClient(compose_files=[compose_file_path])
|
||||||
docker.compose.down()
|
docker.compose.down()
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@click.option('-e', '--existed_dir', 'directory',
|
||||||
|
required=True,
|
||||||
|
help='The existed directory name of the nameko service')
|
||||||
|
@click.option('-t', '--type', '_type',
|
||||||
|
default='unit',
|
||||||
|
show_default=True,
|
||||||
|
type=click.Choice(['unit'], case_sensitive=False),
|
||||||
|
help='The test type of the nameko service')
|
||||||
|
def test_gen(directory, _type):
|
||||||
|
"""
|
||||||
|
Generate test files for nameko services.
|
||||||
|
"""
|
||||||
|
if not os.access(directory, os.F_OK) or not os.listdir(directory):
|
||||||
|
click.echo('Directory {} dose not exist or is empty'.format(directory), err=True)
|
||||||
|
return
|
||||||
|
|
||||||
|
tests_dir = os.path.join(get_directory('tests'), _type)
|
||||||
|
if not os.access(tests_dir, os.F_OK):
|
||||||
|
click.echo('No such test type {}'.format(_type), err=True)
|
||||||
|
return
|
||||||
|
|
||||||
|
copy_files(tests_dir, directory)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
cli()
|
cli()
|
@@ -1,4 +1,5 @@
|
|||||||
AMQP_URI: pyamqp://${RABBIT_USER:guest}:${RABBIT_PASSWORD:guest}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
||||||
|
WEB_SERVER_ADDRESS: '0.0.0.0:8000'
|
||||||
RPC_EXCHANGE: 'nameko-rpc'
|
RPC_EXCHANGE: 'nameko-rpc'
|
||||||
|
|
||||||
max_workers: 10
|
max_workers: 10
|
||||||
|
0
namekoplus/templates/demo/__init__.py
Normal file
0
namekoplus/templates/demo/__init__.py
Normal file
5
namekoplus/templates/demo/config.yml
Normal file
5
namekoplus/templates/demo/config.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
||||||
|
RPC_EXCHANGE: 'nameko-rpc'
|
||||||
|
|
||||||
|
max_workers: 10
|
||||||
|
parent_calls_tracked: 20
|
20
namekoplus/templates/demo/rpc_demo.py
Normal file
20
namekoplus/templates/demo/rpc_demo.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
from nameko.rpc import rpc, ServiceRpc
|
||||||
|
|
||||||
|
|
||||||
|
class RpcResponderDemoService:
|
||||||
|
name = "rpc_responder_demo_service"
|
||||||
|
|
||||||
|
@rpc
|
||||||
|
def hello(self, name):
|
||||||
|
return "Hello, {}!".format(name)
|
||||||
|
|
||||||
|
|
||||||
|
class RpcCallerDemoService:
|
||||||
|
name = "rpc_caller_demo_service"
|
||||||
|
|
||||||
|
remote = ServiceRpc("rpc_responder_demo_service")
|
||||||
|
|
||||||
|
@rpc
|
||||||
|
def remote_hello(self, value="John Doe"):
|
||||||
|
res = u"{}".format(value)
|
||||||
|
return self.remote.hello(res)
|
@@ -1,4 +1,4 @@
|
|||||||
AMQP_URI: pyamqp://${RABBIT_USER:guest}:${RABBIT_PASSWORD:guest}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
||||||
RPC_EXCHANGE: 'nameko-rpc'
|
RPC_EXCHANGE: 'nameko-rpc'
|
||||||
|
|
||||||
max_workers: 10
|
max_workers: 10
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
AMQP_URI: pyamqp://${RABBIT_USER:guest}:${RABBIT_PASSWORD:guest}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
||||||
|
WEB_SERVER_ADDRESS: '0.0.0.0:8000'
|
||||||
RPC_EXCHANGE: 'nameko-rpc'
|
RPC_EXCHANGE: 'nameko-rpc'
|
||||||
|
|
||||||
max_workers: 10
|
max_workers: 10
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
AMQP_URI: pyamqp://${RABBIT_USER:guest}:${RABBIT_PASSWORD:guest}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
||||||
RPC_EXCHANGE: 'nameko-rpc'
|
RPC_EXCHANGE: 'nameko-rpc'
|
||||||
|
|
||||||
max_workers: 10
|
max_workers: 10
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
AMQP_URI: pyamqp://${RABBIT_USER:guest}:${RABBIT_PASSWORD:guest}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
||||||
RPC_EXCHANGE: 'nameko-rpc'
|
RPC_EXCHANGE: 'nameko-rpc'
|
||||||
|
|
||||||
max_workers: 10
|
max_workers: 10
|
||||||
|
0
namekoplus/tests/__init__.py
Normal file
0
namekoplus/tests/__init__.py
Normal file
0
namekoplus/tests/unit/__init__.py
Normal file
0
namekoplus/tests/unit/__init__.py
Normal file
29
namekoplus/tests/unit/test_service.py
Normal file
29
namekoplus/tests/unit/test_service.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
"""
|
||||||
|
Service unit testing best practice.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from nameko.testing.services import worker_factory
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'value, expected',
|
||||||
|
[
|
||||||
|
('John Doe', 'Hello, John Doe!'),
|
||||||
|
('', 'Hello, !'),
|
||||||
|
('Bryant', 'Hello, Bryant!'),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_example_service(value, expected):
|
||||||
|
"""
|
||||||
|
Test example service.
|
||||||
|
"""
|
||||||
|
# create worker with mock dependencies
|
||||||
|
service = worker_factory(ServiceName) # TODO replace ServiceName with the name of the service and import it
|
||||||
|
|
||||||
|
# add side effects to the mock rpc dependency on the "remote" service
|
||||||
|
service.remote.hello.side_effect = lambda name: "Hello, {}!".format(name)
|
||||||
|
|
||||||
|
# test remote_hello business logic
|
||||||
|
assert service.remote_hello(value) == expected
|
||||||
|
service.remote.hello.assert_called_once_with(value)
|
6
setup.py
6
setup.py
@@ -9,7 +9,7 @@ with open(path.join(here, 'README.md'), encoding='utf-8') as f:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='namekoplus',
|
name='namekoplus',
|
||||||
version='0.2.0',
|
version='0.3.1',
|
||||||
description='A lightweight Python distributed microservice solution',
|
description='A lightweight Python distributed microservice solution',
|
||||||
long_description=long_description,
|
long_description=long_description,
|
||||||
long_description_content_type='text/markdown',
|
long_description_content_type='text/markdown',
|
||||||
@@ -33,7 +33,7 @@ setup(
|
|||||||
'Topic :: Software Development :: Libraries :: Python Modules',
|
'Topic :: Software Development :: Libraries :: Python Modules',
|
||||||
],
|
],
|
||||||
platforms='any',
|
platforms='any',
|
||||||
python_requires='>=3',
|
python_requires='>=3.8, <4',
|
||||||
|
|
||||||
keywords='lightweight python distributed microservice solution',
|
keywords='lightweight python distributed microservice solution',
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ setup(
|
|||||||
'sqlalchemy==2.0.15',
|
'sqlalchemy==2.0.15',
|
||||||
'sqlacodegen==2.3.0',
|
'sqlacodegen==2.3.0',
|
||||||
'alembic==1.11.1'],
|
'alembic==1.11.1'],
|
||||||
'ssl': ['cryptography'],
|
'security': ['cryptography'],
|
||||||
'dev': ['mako==1.2.4',
|
'dev': ['mako==1.2.4',
|
||||||
'environs==9.5.0']
|
'environs==9.5.0']
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user