Skip to content

Commit 36f345d

Browse files
committed
Updated docs
1 parent 052ec41 commit 36f345d

File tree

4 files changed

+132
-16
lines changed

4 files changed

+132
-16
lines changed

README.md

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,51 @@ Nowadays, is not perfect and we have a looong roadmap, but we hope this library
3939
pip install py-ms
4040
```
4141

42-
## Structure
42+
# Quickstart
4343

44-
### pyms/config
45-
Module to read yaml or json configuration from a dictionary or a path.
44+
You need to create 2 files: main.py and config.yml:
4645

47-
### pyms/flask/app
48-
With the function `create_app` initialize the Flask app, register [blueprints](http://flask.pocoo.org/docs/0.12/blueprints/)
49-
and initialize all libraries such as Swagger, database, trace system, custom logger format, etc.
46+
main.py
47+
```python
48+
from flask import jsonify
5049

51-
### pyms/flask/services
52-
Integrations and wrappers over common libs like request, swagger, connexion
50+
from pyms.flask.app import Microservice
5351

54-
### pyms/flask/healthcheck
55-
This view is usually used by Kubernetes, Eureka and other systems to check if our application is running.
52+
ms = Microservice(path=__file__) # 1.1
53+
app = ms.create_app() # 2.1
5654

57-
### pyms/logger
58-
Print logger in JSON format to send to server like Elasticsearch. Inject span traces in logger.
5955

60-
### pyms/tracer
61-
Create an injector `flask_opentracing.FlaskTracer` to use in our projects.
56+
@app.route("/") # 3.1
57+
def example():
58+
return jsonify({"main": "hello world"})
59+
60+
61+
if __name__ == '__main__':
62+
app.run()
63+
```
64+
65+
config.yml
66+
```yaml
67+
pyms: # 1.2
68+
requests:
69+
data: {}
70+
ms: # 1.3
71+
DEBUG: true
72+
APP_NAME: business-glossary
73+
APPLICATION_ROOT : ""
74+
SECRET_KEY: "gjr39dkjn344_!67#"
75+
```
76+
77+
### So what did that code do?
78+
79+
1. Create a instance of PyMS Microservice class (#1.1). This initialization inject the configuration defined in the
80+
1.3 block and could be accessed through current_app.config. Then, initialize the service defined in the 1.2 block. See [Services](services.md) for more details.
81+
2. Initialize [Flask](https://flask.palletsprojects.com/en/1.1.x/) instance, [Connexion](https://github.com/zalando/connexion)
82+
if it was defined in the pyms configuration block, create a tracer, add health-check blueprint, initialize libs and set the PyMS Microservice in
83+
`ms` attribute and you can access to it with `current_app.ms`. This steps has their each functions and you can easy override it.
84+
3. `create_app` return the flask instance and you can interact with it as a typical flask app
85+
86+
See Documentation https://py-ms.readthedocs.io/en/latest/ to learn more.
6287

6388
## How To Contrib
6489
We appreciate opening issues and pull requests to make PyMS even more stable & useful! See [This doc](COONTRIBUTING.md)

docs/ms_class.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,89 @@ print(ms.config.multiplevars.config2)
5858
# >> "test2"
5959
```
6060

61+
## Personalize your microservices
62+
63+
Microservice class initialize the libraries and other process by this way:
64+
65+
```python
66+
...
67+
def create_app(self):
68+
"""Initialize the Flask app, register blueprints and initialize
69+
all libraries like Swagger, database,
70+
the trace system...
71+
return the app and the database objects.
72+
:return:
73+
"""
74+
self.application = self.init_app()
75+
self.application.config.from_object(self.config)
76+
self.application.tracer = None
77+
self.application.ms = self
78+
79+
# Initialize Blueprints
80+
self.application.register_blueprint(healthcheck_blueprint)
81+
82+
self.init_libs()
83+
self.add_error_handlers()
84+
85+
self.init_tracer()
86+
87+
self.init_logger()
88+
89+
return self.application
90+
```
91+
92+
Create a class that innerit from `pyms.flask.app.Microservice` and create and override methods with your own configuration.
93+
The next example show how to create your own logger and innit a lib like [Flask Babel](https://pythonhosted.org/Flask-Babel/)
94+
95+
```python
96+
class MyMicroservice(Microservice):
97+
def init_tracer(self):
98+
pass # Disabled tracer
99+
100+
def init_libs(self):
101+
babel = Babel(self.application)
102+
103+
return self.application
104+
105+
def init_logger(self):
106+
level = "INFO"
107+
LOGGING = {
108+
'version': 1,
109+
'disable_existing_loggers': False,
110+
'formatters': {
111+
'console': {
112+
'format': '[%(asctime)s][%(levelname)s] %(name)s '
113+
'%(filename)s:%(funcName)s:%(lineno)d | %(message)s',
114+
'datefmt': '%H:%M:%S',
115+
},
116+
},
117+
'handlers': {
118+
'console': {
119+
'level': level,
120+
'class': 'logging.StreamHandler',
121+
'formatter': 'console'
122+
},
123+
},
124+
'loggers': {
125+
'': {
126+
'handlers': ['console'],
127+
'level': level,
128+
'propagate': True,
129+
},
130+
'root': {
131+
'handlers': ['console'],
132+
'level': level,
133+
'propagate': True,
134+
},
135+
}
136+
}
137+
138+
logging.config.dictConfig(LOGGING)
139+
140+
ms = MyMicroservice(path=__file__)
141+
app = ms.create_app()
142+
```
143+
144+
145+
61146
Check more examples in [this Github page](https://github.com/python-microservices/pyms/tree/master/examples)

docs/quickstart.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ ms: # 1.3
3535
SECRET_KEY: "gjr39dkjn344_!67#"
3636
```
3737
38-
So what did that code do?
38+
## So what did that code do?
39+
3940
1. Create a instance of PyMS Microservice class (#1.1). This initialization inject the configuration defined in the
4041
1.3 block and could be accessed through current_app.config. Then, initialize the service defined in the 1.2 block. See [Services](services.md) for more details.
4142
2. Initialize [Flask](https://flask.palletsprojects.com/en/1.1.x/) instance, [Connexion](https://github.com/zalando/connexion)

docs/services.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ Extends the Microservice with [Connexion](https://github.com/zalando/connexion)
1212

1313
## Requests
1414
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.
15+
Encapsulate common rest operations between business services propagating trace headers if set up.
16+
17+
18+
## How to contrib: create your own service:
19+
20+
TODO

0 commit comments

Comments
 (0)