Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Wroc.py #32: Microservices in flask

314 views

Published on

How we are using flask to build microservices in python.

Published in: Technology
  • Be the first to comment

Wroc.py #32: Microservices in flask

  1. 1. Krzysztof Zuraw Microservices in Flask
  2. 2. Agenda Aplikacja monolityczna 2 Flask
  3. 3. Agenda Deploy 3 Testowanie
  4. 4. Aplikacja monolityczna 4
  5. 5. Aplikacja monolityczna 5 from app.users.models import UserSiteProfile from app.utils.cache import cache_key_user from app.sites.models import SiteProfile from app.sites.utils import site_terms from app.utils.users import get_homepage_url from app.utils.views import ThemedFormView, ThemedTemplateView from app.authentication import signals from app.authentication.forms import AuthForm, EmailForm from app.authentication.idp import ( is_valid_idp, MultipleIDPError, saml_available, site_idp, user_idp_lookup) from app.authentication.loginchecks import ( check_account_expiration, get_site_login_errors, pre_login_checks) from app.saml.utils import site_specific_config_loader from app.saml.views import _get_subject_id
  6. 6. Aplikacja monolityczna 6
  7. 7. 7 Niczego nie narzuca Flexible ale bez ‘bateries included’
  8. 8. Flask-Script 8 flask-script.readthedocs.io/en/latest/ from flask.ext.script import Manager, Shell, Server from my_app.application import app manager = Manager(app) manager.add_command( 'runserver' , Server(host= '0.0.0.0', port=7000, use_debugger= True) )
  9. 9. Flask-RESTful 9 flask-restful.readthedocs.io/en/0.3.5/ from flask_restful import Resource class MyResource(Resource): def get(): return {'OK'} @api.representation('application/xml' ) def output_xml(data, code, headers= None): response = make_response(dicttoxml.dicttoxml(data), code) inject_headers(response, headers) return response
  10. 10. Marshallow & flask-marshallow 10 marshmallow.readthedocs.io/en/latest/index.html ma = Marshmallow(app) class CustomSchema(ma.Schema): id = ma.Int(dump_only= True) name = ma.Str(required= True) url = ma.Url(relative= True) @app.route('/api/custom/<id>' ) def users(): all_obj = Object.all() result = object_schema.dump(all_obj) return jsonify(result.data)
  11. 11. Flask-SQLAlchemy 11 flask-sqlalchemy.pocoo.org/2.1/ from flask.ext.sqlalchemy import SQLAlchemy db = SQLAlchemy(app) class MyModel(db.Model): GROUP = 'group' USER = 'user' TYPES = [ (GROUP, GROUP), (USER, USER), ] __tablename__ = 'my_model' id = db.Column(db.Integer, primary_key= True) model_type = db.Column(ChoiceType(TYPES)) model_value_id = db.Column(db.Integer, db.ForeignKey( 'model_value.id' )) value = db.Column(db.String( 1024)) def __init__(self, model_type, model_value_id, value): self.share_type = share_type self.rule_value_id = rule_value_id self.value = value
  12. 12. Flask-Migrate 12 flask-migrate.readthedocs.io/en/latest/ def upgrade(): op.create_table( 'my_model', sa.Column( 'id', sa.Integer(), nullable= False), sa.Column( 'model_type' , sqlalchemy_utils.types.choice.ChoiceType(TYPES), nullable= True ), sa.PrimaryKeyConstraint( 'id'), sa.Column( 'value', sa.String(length= 1024), nullable= True), ) op.create_table( 'my_model_values' , sa.Column( 'id', sa.Integer(), nullable= False), sa.Column( 'model_value_id' , sa.Integer(), nullable= True), sa.Column( 'value', sa.String(length= 1024), nullable= True), sa.ForeignKeyConstraint([ 'model_value_id' ], ['my_model.id' ]), sa.PrimaryKeyConstraint( 'id') )
  13. 13. Testowanie 13
  14. 14. 14 Deployment R10k: github.com/puppetlabs/r10k
  15. 15. 15 Cookiecutter cookiecutter.readthedocs.io/en/latest/ . └── flask_microservice ├── AUTHORS.rst ├── debian ├── docs │ ├── make.bat │ ├── Makefile │ └── source │ ├── authors.rst │ ├── conf.py │ ├── contributing.rst │ ├── history.rst │ ├── index.rst │ ├── readme.rst │ ├── technical_details.rst │ └── usage.rst ├── HISTORY.rst ├── MANIFEST.in ├── README.rst ├── requirements.txt ├── setup.cfg ├── setup.py ├── src │ ├── flask_microservice │ │ ├── application.py │ │ ├── default_config.ini │ │ ├── __init__.py │ │ └── manage.py │ └── tests │ ├── conftest.py │ └── test_flask_microservice.py └── tox.ini
  16. 16. 16 Dh- virtualenv dh-virtualenv.readthedocs.io/en/latest/ override_dh_auto_test: echo "SKIPPING TESTS" %: dh $@ --with python-virtualenv override_dh_virtualenv: dh_virtualenv --python /usr/bin/python2.7 --setuptools --preinstall pip==6.1.1 --preinstall setuptools==18.7.1 --preinstall wheel==0.26.0 override_dh_builddeb: dh_builddeb --destdir= ${DESTINATION_DPKG_DIR }
  17. 17. QUESTIONS 17 Contact: @krzyszof_zuraw krzysztofzuraw.com

×