-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Charlie Ernesto
committed
Apr 9, 2024
1 parent
3dd65b8
commit e33d72a
Showing
20 changed files
with
498 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Dockerfile | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
FROM python:2.7 | ||
|
||
ENV PYTHONUNBUFFERED 1 | ||
|
||
RUN mkdir /code | ||
WORKDIR /code | ||
|
||
RUN pip install uwsgi | ||
|
||
# ADD requirements.txt /code/ | ||
# RUN pip install -r requirements.txt | ||
RUN pip install -e git+https://github.com/brianz/servant.git#egg=servant | ||
|
||
COPY . /code/ | ||
RUN pip install -e . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Calculator Service demo | ||
|
||
This is a really simple/dummy service which implements a calculator with Servant. There is a | ||
Docker file which can make it easier to get up and running. To run this: | ||
|
||
``` | ||
$ ./build.sh | ||
$ docker run -it --rm servant/calculator bash | ||
root@557b6ba4e2c9:/code# python test_calculator.py | ||
100.0 / 6.0 = 16.6666666667 | ||
``` | ||
|
||
It's also possible to mount the current working directory as a volume so that you can hack on the | ||
`test_calculator.py` script | ||
|
||
``` | ||
docker run -it --rm -v `pwd`:/code servant/calculator bash | ||
``` | ||
|
||
Now you can edit any files on your host system and that is reflected in the Docker container automatically. | ||
|
||
In this default configuration, service calls are executed as a local Python library call. In order to execute the service calls over HTTP, stand up the uwsgi http server: | ||
|
||
``` | ||
docker run -it --rm -v `pwd`:/code -p 8888:8888 servant/calculator uwsgi --ini uwsgi.ini | ||
``` | ||
|
||
Now, from your host system, uncomment the `client.configure()` line in `test_calculator.py`, update the host and port (which is 8888 in our example above) and launch `test_calculator.py` from your host system. You should get the exact same response and see uwsgi reply to the request. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
docker build -t servant/calculator . |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import servant.fields | ||
|
||
from servant.exceptions import ActionError | ||
from servant.exceptions import ServantException | ||
from servant.service.actions import Action | ||
|
||
|
||
class AddAction(Action): | ||
number1 = servant.fields.IntField( | ||
required=True, | ||
) | ||
number2 = servant.fields.IntField( | ||
required=True, | ||
) | ||
result = servant.fields.IntField( | ||
in_response=True, | ||
) | ||
|
||
def run(self, **kwargs): | ||
self.result = self.number1 + self.number2 | ||
|
||
|
||
class SubtractAction(AddAction): | ||
def run(self, **kwargs): | ||
self.result = self.number1 - self.number2 | ||
|
||
|
||
class BackwardSubtractAction(SubtractAction): | ||
def run(self, **kwargs): | ||
self.result = self.number2 - self.number1 | ||
|
||
|
||
class MultiplyAction(AddAction): | ||
def run(self, **kwargs): | ||
self.result = self.number1 * self.number2 | ||
|
||
|
||
class DivideAction(Action): | ||
numerator = servant.fields.DecimalField( | ||
required=True, | ||
in_response=True, | ||
) | ||
denominator = servant.fields.DecimalField( | ||
required=True, | ||
in_response=True, | ||
) | ||
quotient = servant.fields.DecimalField( | ||
in_response=True, | ||
) | ||
|
||
def run(self, **kwargs): | ||
if self.denominator == 0: | ||
raise ActionError('Cannot divide by zero') | ||
self.quotient = self.numerator / self.denominator | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DUMMY_CONFIG = True | ||
VERSION = 1.0 | ||
CALCULATOR_NAME = 'calculator_service' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from servant.service.base import Service | ||
|
||
import actions | ||
|
||
|
||
class CalculatorService(Service): | ||
|
||
name = 'calculator_service' | ||
version = 1 | ||
|
||
action_map = { | ||
'add': actions.AddAction, | ||
'subtract': actions.SubtractAction, | ||
'divide': actions.DivideAction, | ||
} | ||
|
||
|
||
class CalculatorServiceV2(CalculatorService): | ||
version = 2 | ||
action_map = { | ||
'multiply': actions.MultiplyAction, | ||
'subtract': actions.BackwardSubtractAction, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
git+https://github.com/brianz/servant.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from setuptools import setup, find_packages | ||
|
||
setup( | ||
name = 'calculator_service', | ||
packages=find_packages(), | ||
version = '1.0', | ||
description = 'Example application with Servant', | ||
author='Brian Zambrano', | ||
author_email='[email protected]', | ||
classifiers=[ | ||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 2', | ||
'License :: OSI Approved :: GNU General Public License (GPL)', | ||
'Operating System :: OS Independent', | ||
'Development Status :: 1 - Planning', | ||
'Environment :: Console', | ||
'Intended Audience :: Developers', | ||
'Topic :: System :: Distributed Computing', | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import servant.client | ||
|
||
calc_client = servant.client.Client('calculator_service', version=1) | ||
simple_client = servant.client.Client('simple_service', version=1) | ||
calc_client2 = servant.client.Client('calculator_service', version=2) | ||
|
||
# Uncomment this line and change the connection settings in order to hit an HTTP version of the | ||
# service | ||
# client.configure('http', host='192.168.88.100', port=8888) | ||
|
||
def _handle_error(response): | ||
print response.errors, response.field_errors | ||
|
||
|
||
def do_add(client): | ||
response = client.add(number1=10, number2=15) | ||
|
||
if response.is_error(): | ||
_handle_error(response) | ||
else: | ||
print response.result | ||
|
||
|
||
def do_subtract(client): | ||
response = client.subtract(number1=10, number2=15) | ||
|
||
if response.is_error(): | ||
_handle_error(response) | ||
else: | ||
print response.result | ||
|
||
|
||
def do_divide(client): | ||
response = client.divide(numerator=100, denominator=6) | ||
# Here are some examples of requests which throw errors | ||
#response = client.divide(numerator=100, denominator=0) | ||
#response = client.divide(numerator=100, denominator='abc') | ||
|
||
if response.is_error(): | ||
_handle_error(response) | ||
else: | ||
print '%s / %s = %s' % (response.numerator, response.denominator, response.quotient) | ||
|
||
def do_multiply(client): | ||
response = client.multiply(number1=12, number2=12) | ||
|
||
if response.is_error(): | ||
print response.errors | ||
else: | ||
print response.result | ||
|
||
|
||
calc_client.configure('http', host='192.168.88.100', port=8888) | ||
calc_client2.configure('http', host='192.168.88.100', port=8888) | ||
|
||
do_divide(calc_client) | ||
|
||
#response = simple_client.get_theater_listing(theater_id=123) | ||
#if not response.is_error(): | ||
# for movie in response.movies: | ||
# print movie | ||
#else: | ||
# print response.text | ||
# | ||
|
||
do_add(calc_client2) | ||
#calc_client2.service_name = 'fooely' | ||
#do_multiply(calc_client2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[uwsgi] | ||
http=0.0.0.0:8888 | ||
wsgi-file=wsgi_handler.py | ||
processes=1 | ||
threads=1 | ||
py-auto-reload=3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from calculator_service.service import CalculatorService | ||
|
||
service = CalculatorService() | ||
application = service.get_wsgi_application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import sys | ||
import time | ||
|
||
import servant.client | ||
|
||
from pprint import pprint as pp | ||
|
||
client = servant.client.Client('simple_service', version=1) | ||
|
||
try: | ||
if sys.argv[1].lower() == 'http': | ||
client.configure('http', host='localhost', port=8888) | ||
except Exception: | ||
pass | ||
|
||
if not client.is_configured(): | ||
client.configure('local') | ||
|
||
def line(): | ||
print '-' * 50 | ||
|
||
#resp = client.ping() | ||
#line() | ||
#pp(resp) | ||
|
||
#resp = client.echo(name='brianz') | ||
#line() | ||
#pp(resp) | ||
|
||
#resp = client.echo(name='brianz', age=41, is_awesome=True) | ||
#line() | ||
#pp(resp) | ||
|
||
#resp = client.get_weather(city='denver', state='CO') | ||
#line() | ||
#pp(resp) | ||
# | ||
n = time.time() - (3600 * 4) | ||
|
||
#resp = client.get_historical_weather( | ||
# city_id=2885679, | ||
# start_time=n, | ||
# make_real_request=False) | ||
#line() | ||
#pp(resp) | ||
|
||
resp = client.get_theater_listing(theater_id=1234) | ||
line() | ||
pp(resp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from setuptools import setup, find_packages | ||
|
||
setup( | ||
name = 'simple_service', | ||
packages=find_packages(), | ||
version = '1.0', | ||
description = 'Example application with Servant', | ||
author='Brian Zambrano', | ||
author_email='[email protected]', | ||
classifiers=[ | ||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 2', | ||
'License :: OSI Approved :: GNU General Public License (GPL)', | ||
'Operating System :: OS Independent', | ||
'Development Status :: 1 - Planning', | ||
'Environment :: Console', | ||
'Intended Audience :: Developers', | ||
'Topic :: System :: Distributed Computing', | ||
] | ||
) |
Empty file.
Oops, something went wrong.