api-centric web development with tornado or the great refactoring story

44
API-centric Web Development with Tornado or The Great Refactoring Story Roman Zaiev, special for

Upload: roman-zaiev

Post on 12-Aug-2015

59 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: API-centric Web Development with Tornado or The Great Refactoring Story

API-centric Web Development with Tornado or The Great Refactoring Story

Roman Zaiev, special for

Page 2: API-centric Web Development with Tornado or The Great Refactoring Story

Tornado? Why not Django / Flask / etc

Page 3: API-centric Web Development with Tornado or The Great Refactoring Story
Page 4: API-centric Web Development with Tornado or The Great Refactoring Story

is

Page 5: API-centric Web Development with Tornado or The Great Refactoring Story

is prototyping tool

Page 6: API-centric Web Development with Tornado or The Great Refactoring Story
Page 7: API-centric Web Development with Tornado or The Great Refactoring Story

djangoproject/ manage.py project/ __init__.py urls.py wsgi.py settings/ __init__.py base.py dev.py prod.py blog/ __init__.py models.py managers.py views.py urls.py templates/ blog/ base.html list.html detail.html static/ … tests/ __init__.py test_models.py test_managers.py test_views.py static/ css/ … js/ … templates/ base.html index.html requirements/ base.txt dev.txt test.txt prod.txt

Django Project Structure

Page 8: API-centric Web Development with Tornado or The Great Refactoring Story

Modern Django Admin

Page 9: API-centric Web Development with Tornado or The Great Refactoring Story

Django ORM Hell

Page 10: API-centric Web Development with Tornado or The Great Refactoring Story

Django NoSQL? No. SQL!

Page 11: API-centric Web Development with Tornado or The Great Refactoring Story

RedirectView

TemplateView

DetailView

UpdateView

CreateView

FormView

DeleteView

DateDetailView

ListView

DayArchiveView

ArchiveIndexView

TodayArchiveView

MonthArchiveView

YearArchiveView

WeekArchiveView

BaseUpdateView

BaseCreateView

BaseFormView

SingleObjectTemplateResponseMixin

BaseListView

BaseArchiveIndexView

MultipleObjectTemplateResponseMixin

BaseTodayArchiveView

ModelFormMixin

ProcessFormMixin

TemplateResponseMixin

BaseDeleteView

BaseDateDetailView

BaseDayArchiveView

BaseMonthArchiveView

BaseYearArchiveView

BaseWeekArchiveView

FormMixin

DeletionMixin

BaseDetailView

SingleObjectMixin

MultipleObjectMixin

ContextMixin

DateMixin

BaseDateListView

WeekMixin| connect the dotsCBV

Page 12: API-centric Web Development with Tornado or The Great Refactoring Story

Kill meWTF?!

Page 13: API-centric Web Development with Tornado or The Great Refactoring Story

Django is bad

Page 14: API-centric Web Development with Tornado or The Great Refactoring Story

Monolith is bad

Page 15: API-centric Web Development with Tornado or The Great Refactoring Story

first steprefactoring

Page 16: API-centric Web Development with Tornado or The Great Refactoring Story

auth

landingsbasketcheckoutproduct

second stepAPI

emails

Page 17: API-centric Web Development with Tornado or The Great Refactoring Story

API to rule them all

Page 18: API-centric Web Development with Tornado or The Great Refactoring Story

ADD. API Driven Development

Page 19: API-centric Web Development with Tornado or The Great Refactoring Story

POST /api/v1/authorize GET /api/v1/user GET /api/v1/product

POST /users/ajax_user_auth GET /products/ajax_current_user GET /products/ajax_prod_info

API Business logic

Templates rendering SEO Urls

Mob

WebUI AJAX

UI API Calls

Page 20: API-centric Web Development with Tornado or The Great Refactoring Story

Front

Mob

API

Templates rendering API Calls SEO Urls

Web UI AJAX

POST /api/v1/authorize GET /api/v1/user GET /api/v1/product

UI API Calls

Page 21: API-centric Web Development with Tornado or The Great Refactoring Story

API

Monolith Services

Page 22: API-centric Web Development with Tornado or The Great Refactoring Story

A A

A A

A A

A A

A A

A

AA A

A A

A A

A

A

A

A

A A A A

auth

product landings

A A

A A

basket

A

P

T

API

Page 23: API-centric Web Development with Tornado or The Great Refactoring Story

Stack Choice

framework? transport? proxy?

PTA

Page 24: API-centric Web Development with Tornado or The Great Refactoring Story

A framework

Flask? Yes. But no.

Page 25: API-centric Web Development with Tornado or The Great Refactoring Story

