Skip to content

Commit a158404

Browse files
committed
Merge remote-tracking branch 'origin/master' into task/updated-documentation
2 parents 262700e + 824cb5c commit a158404

28 files changed

+514
-272
lines changed

AUTHORS

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Alberto Vara <a.vara.1986@gmail.com>
2+
Àlex Pérez <alexperezpujol@disroot.org>
23
Hugo Camino <hugo.camino.villacorta@gmail.com>
3-
José Manuel <jmrivas86@gmail.com>
44
Javier Luna molina <javierlunamolina@gmail.com>
5-
pilamb <perikopalotes@gmail.com>
5+
José Manuel <jmrivas86@gmail.com>
6+
Mike Rubin <miguelgr1988@gmail.com>

Pipfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ pyyaml = ">=5.1.2"
1010
anyconfig = ">=0.9.8"
1111
swagger-ui-bundle = ">=0.0.2"
1212
connexion = {extras = ["swagger-ui"],version = "==2.4.0"}
13-
jaeger-client = "==4.2.0"
13+
jaeger-client = "==4.3.0"
1414
flask-opentracing = "*"
1515
opentracing = ">=2.1"
1616
opentracing-instrumentation = "==3.2.1"
1717
prometheus_client = ">=0.7.1"
18+
1819
[dev-packages]
1920
requests-mock = "*"
2021
coverage = "==4.5.4"

Pipfile.lock

