Skip to content

Commit 54243a5

Browse files
committed
Merge branch 'feature/update_docs' into develop
2 parents 7f78ab2 + c219dd9 commit 54243a5

File tree

16 files changed

+227
-18
lines changed

16 files changed

+227
-18
lines changed

.travis.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@ sudo: false
33
cache: false
44
python:
55
- '3.6'
6+
addons:
7+
apt:
8+
sources:
9+
- deadsnakes
10+
packages:
11+
- python3.5
12+
- python3.5-dev
613
install:
7-
- pip install -r requirements-tests.txt
14+
- pip install -U tox coveralls
815

916
script:
1017
- coverage erase
11-
- coverage run -m unittest
18+
- tox
1219
after_success:
1320
- coverage combine
1421
- coveralls
1522

1623
notifications:
1724
email:
18-
- a.vara.1986@gmail.com
25+
- a.vara.1986@gmail.com

app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)
4+
5+
6+
@app.route('/')
7+
def pull_requests():
8+
return 'Hello world'

docs/configuration.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
Configuration
22
=============
33

4+
The project is configured in `project/config.py`. You must set the project name `APP_NAME` and the path prefix
5+
of the project `APPLICATION_ROOT`. This constants is defined in the class Config:
46

7+
.. code-block:: python
58
9+
class Config:
10+
DEBUG = False
11+
TESTING = False
12+
APP_NAME = "Template"
13+
APPLICATION_ROOT = "/template"
14+
15+
Documentation
16+
-------------
17+
The Microservice create a URL to inspect the Swagger documentation of the api in:
18+
19+
.. code-block:: bash
20+
localhost:5000/[APPLICATION_ROOT]/apidocs/
21+
22+
Our API Rest work with `Flasgger <https://github.com/rochacbruno/flasgger>`_. You can see Flasgger docs or the official
23+
`Swagger documentation <https://swagger.io/specification/>`_ to add the syntax to your APIS and create your Swagger docs

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ Content
2424
structure
2525
configuration
2626
codeexample
27+
project

docs/installation.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,74 @@
11
Installation
22
============
33

4+
Clone the project
5+
6+
.. code-block:: bash
7+
git clone https://github.com/python-microservices/microservices-scaffold.git
8+
9+
Configure your project and the path of your MS. See :doc:`configuration </configuration>` section.
10+
11+
Configure your setup.py with your project information
12+
13+
14+
15+
Use MS with Docker
16+
------------------
17+
`Install docker <https://docs.docker.com/install/>`_
18+
19+
20+
Use MS with Kubernetes localy
21+
-----------------------------
22+
Configure your service.yaml (TODO: create docs to configure kubernetes service.yaml)
23+
24+
* Installing Kubernetes...
25+
26+
.. code-block:: bash
27+
28+
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.9.0/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
29+
30+
* Installing Minikube...
31+
32+
.. code-block:: bash
33+
34+
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
35+
36+
37+
Start minikube and set the environment of docker
38+
39+
.. code-block:: bash
40+
41+
minikube start
42+
eval $(minikube docker-env)
43+
kubectl config use-context minikube
44+
45+
If a shell isn't your friend. You could use kubernetes web panel to see your pods:
46+
47+
.. code-block:: bash
48+
49+
minikube dashboard
50+
51+
Create your image
52+
53+
.. code-block:: bash
54+
55+
docker build -t your-app:v1 .
56+
57+
Deploy your images localy:
58+
59+
.. code-block:: bash
60+
61+
kubectl create -f service.yaml
62+
minikube service your-app
63+
64+
65+
Clean your environment
66+
67+
.. code-block:: bash
68+
69+
kubectl delete service your-app
70+
kubectl delete deployment your-app
71+
docker rmi your-app:v1 -f
72+
minikube stop
73+
eval $(minikube docker-env -u)
74+
minikube delete

docs/project.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Project
2+
=======
3+
4+
.. automodule:: project
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
8+

docs/quickstart.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Quickstart
22
==========
33

4-
4+
.. code-block:: bash
5+
git clone https://github.com/python-microservices/microservices-scaffold.git
6+
cd microservices-scaffold
7+
python manage.py runserver

