Skip to content

Commit 664fe58

Browse files
committed
Added pyms as lib
1 parent b245cfc commit 664fe58

File tree

18 files changed

+53
-190
lines changed

18 files changed

+53
-190
lines changed

config.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ms:
2+
DEBUG: false
3+
TESTING: false
4+
APP_NAME: Template
5+
APPLICATION_ROOT : /template
6+
SQLALCHEMY_TRACK_MODIFICATIONS: true
7+
SECRET_KEY: "gjr39dkjn344_!67#"

manage.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,9 @@
33

44
from project import create_app
55

6-
app, db = create_app()
6+
app = create_app()
77

88
manager = Manager(app)
99

10-
11-
@manager.command
12-
def create_db():
13-
"""Creates the db tables."""
14-
db.create_all()
15-
16-
17-
@manager.command
18-
def drop_db():
19-
"""Drops the db tables."""
20-
db.drop_all()
21-
22-
2310
if __name__ == '__main__':
2411
manager.run()

project/__init__.py

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,27 @@
1-
# encoding: utf-8
2-
3-
import logging
4-
import os
5-
6-
import connexion
7-
from flask_injector import FlaskInjector
8-
from injector import Injector
1+
from pyms.flask.app import Microservice
2+
from project.config import CONFIG
3+
from project.models.init_db import db
4+
from pyms.flask.app import Microservice
95

106
from project.config import CONFIG
11-
from pyms.healthcheck import healthcheck_blueprint
12-
from pyms.logger import CustomJsonFormatter
13-
from pyms.models import db
14-
from pyms.tracer.main import TracerModule
7+
from project.models.init_db import db
158

169
__author__ = "Alberto Vara"
1710
__email__ = "a.vara.1986@gmail.com"
18-
__version__ = "0.2"
11+
__version__ = "1.0"
1912

20-
logger = logging.getLogger('jaeger_tracing')
21-
logger.setLevel(logging.DEBUG)
2213

23-
ENVIRONMENT = os.environ.get("ENVIRONMENT", "default")
14+
class MyMicroservice(Microservice):
15+
def init_libs(self, app):
16+
db.init_app(app)
17+
with app.test_request_context():
18+
db.create_all()
2419

2520

2621
def create_app():
2722
"""Initialize the Flask app, register blueprints and intialize all libraries like Swagger, database, the trace system...
2823
return the app and the database objects.
2924
:return:
3025
"""
31-
environment = os.environ.get("ENVIRONMENT", "default")
32-
33-
app = connexion.App(__name__, specification_dir='./swagger/')
34-
# app.app.json_encoder = encoder.JSONEncoder
35-
36-
app.add_api('swagger.yaml',
37-
arguments={'title': 'Swagger Example Project'},
38-
base_path=CONFIG[environment].APPLICATION_ROOT)
39-
40-
application = app.app
41-
application.config.from_object(CONFIG[environment])
42-
db.init_app(application)
43-
44-
# Initialize Blueprints
45-
application.register_blueprint(healthcheck_blueprint)
46-
47-
# Inject Modules
48-
if not application.config["TESTING"] and not application.config["DEBUG"]:
49-
log_handler = logging.StreamHandler()
50-
formatter = CustomJsonFormatter('(timestamp) (level) (name) (module) (funcName) (lineno) (message)')
51-
formatter.add_service_name(application.config["APP_NAME"])
52-
tracer = TracerModule(application)
53-
injector = Injector([tracer])
54-
FlaskInjector(app=application, injector=injector)
55-
formatter.add_trace_span(tracer.tracer)
56-
log_handler.setFormatter(formatter)
57-
application.logger.addHandler(log_handler)
58-
application.logger.setLevel(logging.INFO)
59-
60-
with application.test_request_context():
61-
db.create_all()
62-
63-
return application, db
26+
ms = MyMicroservice(service="ms", path=__file__)
27+
return ms.create_app()

project/models/init_db.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from flask_sqlalchemy import SQLAlchemy
2+
3+
4+
db = SQLAlchemy()

project/models/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from sqlalchemy import Column, Integer, String, DateTime
77

8-
from pyms.models import db
8+
from project.models.init_db import db
99

1010

1111
def dump_datetime(value: datetime) -> list:

project/tests/config-tests.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ms:
2+
DEBUG: false
3+
TESTING: true
4+
APP_NAME: Template
5+
APPLICATION_ROOT : /template
6+
SQLALCHEMY_TRACK_MODIFICATIONS: true
7+
SECRET_KEY: "gjr39dkjn344_!67#"
8+
DATABASE: db_test.sqlite3
9+
SQLALCHEMY_DATABASE_URI: sqlite:///db_test.sqlite3

project/tests/test_views.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,38 @@
33
import unittest
44
from typing import Dict, List, Union, Text
55

6-
from project import create_app
6+
from project import MyMicroservice
7+
from pyms.constants import CONFIGMAP_FILE_ENVIRONMENT
78

89

910
def _format_response(response: Text = "") -> Union[List, Dict]:
1011
return json.loads(response)
1112

1213

1314
class ProjectTestCase(unittest.TestCase):
15+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
1416

1517
def setUp(self):
16-
os.environ["ENVIRONMENT"] = "test"
17-
self.app, self.db = create_app()
18+
os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(self.BASE_DIR, "config-tests.yml")
19+
ms = MyMicroservice(service="ms", path=os.path.dirname(__file__))
20+
self.app = ms.create_app()
1821
self.base_url = self.app.config["APPLICATION_ROOT"]
1922
self.client = self.app.test_client()
2023

2124
def tearDown(self):
22-
os.unlink(self.app.config['DATABASE'])
25+
pass # os.unlink(self.app.config['DATABASE'])
2326

2427
def test_home(self):
2528
response = self.client.get('/')
26-
self.assertEqual(response.status_code, 404)
29+
self.assertEqual(404, response.status_code)
2730

2831
def test_healthcheck(self):
2932
response = self.client.get('/healthcheck')
30-
self.assertEqual(response.status_code, 200)
33+
self.assertEqual(200, response.status_code)
3134

3235
def test_list_view(self):
3336
response = self.client.get('{base_url}/'.format(base_url=self.base_url))
34-
self.assertEqual(response.status_code, 200)
35-
self.assertEqual(_format_response(response.data), [])
37+
self.assertEqual(200, response.status_code)
3638

3739
def test_create_view(self):
3840
name = "blue"
@@ -41,5 +43,5 @@ def test_create_view(self):
4143
data=json.dumps(dict(name=name)),
4244
content_type='application/json'
4345
)
44-
self.assertEqual(response.status_code, 200)
45-
self.assertEqual(_format_response(response.data)["name"], name)
46+
self.assertEqual(200, response.status_code)
47+
self.assertEqual(name, _format_response(response.data)["name"])

project/views/views.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# encoding: utf-8
22
from __future__ import absolute_import, print_function, unicode_literals
3+
34
import connexion
4-
from flask import request, jsonify, current_app
5+
from flask import jsonify, current_app
56

6-
from pyms.models import db
7+
from project.models.init_db import db
78
from project.models.models import Colors
89

910

@@ -28,4 +29,4 @@ def create_view():
2829
db.session.commit()
2930

3031
return jsonify(color.serialize)
31-
return jsonify({})
32+
return jsonify({})

pyms/__init__.py

Whitespace-only changes.

pyms/healthcheck/__init__.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)