SlideShare ist ein Scribd-Unternehmen logo
1 von 52
Downloaden Sie, um offline zu lesen
CouchDB
for Web Applications
       Jason Davies
     www.jasondavies.com
About Me

• Director, Jason Davies Ltd
• Apache CouchDB contributor
• Python, Django, JavaScript, jQuery
• Cambridge University (ML!)
CouchApps
• Pure CouchDB applications
• Standalone: hosted entirely on CouchDB
  “stack”, usually one app per _design doc
• Single step deployment via replication
• Enforces “scalable thinking”
• P2P Web
?!!
`couchapp`
• Scripts written in Python to make
  developing pure CouchDB applications
  easier
• sudo easy_install couchapp
• couchapp generate relax && cd relax
• couchapp push http://127.0.0.1:5984/mydb
Directory Structure
Resulting Design Doc
_list
• Arbitrary JS transformation for views
• http://127.0.0.1:5984/mydb/_design/app/
  _list/myview?startkey=...&endkey=...
• JSON -> HTML, JSON -> XML, ...
• E4X nice for XML generation
• Iteratively call getRow() and use send(...)
_show

• Arbitrary transformation for documents
• http://127.0.0.1:5984/mydb/_design/app/
  _show/mydoc
• function (doc, req) { return “foo”; }
JavaScript Templating
•   EmbeddedJS (EJS)

    •   <% /* execute arbitrary JS */ %>

    •   <%= /* execute and include result */ %>

    •   new EJS({ text: mytemplate }).render(doc);

•   John Resig’s Micro-Templating

    •   new template(mytemplate)(doc);

    •   Doesn’t preserve whitespace or LaTeX
        backslashes
Push Helper Macros
• Simple macros to facilitate code re-use
• Insert code directly
 • // !code path/to/code.js
• Encode file as JSON: path/to/test.html
 • // !json path.to.test
 • // !json _attachments/test.html
Experiments!




http://www.flickr.com/photos/seanstayte/378461237/
CouchDB on Wheels




Casual Lofa: the World’s fastest furniture
               (87 m.p.h.)
www.elyservice.co.uk

• “Just a very ordinary-looking garage Web
  site” @jchris
• Originally developed using Django
• 5 static pages
• 1 contact form that sends e-mail
Static Pages

• Very easy to do
• Simple JS function in shows/pages.js
• Takes doc.title, doc.content and renders
  template using EJS
Example shows/page.js
Pretty URLs

• / -> /elyservice/_design/elyservice/_show/
  pages:home
• /about/ -> /elyservice/_design/elyservice/
  _show/pages:about
• We need a flexible URL router
Nginx
• Use Nginx as a reverse-proxy
• Simple rewrite rules using regular
  expressions
• Works well
• Config is a bit unwieldy
• Have to edit config file and reload Nginx
  process every time I change a route
server {
    listen 89.145.97.172:80;
    server_name www.elyservice.co.uk;
    set $projectname elyservice;

    location / {
        if ($request_method !~ ^(GET|HEAD)$) {
            return 444;
        }

        proxy_pass http://127.0.0.1:5984/elyservice;
        proxy_redirect default;
        proxy_set_header X-Orig-Host '$host:$server_port';

        rewrite ^/media/(.+)$ /$projectname/_design/elyservice/$1 break;
        rewrite ^/$ '/$projectname/_design/elyservice/_show/pages' break;
        rewrite ^/(.*)/$ '/$projectname/_design/elyservice/_show/pages/pages:$1' break;

        return 404;
    }

    location /contact/ {
        if ($request_method !~ ^(GET|HEAD|POST)$) {
            return 444;
        }

        proxy_pass http://127.0.0.1:5984/elyservice;
        proxy_redirect default;
        proxy_set_header X-Orig-Host '$host:$server_port';

        if ($request_method = POST) {
            rewrite ^/contact/$ /$projectname/ break;
        }
        rewrite ^/contact/$ '/$projectname/_design/elyservice/_show/contact' break;

        return 404;
    }
}
_rewrite
• URL routing for pure CouchDB
  applications
• Still in experimentation phase
• Simple experiment using Webmachine-style
  syntax encoded as JSON in _design doc
 • Atoms are encoded as “<atom>”, since
    “<“ and “>” are invalid URL characters
