0
Tuesday, July 17, 12
What is Brubeck?                       • A Mongrel2 Handler                       • A Web Framework and Django            ...
What is a “Mongrel2”?                       • A Mongrel2 is an asynchronous web server                       • Delegatesth...
Mongrel2 + A Handler                                     Mongrel 2                           Push / Pull                 P...
What is a Mongrel2 Handler?                       • Processes messages from Mongrel2                         • Essentially...
Mongrel2 + Brubecks                                    Mongrel 2                          Push / Pull                 Pub ...
Hello world Take five!                       class DemoHandler(WebMessageHandler):                           def get(self):...
Hello world Take five!                  class DemoHandler(WebMessageHandler):                      def get(self):          ...
Brubeck: routing                                 Went with the usual style                   class NameHandler(WebMessageH...
Brubeck: templates                       Supports: Jinja2, Mako,Tornado or Mustache        from brubeck.templating import ...
Brubeck: auth (pt 1)               •       Simple example, using `web_authenticated` decorator:        class DemoHandler(....
Brubeck: auth (pt 2)         class BaseHandler(..., UserHandlingMixin):             def get_current_user(self):           ...
Brubeck: user               •       This what a Brubeck user model looks like        class User(Document):            user...
Brubeck: data validation               •       Validation is easy       >>> from brubeck.models import User       >>> u = ...
Databaseless modeling               •       This what a Brubeck user looks like as Python                       >>>   user...
Databaseless modeling                       •   Persistence details are up to you            # Mongo            >>> db.use...
Brubeck: autoapi                       Automatic REST APIs from data models (!!)                                  (Ben Bee...
Brubeck: autoapi                                        A Todo API                       # Define Todo model              ...
Brubeck: autoapi                $ curl -f                     -X POST                     -H "content-type: application/js...
Todos: backbone.js                       AutoAPI works great with backbone.js                            ( https://github....
bpm                          (brubeck project manager)          $ bpm create project -n demo          $ cd demo          $...
Questions ??               Brubeck: http://brubeck.io               Code:    https://github.com/j2labs/brubeck            ...
Upcoming SlideShare
Loading in...5
×

Brubeck: Overview

982

Published on

The latest version of the slides I use when I give talks on Brubeck.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
982
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Brubeck: Overview"

  1. 1. Tuesday, July 17, 12
  2. 2. What is Brubeck? • A Mongrel2 Handler • A Web Framework and Django • Influenced by Flask,Tornado • A pipeline of coroutines • Some coded opinions and a few libraries • Backend agnostic • Database agnostic • No spaghetti codeTuesday, July 17, 12
  3. 3. What is a “Mongrel2”? • A Mongrel2 is an asynchronous web server • Delegatesthat part to external handlers handling • We build • They communicate across 2 ZeroMQ sockets • Less processing than HTTP • Language agnostic JSON or tnetstring • Simple messaging via • ZeroMQ is language agnostic, thus so is Mongrel2Tuesday, July 17, 12
  4. 4. Mongrel2 + A Handler Mongrel 2 Push / Pull Pub / Sub Handler Handler HandlerTuesday, July 17, 12
  5. 5. What is a Mongrel2 Handler? • Processes messages from Mongrel2 • Essentially, a Zed specific WSGI • There has been some dissent: Y U NO USE WSGI? • Responds in HTTP • A ZeroMQ sockets are language agnostic language opinion • • Zed chose Lua when he built Tir • I liked his model, but I like Python too • Lots of languages now supportedTuesday, July 17, 12
  6. 6. Mongrel2 + Brubecks Mongrel 2 Push / Pull Pub / Sub Handler Handler BrubeckTuesday, July 17, 12
  7. 7. Hello world Take five! class DemoHandler(WebMessageHandler):     def get(self):         self.set_body(Take five!)         return self.render() urls = [(^/brubeck, DemoHandler)]Tuesday, July 17, 12
  8. 8. Hello world Take five! class DemoHandler(WebMessageHandler):     def get(self):         self.set_body(Take five!)         return self.render() urls = [(^/brubeck, DemoHandler)] @app.add_route(^/brubeck, method=GET) def foo(application, message):     body = Take five!     return render(body)Tuesday, July 17, 12
  9. 9. Brubeck: routing Went with the usual style class NameHandler(WebMessageHandler):     def get(self, name):         ... def name_handler(application, message, name):     ... urls = [(r^/class/(w+)$, NameHandler),         (r^/fun/(?Pw+)$, name_handler)] • https://github.com/j2labs/brubeck/blob/master/demos/demo_urlargs.pyTuesday, July 17, 12
  10. 10. Brubeck: templates Supports: Jinja2, Mako,Tornado or Mustache from brubeck.templating import Jinja2Rendering class DemoHandler(WebMessageHandler, Jinja2Rendering):     def get(self):         context = {             name: J2 D2,         }         return self.render_template(success.html, **context) • https://github.com/j2labs/brubeck/blob/master/demos/demo_jinja2.py • https://github.com/j2labs/brubeck/blob/master/demos/demo_mustache.pyTuesday, July 17, 12
  11. 11. Brubeck: auth (pt 1) • Simple example, using `web_authenticated` decorator: class DemoHandler(..., UserHandlingMixin): @web_authenticated def get(self):         context = {             name: self.current_user.username,         }         return self.render_template(some.html, **context) • Also supports secure cookies • Routes users to login template • https://github.com/j2labs/brubeck/blob/master/demos/demo_login.pyTuesday, July 17, 12
  12. 12. Brubeck: auth (pt 2) class BaseHandler(..., UserHandlingMixin):     def get_current_user(self):         user = None secret=self.application.cookie_secret         user_id = self.get_cookie(user_id, secret=secret)         if user_id:             return load_user(self.db_conn, username=user_id)         else:             username = self.get_argument(username)             password = self.get_argument(password)             if username:                 user = load_user(self.db_conn, username=username)         if not user or not user.check_password(password):             return         return userTuesday, July 17, 12
  13. 13. Brubeck: user • This what a Brubeck user model looks like class User(Document):     username = StringField(max_length=30, required=True)     password = StringField(max_length=128)     is_active = BooleanField(default=False)     last_login = LongField(default=curtime)     date_joined = LongField(default=curtime)     ... • Uses UUID for id field • Could use Mongo’s ObjectID if you prefer that • https://github.com/j2labs/brubeck/blob/master/brubeck/models.pyTuesday, July 17, 12
  14. 14. Brubeck: data validation • Validation is easy >>> from brubeck.models import User >>> u = User(username=jd, is_active=True) >>> u.set_password(foo) >>> u.validate() >>> u.username = True >>> u.validate() Traceback (most recent call last): ... dictshield.base.ShieldException: Invalid value - username:TrueTuesday, July 17, 12
  15. 15. Databaseless modeling • This what a Brubeck user looks like as Python >>> user_instance.to_python() {     _types: [User],     _cls: User,     username: ujd,     is_active: False,     last_login: 1311718487532L,     password: ubcrypt|||salt|||hash,     date_joined: 1311718487532L }Tuesday, July 17, 12
  16. 16. Databaseless modeling • Persistence details are up to you # Mongo >>> db.users.save(u.to_python()) # Riak >>> user = bucket.new(user_key, data=u.to_python()) >>> user.store() # Memcached >>> mc["user_key"] = u.to_json()Tuesday, July 17, 12
  17. 17. Brubeck: autoapi Automatic REST APIs from data models (!!) (Ben Beecher++) • Define a DictShield document (our model) • Define a QuerySet - Implements persistence • Dictionary based queryset is provided • Redis, Mongo and MySQL on the way • Subclass AutoAPIBase • Attach your model as `model` • Attach your queryset as `queries` • Register API for model in a Brubeck instanceTuesday, July 17, 12
  18. 18. Brubeck: autoapi A Todo API # Define Todo model class Todo(Document):     completed = BooleanField(default=False)     deleted = BooleanField(default=False)     archived = BooleanField(default=False)     title = StringField(required=True)     ... # Add fields to handler class TodosAPI(AutoAPIBase):     queries = DictQueryset(db_conn={})     model = Todo # Register with Brubeck instance app.register_api(TodosAPI)Tuesday, July 17, 12
  19. 19. Brubeck: autoapi $ curl -f -X POST -H "content-type: application/json" -d {"text": "Watch more bsg", "order": 1} http://localhost:6767/todo/ {     "_cls": "Todo",     "_id": "111b4bb7-55f5-441b-ba25-c7a4fd99442c",     "_types": [         "Todo"     ],     "done": false,     "order": 1,     "text": "Watch more bsg" }Tuesday, July 17, 12
  20. 20. Todos: backbone.js AutoAPI works great with backbone.js ( https://github.com/j2labs/todos )Tuesday, July 17, 12
  21. 21. bpm (brubeck project manager) $ bpm create project -n demo $ cd demo $ bpm create env $ bpm create env # Web Server ... Choose one (m2 wsgi): m2 # Concurrency Choose one (gevent eventlet): gevent # Templating Choose one or more (jinja2 mako tornado mustache none): jinja2 ... (demo) $Tuesday, July 17, 12
  22. 22. Questions ?? Brubeck: http://brubeck.io Code: https://github.com/j2labs/brubeck Mongrel2: http://mongrel.org DictShield: https://github.com/j2labs/dictshield Gevent: http://gevent.org Eventlet: http://eventlet.netTuesday, July 17, 12
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×