Easy Light Fast Async Python 2.7

Django

Flask

Pyramid

Tornado

A framework

Page 26: API-centric Web Development with Tornado or The Great Refactoring Story

tornadoweb.org

A framework

Page 27: API-centric Web Development with Tornado or The Great Refactoring Story

import tornado.ioloop import tornado.web

class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world")

application = tornado.web.Application([ (r"/", MainHandler), ])

if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.current().start()

A Tornado | Start

Page 28: API-centric Web Development with Tornado or The Great Refactoring Story

A Tornado | Async handler

class GenAsyncHandler(tornado.web.RequestHandler):

@gen.coroutine def get(self): http_client = AsyncHTTPClient() response = yield http_client.fetch("http://async.ua") do_something_with_response(response) self.render("template.html")

Page 29: API-centric Web Development with Tornado or The Great Refactoring Story

PT

Tornado transport? proxy?

Page 30: API-centric Web Development with Tornado or The Great Refactoring Story

transport

ZeroMQ + Protocol Buffers

T

Page 31: API-centric Web Development with Tornado or The Great Refactoring Story

ProtobufT

message BannerItem { required string link = 1; required string image = 2; optional string title = 3; required string alt = 4; required string selector = 5; required string last_edited = 6; required string last_editor = 7; optional string date_start = 8; optional string date_end = 9; optional int32 order = 10; }

‘\n\x01/\x12\x07img.png\x1a\x04Frau "\x06Muller*\x07.snoopy2\tyesterday:\x02me'

new_banner = banner_pb2.BannerItem() new_banner.link = ‘/' new_banner.image = ‘img.png' new_banner.title = ‘Frau' new_banner.alt = ‘Muller' new_banner.selector = ‘.snoopy' new_banner.last_edited = ‘yesterday' new_banner.last_editor = ‘me'

new_banner.SerializeToString()

Page 32: API-centric Web Development with Tornado or The Great Refactoring Story

P

Tornado ZeroMQ proxy?

Page 33: API-centric Web Development with Tornado or The Great Refactoring Story

proxyP

Nginx

Page 34: API-centric Web Development with Tornado or The Great Refactoring Story

Tornado ZeroMQ Nginx

Page 35: API-centric Web Development with Tornado or The Great Refactoring Story

auth

product landings

basket

API

Page 36: API-centric Web Development with Tornado or The Great Refactoring Story

API

Monolith Services

Page 37: API-centric Web Development with Tornado or The Great Refactoring Story

API Front

Page 38: API-centric Web Development with Tornado or The Great Refactoring Story

A Tornado | API HTTP

class ProductAPIHandler(BaseAPIHandler):

def get(self): user_attrs = self.request.arguments pbf_response = get_product(self, **user_attrs) response = protobuf_to_dict( pbf_response, ignore_list=['body'] ) if pbf_response.success: product = product_pb2.ProductSample() product.ParseFromString(pbf_response.body) response['body'] = protobuf_to_dict(product)

self.write(response)

Page 39: API-centric Web Development with Tornado or The Great Refactoring Story

A Tornado | API ZeroMQ

class ProductZMQHandler(BaseZMQHandler):

def get(self): user_attrs = self.request.arguments pbf_response = get_product(self, **user_attrs) return pbf_response

Page 40: API-centric Web Development with Tornado or The Great Refactoring Story

A Tornado | API Call

class ProductHandler(BaseHandler): TEMPLATE = 'pdp.html'

@tornado.web.asynchronous def get(self, slug): zmq_stream = get_zmq_client(self) requests = [{ 'url': '/api/v1/product', 'callback': self.gen_product, 'data': { 'url': slug }, }] self.pbc_client( zmq_stream=zmq_stream, requests=requests, callback=self.on_fetch, ) ...

Page 41: API-centric Web Development with Tornado or The Great Refactoring Story

A Tornado | API Call Processing

class ProductHandler(BaseHandler):

...

def gen_product(self, response): if not response.success: raise HTTPError(404)

product_pbc = product_pb2.ProductSample() product_pbc.ParseFromString(response.body) product_dict = protobuf_to_dict(product_pbc)

self.context['product'] = product_dict

def on_fetch(self): self.render(self.TEMPLATE, **self.context)

Page 42: API-centric Web Development with Tornado or The Great Refactoring Story

Find your balance

Page 43: API-centric Web Development with Tornado or The Great Refactoring Story

Use proper technologies

Page 44: API-centric Web Development with Tornado or The Great Refactoring Story

github.com/semirookua.linkedin.com/in/semirook

Thank you! And good luck!