rewrites.json
[
    {
           "match": ["media", "<*>"],
           "rewrite": ["_design", "bethabracha", "<*>"]
    }, {
       "match": [“products”, “<id>”],
       "rewrite": ["_design", "bethabracha", "_show",
"<id>"]
  }, {
       "match": ["products", "<id>", "media", "<*>"],
       "rewrite": ["<id>", "<*>"]
  }
]
Code
• http://github.com/jasondavies/couchdb/tree/
  rewrite
• Supports Webmachine-style routes for URL
  rewriting
• Needs support for rewriting query string
  (or equivalent)
  • e.g. /blog/tags/foo/ -> .../_view/by_tag?
Sending E-Mail

• No native SMTP support in CouchDB (yet)
• Never give up! Implement simple message
  spooler in CouchDB
 • Use an update_notification process
    (python send_emails.py)
 • Or run this as a cron job on N slaves
Code
http://github.com/jasondavies/couchdb-contact-
                      form
Security & Validation I
Configure Nginx to reject non-GET/HEAD
requests:




Non-standard error code 444 causes Nginx
to drop connection
 • Use separate Nginx config block to
   allow POSTs to /contact/
Security & Validation II
     validate_doc_update.js
IRC Experiments

• CouchDB good for storing large quantities
  of data for analysis
• Simple logger for #couchdb IRC chatroom
• Create pretty graphs
rakieandjake.com
• Originally written using Django
• Converted to CouchApp for fun
• Auto-thumbnailing of wedding photos
 • Similar to spooler, a special view lists
    thumbnail sizes that still need to be
    generated
  • Python script pushes thumbnails into
    docs as attachments
Secure Cookie Authentication
• Reasonable performance/simplicity of
  JavaScript implementation
• Mutual authentication
• Resistance to off-line dictionary attacks
  based on passive eavesdropping
• Passwords stored in a form that is not
  plaintext-equivalent
• Limited resistance to replay attacks
Tamper-Proof Cookies


Timestamp + signature => limited forward-security
        (outside of timestamp window)
Secure Remote Password Protocol (SRP)

• Zero-Knowledge Password Proof
• Simple to implement in Erlang using BigInt
  and crypto libraries
• JavaScript too slow: over 5s for 1024 bits
• Vulnerable to active injection attacks
• There are simpler protocols that can be
  used to give equivalent security
• Just add SSL for protection from active
  attacks (or lobby for TLS-SRP/J-PAKE!)
couch_httpd_auth I

• Drop-in replacement for
  default_authentication_handler
 • Populates user_ctx (req.userCtx)
 • Falls back to HTTP Basic for replication
couch_httpd_auth II

• http://github.com/jasondavies/couchdb/tree/
  cookie-auth
• Uses simple plaintext authentication for
  now, will add pluggable authentication
  mechanisms
• Due to be merged into trunk “soon”
• Used in http://nymphormation.org
Bet Ha Bracha
• Mum’s Web site
• Fun experiment: E-commerce on pure
  CouchDB!
• Product catalogue
• Google Checkout integration
• Google Base Atom feed
• Again, originally written in Django
Shopping Cart

• Store shopping cart in cookie (4kb max)
 • Requires no persistent server-side
    session state, good for clusters!
 • Obvious size limitation, for a larger site
    we would probably store the cart in
    CouchDB keyed by a session cookie
The Endless Quest for
       Purity
• Google Checkout integration currently
  needs _external + Python script, since the
  callback uses XML
• For 100% purity we need _update handler
  to transform XML -> JSON
_update
• Analagous to _show
• Precise semantics still being worked on
• e.g. function (doc, req) { /* mutate doc */
  return doc; }
• Watch this space: http://github.com/
  jasondavies/couchdb/tree/update
Joe’s Blog
• Simple blog experiment from Joe
  Armstrong’s lightning talk
• Uses contentEditable
• Original version used simple Erlang server
  to save versions of blog post
• Super-easy to replace with CouchDB!
CouchDB “Revisions”

• These are used for optimistic concurrency
  control
• Not for implementing a VCS!
• To store a revision history we can simply
  create a new doc for each revision and
  never change it
Other Wishlist Items


• View intersections and unions
 • Load HTML page in single request e.g.
    the categories/tags list in the sidebar
Thank you for listening!


  www.jasondavies.com

Weitere ähnliche Inhalte

Was ist angesagt?

JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPressTaylor Lovett
 
Moxi - Memcached Proxy
Moxi - Memcached ProxyMoxi - Memcached Proxy
Moxi - Memcached ProxyNorthScale
 
