Skip to content

Commit

Permalink
Added tutorials and new examples
Browse files Browse the repository at this point in the history
  • Loading branch information
avara1986 committed Mar 14, 2020
1 parent 9a4cede commit 14942ae
Show file tree
Hide file tree
Showing 13 changed files with 367 additions and 94 deletions.
110 changes: 109 additions & 1 deletion docs/examples.md
@@ -1,5 +1,7 @@
# Examples

## Example 1: Basic Example

```bash
pip install py-ms[all]
```
Expand All @@ -19,7 +21,7 @@ from flask import jsonify, current_app

from pyms.flask.app import Microservice

ms = Microservice(path=__file__)
ms = Microservice()
app = ms.create_app()


Expand All @@ -38,4 +40,110 @@ python main.py

Open in your browser http://localhost:5000/

## Example 2: Create your Microservice class

Create a class that inherit from `pyms.flask.app.Microservice` and override methods with your own configuration.
The next example show how to innit a lib like [Flask Babel](https://pythonhosted.org/Flask-Babel/)

main.py:

```python
from flask_babel import Babel
from pyms.flask.app import Microservice

class MyMicroservice(Microservice):
def init_libs(self):
babel = Babel(self.application)

return self.application

ms = MyMicroservice()
app = ms.create_app()
```

## Example 2: Initialize SQLAlchemy

The next example show how to innit a lib like [Flask SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/2.x/)

config.yml:

```yaml
pyms:
config:
DEBUG: true
APP_NAME: MyDB
APPLICATION_ROOT: ""
SQLALCHEMY_DATABASE_URI: mysql+mysqlconnector://user:pass@0.0.0.0/myschema
```
main.py:

```python
from flask_sqlalchemy import SQLAlchemy
from pyms.flask.app import Microservice

DB = SQLAlchemy()


class MyMicroservice(Microservice):
def init_libs(self):
self.application.config["SQLALCHEMY_ENGINE_OPTIONS"] = {
'pool_size': 10,
'pool_recycle': 120,
'pool_pre_ping': True
}
DB.init_app(self.application)

ms = MyMicroservice()
app = ms.create_app()
```

## Example 3: Create your logger

The next example show how to create a personal logger for your application

```python
import logging.config

from pyms.flask.app import Microservice


class MyMicroservice(Microservice):
def init_logger(self):
level = "INFO"
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'console': {
'format': '[%(asctime)s][%(levelname)s] %(name)s '
'%(filename)s:%(funcName)s:%(lineno)d | %(message)s',
'datefmt': '%H:%M:%S',
},
},
'handlers': {
'console': {
'level': level,
'class': 'logging.StreamHandler',
'formatter': 'console'
},
},
'loggers': {
'': {
'handlers': ['console'],
'level': level,
'propagate': True,
},
'root': {
'handlers': ['console'],
'level': level,
'propagate': True,
},
}
}
logging.config.dictConfig(LOGGING)

ms = MyMicroservice(path=__file__)
app = ms.create_app()
```

See [this Github page](https://github.com/python-microservices/pyms/tree/master/examples) to see a examples
Binary file added docs/imgs/PyMSdistributedtracing.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/imgs/multiple-ms.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions docs/index.md
Expand Up @@ -46,5 +46,7 @@ Nowadays, is not perfect and we have a looong roadmap, but we hope this library
* [Encrypt/Decrypt Configuration](encrypt_decryt_configuration.md)
* [Command line](command_line.md)
* [Examples](examples.md)
* [Tutorials](tutorials.md)
* [Tutorial: Propagate traces](tutorial_propagate_traces.md)
* [PyMS structure](structure.md)
* [Structure of a microservice project](structure_project.md)
92 changes: 0 additions & 92 deletions docs/ms_class.md
Expand Up @@ -96,97 +96,5 @@ Microservice class initialize the libraries and other process by this way:
return self.application
```

## Example 1: Basic Example

Create a class that inherit from `pyms.flask.app.Microservice` and override methods with your own configuration.
The next example show how to innit a lib like [Flask Babel](https://pythonhosted.org/Flask-Babel/)

```python
from flask_babel import Babel
from pyms.flask.app import Microservice

class MyMicroservice(Microservice):
def init_libs(self):
babel = Babel(self.application)

return self.application

ms = MyMicroservice(path=__file__)
app = ms.create_app()
```

## Example 2: Initialize SQLAlchemy

The next example show how to innit a lib like [Flask SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/2.x/)

```python
from flask_sqlalchemy import SQLAlchemy
from pyms.flask.app import Microservice

DB = SQLAlchemy()


class MyMicroservice(Microservice):
def init_libs(self):
self.application.config["SQLALCHEMY_ENGINE_OPTIONS"] = {
'pool_size': 10,
'pool_recycle': 120,
'pool_pre_ping': True
}
DB.init_app(self.application)

ms = MyMicroservice(path=__file__)
app = ms.create_app()
```

## Example 3: Create your logger

The next example show how to create a personal logger for your application

```python
import logging.config

from pyms.flask.app import Microservice


class MyMicroservice(Microservice):
def init_logger(self):
level = "INFO"
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'console': {
'format': '[%(asctime)s][%(levelname)s] %(name)s '
'%(filename)s:%(funcName)s:%(lineno)d | %(message)s',
'datefmt': '%H:%M:%S',
},
},
'handlers': {
'console': {
'level': level,
'class': 'logging.StreamHandler',
'formatter': 'console'
},
},
'loggers': {
'': {
'handlers': ['console'],
'level': level,
'propagate': True,
},
'root': {
'handlers': ['console'],
'level': level,
'propagate': True,
},
}
}
logging.config.dictConfig(LOGGING)

ms = MyMicroservice(path=__file__)
app = ms.create_app()
```


Check more examples in [this Github page](https://github.com/python-microservices/pyms/tree/master/examples)
3 changes: 2 additions & 1 deletion docs/services.md
Expand Up @@ -79,7 +79,8 @@ pyms:

## Tracer

Add trace to all executions with [opentracing](https://github.com/opentracing-contrib/python-flask).
Add trace to all executions with [opentracing](https://github.com/opentracing-contrib/python-flask). This service
solves the problem of [distributed tracing](https://microservices.io/patterns/observability/distributed-tracing.html)

### Installation

Expand Down

0 comments on commit 14942ae

Please sign in to comment.