Brubeck: The Lightning Talk

2,732 views
2,615 views

Published on

A bird's eye view of Brubeck.

Please see http://brubeck.io for more information.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,732
On SlideShare
0
From Embeds
0
Number of Embeds
375
Actions
Shares
0
Downloads
19
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Brubeck: The Lightning Talk

  1. 2. What is Brubeck? <ul><li>A Mongrel2 Handler </li></ul><ul><li>A Web Framework </li></ul><ul><ul><li>Influenced by Flask, Tornado and Django </li></ul></ul><ul><li>A pipeline of coroutines </li></ul><ul><li>Some coded opinions and a few libraries </li></ul><ul><ul><li>Backend platform agnostic </li></ul></ul><ul><ul><li>Database agnostic </li></ul></ul><ul><ul><li>No spaghetti… </li></ul></ul><ul><ul><ul><li>implicit context switching </li></ul></ul></ul>
  2. 3. What is a “Mongrel2”? <ul><li>Mongrel2 is an asynchronous web server </li></ul><ul><li>Delegates handling to external handlers </li></ul><ul><ul><li>We build that part anyway </li></ul></ul><ul><ul><li>Communicates via 2 ZeroMQ ( zmq ) sockets </li></ul></ul><ul><ul><ul><li>Significantly less overhead than HTTP </li></ul></ul></ul><ul><li>Language agnostic </li></ul><ul><ul><li>Simple messaging via JSON or tnetstrings </li></ul></ul><ul><ul><li>Zmq is language agnostic, thus so is Mongrel2 </li></ul></ul>
  3. 4. Mongrel2 + A Handler
  4. 5. What is a Mongrel2 Handler? <ul><li>Processes the Mongrel2 messages </li></ul><ul><ul><li>Essentially, a Zed specific WSGI </li></ul></ul><ul><ul><ul><li>There has been some dissent: Y U NO USE WSGI? </li></ul></ul></ul><ul><li>Responds in HTTP format </li></ul><ul><li>A language opinion </li></ul><ul><ul><li>Remember, ZeroMQ sockets are language agnostic </li></ul></ul><ul><ul><li>Zed chose Lua </li></ul></ul><ul><ul><li>I liked his model, but wanted to use Python. </li></ul></ul><ul><ul><ul><li>I like building stuff! </li></ul></ul></ul><ul><ul><li>Lots of languages now supported </li></ul></ul>
  5. 6. Mongrel2 + Brubeck(s)
  6. 7. <ul><li>class DemoHandler(WebMessageHandler): </li></ul><ul><li>def get(self): </li></ul><ul><li>self.set_body('Take five!') </li></ul><ul><li>return self.render() </li></ul><ul><li>urls = [('^/brubeck', DemoHandler)] </li></ul>
  7. 8. <ul><li>class DemoHandler(WebMessageHandler): </li></ul><ul><li>def get(self): </li></ul><ul><li>self.set_body(’Take five!') </li></ul><ul><li>return self.render() </li></ul><ul><li>urls = [( ' ^/brubeck ' , DemoHandler)] </li></ul><ul><li>OR just use functions… </li></ul><ul><li>@app.add_route('^/brubeck', method='GET') </li></ul><ul><li>def foo(application, message): </li></ul><ul><li>body = 'Take five!' </li></ul><ul><li>return http_response(body, 200, 'OK', {}) </li></ul>
  8. 9. Brubeck: templates <ul><li>Jinja2 , Mako or Tornado templates are supported </li></ul><ul><li>class DemoHandler(..., Jinja2Rendering ): </li></ul><ul><li>def get(self): </li></ul><ul><li>name = self.get_argument('name') </li></ul><ul><li>context = { </li></ul><ul><li>'name' : name, </li></ul><ul><li>} </li></ul><ul><li>return self.render_template(’some.html', **context) </li></ul>
  9. 10. Brubeck: auth <ul><li>Simple example, using ` web_authenticated ` decorator: </li></ul><ul><li>class DemoHandler(..., UserHandlingMixin): </li></ul><ul><li>@web_authenticated </li></ul><ul><li>def get(self): </li></ul><ul><li>context = { </li></ul><ul><li>'name': self.current_user.username, </li></ul><ul><li>} </li></ul><ul><li>return self.render_template('some.html', **context) </li></ul><ul><li>Also supports secure cookies </li></ul><ul><li>Routes users to login template </li></ul><ul><li>https://github.com/j2labs/brubeck/blob/master/demos/demo_login.py </li></ul>
  10. 11. Pipeline of Coroutines <ul><li>Each request creates 3 </li></ul><ul><ul><li>Preprocessing </li></ul></ul><ul><ul><ul><li>Currently just maps URL to handler </li></ul></ul></ul><ul><ul><li>Request Handling </li></ul></ul><ul><ul><ul><li>The part you write </li></ul></ul></ul><ul><ul><li>Postprocessing </li></ul></ul><ul><ul><ul><li>Just sends the HTTP response for now </li></ul></ul></ul>
  11. 12. Database less Modeling <ul><li>This is a Brubeck User , built with DictShield </li></ul><ul><li>class User(Document): </li></ul><ul><li>username = StringField(max_length=30, required=True) </li></ul><ul><li>password = StringField(max_length=128) </li></ul><ul><li>is_active = BooleanField(default=False) </li></ul><ul><li>last_login = LongField(default=curtime) </li></ul><ul><li>date_joined = LongField(default=curtime) </li></ul><ul><li>... </li></ul>
  12. 13. Database less Modeling <ul><li>Validation is easy </li></ul><ul><li>>>> u = User(username='jd', is_active=True) </li></ul><ul><li>>>> u.set_password('foo') </li></ul><ul><li>>>> u.validate() # no exception </li></ul><ul><li>>>> u.username = True # this line makes no sense </li></ul><ul><li>>>> u.validate() # thus, validation fails </li></ul><ul><li>dictshield.base.DictPunch: username(True): Invalid value </li></ul><ul><li>>>> </li></ul>
  13. 14. Database less Modeling <ul><li>Call ` to_python() ` for a dict </li></ul><ul><li>>>> user_instance.to_python() </li></ul><ul><li>{ </li></ul><ul><li>'_types': ['User'], </li></ul><ul><li>'_cls': 'User', </li></ul><ul><li>'username': u'jd', </li></ul><ul><li>'is_active': False, </li></ul><ul><li>'last_login': 1311718487532L, </li></ul><ul><li>'password': u'bcrypt|||salt|||hash', </li></ul><ul><li>'date_joined': 1311718487532L </li></ul><ul><li>} </li></ul>
  14. 15. Database less Modeling <ul><li>The persistence details are up to you </li></ul><ul><li># Mongo </li></ul><ul><li>>>> db.users.save(u.to_python()) </li></ul><ul><li># Riak </li></ul><ul><li>>>> user = bucket.new('user_key', data=u.to_python()) </li></ul><ul><li>>>> user.store() </li></ul><ul><li># Memcached </li></ul><ul><li>>>> mc[&quot;user_key&quot;] = u.to_json() </li></ul>
  15. 16. Questions ?? <ul><li>Brubeck: http://brubeck.io </li></ul><ul><li>Code: http://github.com/j2labs/brubeck </li></ul><ul><li>Mongrel2: http://mongrel2.org </li></ul><ul><li>DictShield: http://github.com/j2labs/dictshield </li></ul><ul><li>Concurrency: http://eventlet.net </li></ul><ul><li>or: http://gevent.org </li></ul>

×