Internals - Exploring the webOS Browser and JavaScript
Internals - Exploring the webOS Browser and JavaScriptInternals - Exploring the webOS Browser and JavaScript
Internals - Exploring the webOS Browser and JavaScriptfpatton
 
Ruby MVC from scratch with Rack
Ruby MVC from scratch with RackRuby MVC from scratch with Rack
Ruby MVC from scratch with RackDonSchado
 
Here Be Dragons - Debugging WordPress
Here Be Dragons - Debugging WordPressHere Be Dragons - Debugging WordPress
Here Be Dragons - Debugging WordPressRami Sayar
 
Create Rest API in Nodejs
Create Rest API in Nodejs Create Rest API in Nodejs
Create Rest API in Nodejs Irfan Maulana
 
WebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkWebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkFabio Tiriticco
 
Profiling JavaScript Performance
Profiling JavaScript PerformanceProfiling JavaScript Performance
Profiling JavaScript PerformanceFITC
 
HyperLight Websites
HyperLight WebsitesHyperLight Websites
HyperLight WebsitesFITC
 
EasyEngine - Command-Line tool to manage WordPress Sites on Nginx
EasyEngine - Command-Line tool to manage WordPress Sites on NginxEasyEngine - Command-Line tool to manage WordPress Sites on Nginx
EasyEngine - Command-Line tool to manage WordPress Sites on NginxrtCamp
 
HBaseConEast2016: Practical Kerberos with Apache HBase
HBaseConEast2016: Practical Kerberos with Apache HBaseHBaseConEast2016: Practical Kerberos with Apache HBase
HBaseConEast2016: Practical Kerberos with Apache HBaseMichael Stack
 
Massively Scaled High Performance Web Services with PHP
Massively Scaled High Performance Web Services with PHPMassively Scaled High Performance Web Services with PHP
Massively Scaled High Performance Web Services with PHPDemin Yin
 
Developing Java based microservices ready for the world of containers
Developing Java based microservices ready for the world of containersDeveloping Java based microservices ready for the world of containers
Developing Java based microservices ready for the world of containersClaus Ibsen
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsMongoDB
 
Umleitung: a tiny mochiweb/CouchDB app
Umleitung: a tiny mochiweb/CouchDB appUmleitung: a tiny mochiweb/CouchDB app
Umleitung: a tiny mochiweb/CouchDB appLenz Gschwendtner
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsRichard Lee
 

Was ist angesagt? (20)

10 Data caching
10 Data caching10 Data caching
10 Data caching
 
Express JS
Express JSExpress JS
Express JS
 
Node.js Express Framework
Node.js Express FrameworkNode.js Express Framework
Node.js Express Framework
 
JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPress
 
Moxi - Memcached Proxy
Moxi - Memcached ProxyMoxi - Memcached Proxy
Moxi - Memcached Proxy
 
Internals - Exploring the webOS Browser and JavaScript
Internals - Exploring the webOS Browser and JavaScriptInternals - Exploring the webOS Browser and JavaScript
Internals - Exploring the webOS Browser and JavaScript
 
Ruby MVC from scratch with Rack
Ruby MVC from scratch with RackRuby MVC from scratch with Rack
Ruby MVC from scratch with Rack
 
Here Be Dragons - Debugging WordPress
Here Be Dragons - Debugging WordPressHere Be Dragons - Debugging WordPress
Here Be Dragons - Debugging WordPress
 
Create Rest API in Nodejs
Create Rest API in Nodejs Create Rest API in Nodejs
Create Rest API in Nodejs
 
WebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkWebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! Framework
 
Profiling JavaScript Performance
Profiling JavaScript PerformanceProfiling JavaScript Performance
Profiling JavaScript Performance
 
HyperLight Websites
HyperLight WebsitesHyperLight Websites
HyperLight Websites
 
EasyEngine - Command-Line tool to manage WordPress Sites on Nginx
EasyEngine - Command-Line tool to manage WordPress Sites on NginxEasyEngine - Command-Line tool to manage WordPress Sites on Nginx
EasyEngine - Command-Line tool to manage WordPress Sites on Nginx
 
HBaseConEast2016: Practical Kerberos with Apache HBase
HBaseConEast2016: Practical Kerberos with Apache HBaseHBaseConEast2016: Practical Kerberos with Apache HBase
HBaseConEast2016: Practical Kerberos with Apache HBase
 
Web sockets in Java
Web sockets in JavaWeb sockets in Java
Web sockets in Java
 
