Skip to content

Commit 15b47eb

Browse files
authored
Merge pull request #33 from python-microservices/feature/docs
Documentation
2 parents 5736106 + f6b56d5 commit 15b47eb

File tree

17 files changed

+354
-2
lines changed

17 files changed

+354
-2
lines changed

AUTHORS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Alberto Vara <[email protected]>
22
Hugo Camino <[email protected]>
33
José Manuel <[email protected]>
4+
Javier Luna molina <[email protected]>
5+

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
[![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/)
1010
[![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)
1111

12+
PyMS, Python MicroService, is a collections of libraries, best practices and recommended ways to build
13+
microservices with Python.
14+
15+
To know how use, install or build a porject see the docs (TODO: add url to readthedocs)
1216

1317
## Installation
1418
```bash
@@ -81,3 +85,20 @@ pipenv install --deploy
8185
```bash
8286
pipenv shell
8387
```
88+
89+
## Documentation
90+
91+
This project use MkDocs
92+
93+
* `mkdocs new [dir-name]` - Create a new project.
94+
* `mkdocs serve` - Start the live-reloading docs server.
95+
* `mkdocs build` - Build the documentation site.
96+
* `mkdocs help` - Print this help message.
97+
98+
### Project layout
99+
100+
mkdocs.yml # The configuration file.
101+
docs/
102+
index.md # The documentation homepage.
103+
... # Other markdown pages, images and other files.
104+

docs/configuration.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Configuration
2+
3+
Each microservice needs a config file in yaml or json format to work with it. This configuration contains
4+
the Flask settings of your project and the [Services](services.md).
5+
6+
a simple configuration file could be a config.yaml:
7+
8+
```yaml
9+
pyms:
10+
requests: true
11+
swagger:
12+
path: ""
13+
file: "swagger.yaml"
14+
my-ms:
15+
DEBUG: true
16+
TESTING: false
17+
APP_NAME: "Python Microservice"
18+
APPLICATION_ROOT: ""
19+
```
20+
21+
or in a config.json:
22+
23+
```json
24+
{
25+
"pyms":{
26+
"requests": true,
27+
"swagger": {
28+
"path": "",
29+
"file": "swagger.yaml"
30+
}
31+
},
32+
"my-ms": {
33+
"DEBUG": true,
34+
"TESTIN": false,
35+
"APP_NAME": "Python Microservice",
36+
"APPLICATION_ROOT": ""
37+
}
38+
}
39+
```
40+
41+
This file could contains this keywords:
42+
43+
44+
## pyms block
45+
46+
```pyms```: all subsets inside this keyword are the settings of this library. Each keyword will be a service of our
47+
[Microservice class](ms_class.md). For example, we declare our microservice class as:
48+
49+
```python
50+
from pyms.flask.app import Microservice
51+
ms = Microservice(service="my-ms", path=__file__)
52+
```
53+
and a `config.yaml` file:
54+
55+
```yaml
56+
pyms:
57+
requests: true
58+
```
59+
60+
our object `ms` has an attribute `requests` that is a instance of our service [requests](services.md).
61+
62+
## Our microservice block
63+
This part contains all keywords of a [Flask Configuration Handling](http://flask.pocoo.org/docs/1.0/config/) and our
64+
constants of the enviroments (local configuration, staging configuration...). Keep in mind that a Flask configuration needs
65+
the keywords to be declared as uppercase.
66+
67+
The name of this block is defined when you create the object of [Microservice class](ms_class.md):
68+
69+
### Example 1
70+
```python
71+
from pyms.flask.app import Microservice
72+
ms = Microservice(service="my-personal-microservice", path=__file__)
73+
```
74+
and a `config.yaml` file:
75+
76+
```yaml
77+
my-personal-microservice:
78+
DEBUG: true
79+
TESTING: false
80+
```
81+
82+
### Example 2
83+
```python
84+
from pyms.flask.app import Microservice
85+
ms = Microservice(service="ms1-api", path=__file__)
86+
```
87+
and a `config.yaml` file:
88+
89+
```yaml
90+
ms1-api:
91+
DEBUG: true
92+
TESTING: false
93+
```

docs/examples.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Examples
2+
3+
```bash
4+
pip install py-ms
5+
```
6+
7+
config.yml:
8+
9+
```yaml
10+
my-minimal-microservice:
11+
APP_NAME: "Python Microservice"
12+
```
13+
14+
main.py
15+
16+
```python
17+
from flask import jsonify
18+
19+
from pyms.flask.app import Microservice
20+
21+
ms = Microservice(service="my-minimal-microservice", path=__file__)
22+
app = ms.create_app()
23+
24+
25+
@app.route("/")
26+
def example():
27+
return jsonify({"main": "hello world"})
28+
29+
30+
if __name__ == '__main__':
31+
app.run()
32+
```
33+
34+
```bash
35+
python main.py
36+
```
37+
38+
Open in your browser http://localhost:5000/
39+
40+
See [this Github page](https://github.com/python-microservices/pyms/tree/master/examples) to see a examples

docs/index.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Welcome to PyMS
2+
3+
PyMS, Python MicroService, is a collection of libraries, best practices and recommended ways to build
4+
microservices with Python.
5+
6+
## Index:
7+
* [PyMS structure](structure.md)
8+
* [Configuration](configuration.md)
9+
* [Services](services.md)
10+
* [Microservice class](ms_class.md)
11+
* [Quick start and examples](examples.md)
12+
* [Structure of a microservice project](structure_project.md)
13+
* Swagger and connexion
14+
* Tracing requests
15+
* [Services of PyMS](structure.md)

docs/ms_class.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Microservices class
2+
3+
The class Microservice is the core of all microservices built with PyMS.
4+
5+
6+
You can create a simple microservice such as:
7+
8+
```python
9+
from flask import jsonify
10+
11+
from pyms.flask.app import Microservice
12+
13+
ms = Microservice(service="my-minimal-microservice", path=__file__)
14+
app = ms.create_app()
15+
16+
17+
@app.route("/")
18+
def example():
19+
return jsonify({"main": "hello world"})
20+
21+
22+
if __name__ == '__main__':
23+
app.run()
24+
```
25+
26+
And a config file like this config.yml
27+
28+
```yaml
29+
my-minimal-microservice:
30+
APP_NAME: "Python Microservice"
31+
```
32+
Check [Configuration](configuration.md) section to know how to create a configuration file.
33+
34+
Each keyword in our configuration block, can be accessed in our Microservice object through the attribute `config`.
35+
36+
```yaml
37+
# Config.yml
38+
example-config:
39+
APP_NAME: "Python Microservice"
40+
foo: "var"
41+
multiplevars:
42+
config1: "test1"
43+
config2: "test2"
44+
45+
```
46+
```python
47+
#app.py
48+
from pyms.flask.app import Microservice
49+
50+
ms = Microservice(service="example-config", path=__file__)
51+
print(ms.config.APP_NAME)
52+
# >> "Python Microservice"
53+
print(ms.config.foo)
54+
# >> "bar"
55+
print(ms.config.multiplevars.config1)
56+
# >> "test1"
57+
print(ms.config.multiplevars.config2)
58+
# >> "test2"
59+
```
60+
61+
62+
63+
# Looking for Configuration file
64+
By default, Microservice class search a config.yml in the same path. You can set a different route or set a json file.
65+
To change this path, define a environment variable `CONFIGMAP_FILE`.
66+
67+
This way of looking for the configuration is useful when you work with Docker and Kubernetes. For example, you can integrate
68+
a configmap of Kubernetes, with this microservice and a deployment with:
69+
70+
```yaml
71+
apiVersion: extensions/v1beta1
72+
kind: Deployment
73+
metadata:
74+
name: my-microservice
75+
spec:
76+
replicas: 1
77+
template:
78+
spec:
79+
containers:
80+
- name: my-microservice
81+
image: ...
82+
env:
83+
- name: CONFIGMAP_FILE
84+
value: "/usr/share/microservice/config.yaml"
85+
86+
volumeMounts:
87+
- mountPath: /usr/share/microservice
88+
name: ms-config-volume
89+
volumes:
90+
- name: ms-config-volume
91+
configMap:
92+
name: my-microservice-configmap
93+
```
94+
95+
Check more examples in [this Github page](https://github.com/python-microservices/pyms/tree/master/examples)

docs/services.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Services
2+
3+
Services are libraries, resources and extensions added to the Microservice in the configuration file.
4+
This services are created as an attribute of the [Microservice class](ms_class.md) to use in the code.
5+
6+
To add a service check the [configuration section](configuration.md).
7+
8+
Current services are:
9+
10+
## Swagger / connexion
11+
Extends the Microservice with [Connexion](https://github.com/zalando/connexion)
12+
13+
## Requests
14+
Extend the [requests library](http://docs.python-requests.org/en/master/) with trace headers and parsing JSON objects.
15+
Encapsulate common rest operations between business services propagating trace headers if set up.

docs/structure.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Structure
2+
3+
### pyms/config
4+
Module to read yaml or json configuration from a dictionary or a path.
5+
6+
### pyms/flask/app
7+
With the function `create_app` initialize the Flask app, register [blueprints](http://flask.pocoo.org/docs/0.12/blueprints/)
8+
and initialize all libraries such as Swagger, database, trace system, custom logger format, etc.
9+
10+
### pyms/flask/healthcheck
11+
This view is usually used by Kubernetes, Eureka and other systems to check if our application is running.
12+
13+
### pyms/logger
14+
Print logger in JSON format to send to server like Elasticsearch. Inject span traces in logger.
15+
16+
### pyms/tracer
17+
Create an injector `flask_opentracing.FlaskTracer` to use in our projects.

docs/structure_project.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Project structure
2+
3+
See [Microservice Scaffold](https://microservices-scaffold.readthedocs.io/en/latest/structure.html) for more info.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM python:3.6.4-alpine3.7
2+
3+
RUN apk add --update curl gcc g++ git libffi-dev openssl-dev python3-dev build-base linux-headers \
4+
&& rm -rf /var/cache/apk/*
5+
6+
ENV PYTHONUNBUFFERED=1 APP_HOME=/microservice/
7+
ENV CONFIGMAP_FILE="$APP_HOME"config-docker.yml
8+
9+
RUN mkdir $APP_HOME && adduser -S -D -H python
10+
RUN chown -R python $APP_HOME
11+
WORKDIR $APP_HOME
12+
13+
ADD requirement*.txt $APP_HOME
14+
RUN pip install --upgrade pip
15+
RUN pip install -r requirements.txt
16+
17+
ADD . $APP_HOME
18+
19+
EXPOSE 5000
20+
USER python
21+
22+
CMD ["gunicorn", "--worker-class", "gevent", "--workers", "8", "--log-level", "INFO", "--bind", "0.0.0.0:5000", "manage:app"]

0 commit comments

Comments
 (0)