docs/structure.rst

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,40 @@ You have a project with this structure:
1111
├ config.py
1212
├ views
1313
│ ├ __init__.py
14-
│ └ views.py
14+
│ ├ views.py
15+
│ └ healthcheck.py
1516
├ models
1617
│ ├ __init__.py
1718
│ └ models.py
18-
└ models
19-
└ test_views.py
19+
└ tests
20+
└ test_views.py
21+
22+
manager.py
23+
----------
24+
25+
A Django style command line. Use this to start the application like:
26+
27+
.. code-block:: bash
28+
python manage.py runserver
29+
30+
You can set the host and the port with:
31+
32+
.. code-block:: bash
33+
python manage.py runserver -h 0.0.0.0 -p 8080
34+
35+
project/__init__.py
36+
-------------------
37+
This file init the project with the funcion `create_app`. Initialize the Flask app, register `blueprints <http://flask.pocoo.org/docs/0.12/blueprints/>`_
38+
and intialize all libraries like Swagger, database, the trace system...
39+
40+
project/config.py
41+
-----------------
42+
See :doc:`configuration </configuration>` section
43+
44+
project/views
45+
-------------
46+
use views.py or create your file. You must add after register the view blueprint in `project/views/__init__.py`.
47+
48+
project/views/healthcheck.py
49+
----------------------------
50+
This views is usually used by Kubernetes, Eureka and other systems to check if our application is up and running

project/__init__.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from flasgger import Swagger
66
from flask import Flask
7+
from flask_opentracing import FlaskTracer
8+
from jaeger_client import Config
79

810
from project.config import CONFIG
911

@@ -45,34 +47,62 @@
4547
}
4648

4749

48-
class PrefixMiddleware(object):
50+
def init_jaeger_tracer(service_name='your-app-name'):
51+
"""This scaffold is configured whith `Jeager <https://github.com/jaegertracing/jaeger>`_ but you can use
52+
one of the `opentracing tracers <http://opentracing.io/documentation/pages/supported-tracers.html>`_
53+
:param service_name: the name of your application to register in the tracer
54+
:return: opentracing.Tracer
55+
"""
56+
config = Config(config={
57+
'sampler': {'type': 'const', 'param': 1}
58+
}, service_name=service_name)
59+
return config.initialize_tracer()
60+
4961

62+
class PrefixMiddleware(object):
63+
"""Set a prefix path to all routes. This action is needed if you have a stack of microservices and each of them
64+
exist in the same domain but different path. Por example:
65+
* mydomain.com/ms1/
66+
* mydomain.com/ms2/
67+
"""
5068
def __init__(self, app, prefix=''):
5169
self.app = app
5270
self.prefix = prefix
5371

5472
def __call__(self, environ, start_response):
55-
5673
if environ['PATH_INFO'].startswith(self.prefix):
5774
environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):]
5875
environ['SCRIPT_NAME'] = self.prefix
5976
return self.app(environ, start_response)
77+
elif environ['PATH_INFO'].startswith("/healthcheck"):
78+
return self.app(environ, start_response)
6079
else:
6180
start_response('404', [('Content-Type', 'text/plain')])
6281
return ["This url does not belong to the app.".encode()]
6382

6483

6584
def create_app():
85+
"""Initialize the Flask app, register blueprints and intialize all libraries like Swagger, database, the trace system...
86+
return the app and the database objects.
87+
:return:
88+
"""
6689
from project.models import db
6790
from project.views import views_bp as views_blueprint
91+
from project.views import views_hc as views_hc_blueprint
6892
environment = os.environ.get("ENVIRONMENT", "default")
6993

7094
app = Flask(__name__)
7195
app.config.from_object(CONFIG[environment])
7296
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=app.config["APPLICATION_ROOT"])
7397

98+
99+
if not app.config["TESTING"] and not app.config["DEBUG"]:
100+
j_tracer = init_jaeger_tracer(app.config["APP_NAME"])
101+
FlaskTracer(j_tracer, True, app)
102+
74103
db.init_app(app)
75104

105+
# Initialize Swagger
76106
SWAGGER_CONFIG["specs"][0]["route"] = SWAGGER_CONFIG["specs"][0]["route"].format(
77107
application_root=app.config["APPLICATION_ROOT"]
78108
)
@@ -87,7 +117,9 @@ def create_app():
87117
)
88118
Swagger(app, config=SWAGGER_CONFIG)
89119

120+
# Initialize Blueprints
90121
app.register_blueprint(views_blueprint)
122+
app.register_blueprint(views_hc_blueprint)
91123
with app.test_request_context():
92124
db.create_all()
93125
return app, db

project/views/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
from flask import Blueprint
55

66
views_bp = Blueprint('views', __name__, static_url_path='/static')
7+
views_hc = Blueprint('healthcheck', __name__, static_url_path='/static')
78

8-
from project.views import views
9+
from project.views import views, healthcheck

0 commit comments

Comments
 (0)