Lines changed: 161 additions & 140 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,37 @@
66
[![Requirements Status](https://requires.io/github/python-microservices/pyms/requirements.svg?branch=master)](https://requires.io/github/python-microservices/pyms/requirements/?branch=master)
77
[![Total alerts](https://img.shields.io/lgtm/alerts/g/python-microservices/pyms.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/python-microservices/pyms/alerts/)
88
[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/python-microservices/pyms.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/python-microservices/pyms/context:python)
9+
[![Gitter](https://img.shields.io/gitter/room/DAVFoundation/DAV-Contributors.svg)](https://gitter.im/python-microservices/pyms)
910

10-
PyMS, Python MicroService, is a collections of libraries, best practices and recommended ways to build
11-
microservices with Python.
11+
12+
PyMS, Python MicroService, is a collections of libraries, best practices and recommended ways to build microservices with Python.
1213

1314
## Documentation
1415

15-
To know how use, install or build a project see the docs: https://py-ms.readthedocs.io/en/latest/
16+
To know how use, install or build a project see the [docs](https://py-ms.readthedocs.io/en/latest/).
1617

1718
## Motivation
1819

1920
When we started to create microservice with no idea, we were looking for tutorials, guides, best practices, but we found
2021
nothing to create professional projects. Most articles say:
22+
2123
- "Install flask"
2224
- "Create routes"
2325
- (Sometimes) "Create a swagger specs"
2426
- "TA-DA! you have a microservice"
2527

26-
But... what happens with our configuration out of code like Kubernetes configmap? what happens with transactionality?
28+
But... what happens with our configuration out of code like Kubernetes configmap? what happens with transactionality?
2729
If we have many microservices, what happens with traces?.
2830

2931
There are many problems around Python and microservices and we can`t find anyone to give a solution.
3032

31-
We start creating these projects to try to solve all the problems we have found in our professional lives about
33+
We start creating these projects to try to solve all the problems we have found in our professional lives about
3234
microservices architecture.
3335

34-
Nowadays, is not perfect and we have a looong roadmap, but we hope this library could help other felas and friends ;)
36+
Nowadays, is not perfect and we have a looong roadmap, but we hope this library could help other felas and friends ;)
3537

38+
## Installation
3639

37-
## Installation
3840
```bash
3941
pip install py-ms
4042
```
@@ -43,7 +45,8 @@ pip install py-ms
4345

4446
You need to create 2 files: main.py and config.yml:
4547

46-
main.py
48+
- **main.py**
49+
4750
```python
4851
from flask import jsonify
4952

@@ -62,9 +65,10 @@ if __name__ == '__main__':
6265
app.run()
6366
```
6467

65-
config.yml
68+
- **config.yml**
69+
6670
```yaml
67-
pyms:
71+
pyms:
6872
services: # 1.2
6973
requests:
7074
data: {}
@@ -77,19 +81,22 @@ pyms:
7781
7882
### So what did that code do?
7983
80-
1. Create a instance of PyMS Microservice class (#1.1). This initialization inject the configuration defined in the
81-
1.3 block and could be accessed through current_app.config like typical
82-
[Flask config](https://flask.palletsprojects.com/en/1.1.x/config/).
84+
1. Create a instance of PyMS Microservice class (#1.1). This initialization inject the configuration defined in the
85+
1.3 block and could be accessed through current_app.config like typical
86+
[Flask config](https://flask.palletsprojects.com/en/1.1.x/config/).
8387
Then, initialize the service defined in the 1.2 block. See [Services](services.md) for more details.
84-
2. Initialize [Flask](https://flask.palletsprojects.com/en/1.1.x/) instance, [Connexion](https://github.com/zalando/connexion)
85-
if it was defined in the pyms configuration block, create a tracer, add health-check blueprint, initialize libs and set
86-
the PyMS Microservice in
87-
`ms` attribute and you can access to it with `current_app.ms`. This steps has their each functions and you can easy
88+
89+
2. Initialize [Flask](https://flask.palletsprojects.com/en/1.1.x/) instance, [Connexion](https://github.com/zalando/connexion)
90+
if it was defined in the pyms configuration block, create a tracer, add health-check blueprint, initialize libs and set
91+
the PyMS Microservice in `ms` attribute and you can access to it with `current_app.ms`.
92+
This steps has their each functions and you can easy
8893
override it.
94+
8995
3. `create_app` return the flask instance and you can interact with it as a typical flask app
9096

91-
See Documentation https://py-ms.readthedocs.io/en/latest/ to learn more.
97+
See [Documentation](https://py-ms.readthedocs.io/en/latest/) to learn more.
9298

9399
## How To Contrib
94-
We appreciate opening issues and pull requests to make PyMS even more stable & useful! See [This doc](COONTRIBUTING.md)
95-
for more details
100+
101+
We appreciate opening issues and pull requests to make PyMS even more stable & useful! See [This doc](CONTRIBUTING.md)
102+
for more details.

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ microservices with Python which handles cross-cutting concerns:
77
- Externalized configuration
88
- Logging
99
- Health checks
10-
- Metrics (TODO)
10+
- Metrics
1111
- Distributed tracing
1212

1313
PyMS is powered by [Flask](https://flask.palletsprojects.com/en/1.1.x/), [Connexion](https://github.com/zalando/connexion) and [Opentracing](https://opentracing.io/).

docs/ms_class.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ Each keyword in our configuration block, can be accessed in our Microservice obj
3737
```yaml
3838
# Config.yml
3939
pyms:
40-
config:
41-
app_name: "Python Microservice"
42-
foo: "var"
43-
multiplevars:
44-
config1: "test1"
45-
config2: "test2"
40+
config:
41+
app_name: "Python Microservice"
42+
foo: "var"
43+
multiplevars:
44+
config1: "test1"
45+
config2: "test2"
4646
4747
```
4848
```python
4949
#app.py
5050
from pyms.flask.app import Microservice
5151
52-
ms = Microservice(service="example-config", path=__file__)
52+
ms = Microservice(path=__file__)
5353
print(ms.config.APP_NAME)
5454
# >> "Python Microservice"
5555
print(ms.config.app_name)

docs/quickstart.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ if __name__ == '__main__':
2626
config.yml
2727
```yaml
2828
pyms:
29-
services: # 1.2
30-
requests:
31-
data: {}
32-
config: # 1.3
33-
DEBUG: true
34-
APP_NAME: business-glossary
35-
APPLICATION_ROOT : ""
36-
SECRET_KEY: "gjr39dkjn344_!67#"
29+
services: # 1.2
30+
requests:
31+
data: {}
32+
config: # 1.3
33+
DEBUG: true
34+
APP_NAME: business-glossary
35+
APPLICATION_ROOT : ""
36+
SECRET_KEY: "gjr39dkjn344_!67#"
3737
```
3838
3939
## So what did that code do?

pyms/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
__email__ = "a.vara.1986@gmail.com"
44

5-
__version__ = "2.0.0rc1"
5+
__version__ = "2.1.0"

pyms/config/conf.py

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from pyms.config.confile import ConfFile
2-
from pyms.exceptions import ServiceDoesNotExistException
3-
4-
5-
__service_configs = {}
2+
from pyms.constants import PYMS_CONFIG_WHITELIST_KEYWORDS
3+
from pyms.exceptions import ServiceDoesNotExistException, ConfigErrorException, AttrDoesNotExistException
64

75

86
def get_conf(*args, **kwargs):
@@ -11,34 +9,98 @@ def get_conf(*args, **kwargs):
119
parent and this name, in example of the next yaml, tracer will be `pyms.tracer`. If we have got his config file:
1210
```
1311
pyms:
14-
metrics: true
15-
requests:
16-
data: data
17-
swagger:
18-
path: ""
19-
file: "swagger.yaml"
20-
tracer:
21-
client: "jaeger"
22-
host: "localhost"
23-
component_name: "Python Microservice"
24-
my-ms:
25-
DEBUG: true
26-
TESTING: true
12+
services:
13+
metrics: true
14+
requests:
15+
data: data
16+
swagger:
17+
path: ""
18+
file: "swagger.yaml"
19+
tracer:
20+
client: "jaeger"
21+
host: "localhost"
22+
component_name: "Python Microservice"
23+
config:
24+
DEBUG: true
25+
TESTING: true
2726
```
28-
* `pyms` block is the default key to load in the pyms.flask.app.create_app.Microservice class.
27+
* `pyms.services`: block is the default key to load in the pyms.flask.app.create_app.Microservice class.
2928
* `metrics`: is set as the service `pyms.metrics`
3029
* `swagger`: is set as the service `pyms.swagger`
3130
* `tracer`: is set as the service `pyms.tracer`
32-
* `my-ms` block is defined by the env var `CONFIGMAP_SERVICE`. By default is `ms`. This block is the default flask
33-
block config
31+
* `pyms.config`: This block is the default flask block config
3432
:param args:
3533
:param kwargs:
34+
3635
:return:
3736
"""
3837
service = kwargs.pop('service', None)
39-
memoize = kwargs.pop('memoize', True)
4038
if not service:
4139
raise ServiceDoesNotExistException("Service not defined")
42-
if not memoize or service not in __service_configs:
43-
__service_configs[service] = ConfFile(*args, **kwargs)
44-
return getattr(__service_configs[service], service)
40+
config = ConfFile(*args, **kwargs)
41+
return getattr(config, service)
42+
43+
44+
def validate_conf(*args, **kwargs):
45+
config = ConfFile(*args, **kwargs)
46+
is_config_ok = True
47+
try:
48+
config.pyms
49+
except AttrDoesNotExistException:
50+
is_config_ok = False
51+
if not is_config_ok:
52+
raise ConfigErrorException("""Config file must start with `pyms` keyword, for example:
53+
pyms:
54+
services:
55+
metrics: true
56+
requests:
57+
data: data
58+
swagger:
59+
path: ""
60+
file: "swagger.yaml"
61+
tracer:
62+
client: "jaeger"
63+
host: "localhost"
64+
component_name: "Python Microservice"
65+
config:
66+
DEBUG: true
67+
TESTING: true""")
68+
try:
69+
config.pyms.config
70+
except AttrDoesNotExistException:
71+
is_config_ok = False
72+
if not is_config_ok:
73+
raise ConfigErrorException("""`pyms` block must contain a `config` keyword in your Config file, for example:
74+
pyms:
75+
services:
76+
metrics: true
77+
requests:
78+
data: data
79+
swagger:
80+
path: ""
81+
file: "swagger.yaml"
82+
tracer:
83+
client: "jaeger"
84+
host: "localhost"
85+
component_name: "Python Microservice"
86+
config:
87+
DEBUG: true
88+
TESTING: true""")
89+
wrong_keywords = [i for i in config.pyms if i not in PYMS_CONFIG_WHITELIST_KEYWORDS]
90+
if len(wrong_keywords) > 0:
91+
raise ConfigErrorException("""{} isn`t a valid keyword for pyms block, for example:
92+
pyms:
93+
services:
94+
metrics: true
95+
requests:
96+
data: data
97+
swagger:
98+
path: ""
99+
file: "swagger.yaml"
100+
tracer:
101+
client: "jaeger"
102+
host: "localhost"
103+
component_name: "Python Microservice"
104+
config:
105+
DEBUG: true
106+
TESTING: true""".format(wrong_keywords))

0 commit comments

Comments
 (0)