Massively Scaled High Performance Web Services with PHP
Massively Scaled High Performance Web Services with PHPMassively Scaled High Performance Web Services with PHP
Massively Scaled High Performance Web Services with PHP
 
Developing Java based microservices ready for the world of containers
Developing Java based microservices ready for the world of containersDeveloping Java based microservices ready for the world of containers
Developing Java based microservices ready for the world of containers
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
 
Umleitung: a tiny mochiweb/CouchDB app
Umleitung: a tiny mochiweb/CouchDB appUmleitung: a tiny mochiweb/CouchDB app
Umleitung: a tiny mochiweb/CouchDB app
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 

Ähnlich wie CouchDB for Web Applications - Erlang Factory London 2009

Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsasync_io
 
Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Ran Mizrahi
 
Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Ran Mizrahi
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.jsorkaplan
 
Introduction to Infrastructure as Code & Automation / Introduction to Chef
Introduction to Infrastructure as Code & Automation / Introduction to ChefIntroduction to Infrastructure as Code & Automation / Introduction to Chef
Introduction to Infrastructure as Code & Automation / Introduction to ChefNathen Harvey
 
Groovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationGroovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationStuart (Pid) Williams
 
JavaScript Service Worker Design Patterns for Better User Experience
JavaScript Service Worker Design Patterns for Better User ExperienceJavaScript Service Worker Design Patterns for Better User Experience
JavaScript Service Worker Design Patterns for Better User Experiencereeder29
 
Improve WordPress performance with caching and deferred execution of code
Improve WordPress performance with caching and deferred execution of codeImprove WordPress performance with caching and deferred execution of code
Improve WordPress performance with caching and deferred execution of codeDanilo Ercoli
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to TornadoGavin Roy
 
1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb1.6 米嘉 gobuildweb
1.6 米嘉 gobuildwebLeo Zhou
 
Best Practices for WordPress
Best Practices for WordPressBest Practices for WordPress
Best Practices for WordPressTaylor Lovett
 
Stress Free Deployment - Confoo 2011
Stress Free Deployment  - Confoo 2011Stress Free Deployment  - Confoo 2011
Stress Free Deployment - Confoo 2011Bachkoutou Toutou
 
Best Practices for Building WordPress Applications
Best Practices for Building WordPress ApplicationsBest Practices for Building WordPress Applications
Best Practices for Building WordPress ApplicationsTaylor Lovett
 
Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011Timothy Fisher
 
Nginx A High Performance Load Balancer, Web Server & Reverse Proxy
Nginx A High Performance Load Balancer, Web Server & Reverse ProxyNginx A High Performance Load Balancer, Web Server & Reverse Proxy
Nginx A High Performance Load Balancer, Web Server & Reverse ProxyAmit Aggarwal
 
Web Development using Ruby on Rails
Web Development using Ruby on RailsWeb Development using Ruby on Rails
Web Development using Ruby on RailsAvi Kedar
 

Ähnlich wie CouchDB for Web Applications - Erlang Factory London 2009 (20)

Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.js
 
Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)
 
Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Introduction to Infrastructure as Code & Automation / Introduction to Chef
Introduction to Infrastructure as Code & Automation / Introduction to ChefIntroduction to Infrastructure as Code & Automation / Introduction to Chef
Introduction to Infrastructure as Code & Automation / Introduction to Chef
 
Groovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationGroovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentation
 
JavaScript Service Worker Design Patterns for Better User Experience
JavaScript Service Worker Design Patterns for Better User ExperienceJavaScript Service Worker Design Patterns for Better User Experience
JavaScript Service Worker Design Patterns for Better User Experience
 
Improve WordPress performance with caching and deferred execution of code
Improve WordPress performance with caching and deferred execution of codeImprove WordPress performance with caching and deferred execution of code
Improve WordPress performance with caching and deferred execution of code
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
 
Rack
RackRack
Rack
 
1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb
 
Best Practices for WordPress
Best Practices for WordPressBest Practices for WordPress
Best Practices for WordPress
 
Stress Free Deployment - Confoo 2011
Stress Free Deployment  - Confoo 2011Stress Free Deployment  - Confoo 2011
Stress Free Deployment - Confoo 2011
 
Best Practices for Building WordPress Applications
Best Practices for Building WordPress ApplicationsBest Practices for Building WordPress Applications
Best Practices for Building WordPress Applications
 
Nodejs web,db,hosting
Nodejs web,db,hostingNodejs web,db,hosting
Nodejs web,db,hosting
 
Node.js on Azure
Node.js on AzureNode.js on Azure
Node.js on Azure
 
Wider than rails
Wider than railsWider than rails
Wider than rails
 
Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011
 
Nginx A High Performance Load Balancer, Web Server & Reverse Proxy
Nginx A High Performance Load Balancer, Web Server & Reverse ProxyNginx A High Performance Load Balancer, Web Server & Reverse Proxy
Nginx A High Performance Load Balancer, Web Server & Reverse Proxy
 
Web Development using Ruby on Rails
Web Development using Ruby on RailsWeb Development using Ruby on Rails
Web Development using Ruby on Rails
 

Kürzlich hochgeladen

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Kürzlich hochgeladen (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

CouchDB for Web Applications - Erlang Factory London 2009

  • 1. CouchDB for Web Applications Jason Davies www.jasondavies.com
  • 2. About Me • Director, Jason Davies Ltd • Apache CouchDB contributor • Python, Django, JavaScript, jQuery • Cambridge University (ML!)
  • 3. CouchApps • Pure CouchDB applications • Standalone: hosted entirely on CouchDB “stack”, usually one app per _design doc • Single step deployment via replication • Enforces “scalable thinking” • P2P Web
  • 4. ?!!
  • 5. `couchapp` • Scripts written in Python to make developing pure CouchDB applications easier • sudo easy_install couchapp • couchapp generate relax && cd relax • couchapp push http://127.0.0.1:5984/mydb
  • 8. _list • Arbitrary JS transformation for views • http://127.0.0.1:5984/mydb/_design/app/ _list/myview?startkey=...&endkey=... • JSON -> HTML, JSON -> XML, ... • E4X nice for XML generation • Iteratively call getRow() and use send(...)
  • 9. _show • Arbitrary transformation for documents • http://127.0.0.1:5984/mydb/_design/app/ _show/mydoc • function (doc, req) { return “foo”; }
  • 10. JavaScript Templating • EmbeddedJS (EJS) • <% /* execute arbitrary JS */ %> • <%= /* execute and include result */ %> • new EJS({ text: mytemplate }).render(doc); • John Resig’s Micro-Templating • new template(mytemplate)(doc); • Doesn’t preserve whitespace or LaTeX backslashes
  • 11. Push Helper Macros • Simple macros to facilitate code re-use • Insert code directly • // !code path/to/code.js • Encode file as JSON: path/to/test.html • // !json path.to.test • // !json _attachments/test.html
  • 13. CouchDB on Wheels Casual Lofa: the World’s fastest furniture (87 m.p.h.)
  • 14.
  • 15. www.elyservice.co.uk • “Just a very ordinary-looking garage Web site” @jchris • Originally developed using Django • 5 static pages • 1 contact form that sends e-mail
  • 16.
  • 17. Static Pages • Very easy to do • Simple JS function in shows/pages.js • Takes doc.title, doc.content and renders template using EJS
  • 19. Pretty URLs • / -> /elyservice/_design/elyservice/_show/ pages:home • /about/ -> /elyservice/_design/elyservice/ _show/pages:about • We need a flexible URL router
  • 20. Nginx • Use Nginx as a reverse-proxy • Simple rewrite rules using regular expressions • Works well • Config is a bit unwieldy • Have to edit config file and reload Nginx process every time I change a route
  • 21. server { listen 89.145.97.172:80; server_name www.elyservice.co.uk; set $projectname elyservice; location / { if ($request_method !~ ^(GET|HEAD)$) { return 444; } proxy_pass http://127.0.0.1:5984/elyservice; proxy_redirect default; proxy_set_header X-Orig-Host '$host:$server_port'; rewrite ^/media/(.+)$ /$projectname/_design/elyservice/$1 break; rewrite ^/$ '/$projectname/_design/elyservice/_show/pages' break; rewrite ^/(.*)/$ '/$projectname/_design/elyservice/_show/pages/pages:$1' break; return 404; } location /contact/ { if ($request_method !~ ^(GET|HEAD|POST)$) { return 444; } proxy_pass http://127.0.0.1:5984/elyservice; proxy_redirect default; proxy_set_header X-Orig-Host '$host:$server_port'; if ($request_method = POST) { rewrite ^/contact/$ /$projectname/ break; } rewrite ^/contact/$ '/$projectname/_design/elyservice/_show/contact' break; return 404; } }
  • 22. _rewrite • URL routing for pure CouchDB applications • Still in experimentation phase • Simple experiment using Webmachine-style syntax encoded as JSON in _design doc • Atoms are encoded as “<atom>”, since “<“ and “>” are invalid URL characters
  • 23. rewrites.json [ { "match": ["media", "<*>"], "rewrite": ["_design", "bethabracha", "<*>"] }, { "match": [“products”, “<id>”], "rewrite": ["_design", "bethabracha", "_show", "<id>"] }, { "match": ["products", "<id>", "media", "<*>"], "rewrite": ["<id>", "<*>"] } ]
  • 24. Code • http://github.com/jasondavies/couchdb/tree/ rewrite • Supports Webmachine-style routes for URL rewriting • Needs support for rewriting query string (or equivalent) • e.g. /blog/tags/foo/ -> .../_view/by_tag?
  • 25. Sending E-Mail • No native SMTP support in CouchDB (yet) • Never give up! Implement simple message spooler in CouchDB • Use an update_notification process (python send_emails.py) • Or run this as a cron job on N slaves
  • 26.
  • 27.
  • 29. Security & Validation I Configure Nginx to reject non-GET/HEAD requests: Non-standard error code 444 causes Nginx to drop connection • Use separate Nginx config block to allow POSTs to /contact/
  • 30. Security & Validation II validate_doc_update.js
  • 31. IRC Experiments • CouchDB good for storing large quantities of data for analysis • Simple logger for #couchdb IRC chatroom • Create pretty graphs
  • 32.
  • 33. rakieandjake.com • Originally written using Django • Converted to CouchApp for fun • Auto-thumbnailing of wedding photos • Similar to spooler, a special view lists thumbnail sizes that still need to be generated • Python script pushes thumbnails into docs as attachments
  • 34.
  • 35.
  • 36.
  • 37. Secure Cookie Authentication • Reasonable performance/simplicity of JavaScript implementation • Mutual authentication • Resistance to off-line dictionary attacks based on passive eavesdropping • Passwords stored in a form that is not plaintext-equivalent • Limited resistance to replay attacks
  • 38.
  • 39. Tamper-Proof Cookies Timestamp + signature => limited forward-security (outside of timestamp window)
  • 40. Secure Remote Password Protocol (SRP) • Zero-Knowledge Password Proof • Simple to implement in Erlang using BigInt and crypto libraries • JavaScript too slow: over 5s for 1024 bits • Vulnerable to active injection attacks • There are simpler protocols that can be used to give equivalent security • Just add SSL for protection from active attacks (or lobby for TLS-SRP/J-PAKE!)
  • 41. couch_httpd_auth I • Drop-in replacement for default_authentication_handler • Populates user_ctx (req.userCtx) • Falls back to HTTP Basic for replication
  • 42. couch_httpd_auth II • http://github.com/jasondavies/couchdb/tree/ cookie-auth • Uses simple plaintext authentication for now, will add pluggable authentication mechanisms • Due to be merged into trunk “soon” • Used in http://nymphormation.org
  • 43.
  • 44. Bet Ha Bracha • Mum’s Web site • Fun experiment: E-commerce on pure CouchDB! • Product catalogue • Google Checkout integration • Google Base Atom feed • Again, originally written in Django
  • 45.
  • 46. Shopping Cart • Store shopping cart in cookie (4kb max) • Requires no persistent server-side session state, good for clusters! • Obvious size limitation, for a larger site we would probably store the cart in CouchDB keyed by a session cookie
  • 47. The Endless Quest for Purity • Google Checkout integration currently needs _external + Python script, since the callback uses XML • For 100% purity we need _update handler to transform XML -> JSON
  • 48. _update • Analagous to _show • Precise semantics still being worked on • e.g. function (doc, req) { /* mutate doc */ return doc; } • Watch this space: http://github.com/ jasondavies/couchdb/tree/update
  • 49. Joe’s Blog • Simple blog experiment from Joe Armstrong’s lightning talk • Uses contentEditable • Original version used simple Erlang server to save versions of blog post • Super-easy to replace with CouchDB!
  • 50. CouchDB “Revisions” • These are used for optimistic concurrency control • Not for implementing a VCS! • To store a revision history we can simply create a new doc for each revision and never change it
  • 51. Other Wishlist Items • View intersections and unions • Load HTML page in single request e.g. the categories/tags list in the sidebar
  • 52. Thank you for listening! www.jasondavies.com