112
Building a desktop app with HTTP::Engine, SQLite & jQuery Tatsuhiko Miyagawa YAPC::NA 2009 Pittsburgh Wednesday, June 24, 2009

Building a desktop app with HTTP::Engine, SQLite and jQuery

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Building a desktop app with HTTP::Engine, SQLite and jQuery

Building a desktop app with HTTP::Engine, SQLite & jQuery

Tatsuhiko MiyagawaYAPC::NA 2009 Pittsburgh

Wednesday, June 24, 2009

Page 2: Building a desktop app with HTTP::Engine, SQLite and jQuery

Tatsuhiko Miyagawa

Wednesday, June 24, 2009

Page 3: Building a desktop app with HTTP::Engine, SQLite and jQuery

Wednesday, June 24, 2009

Page 4: Building a desktop app with HTTP::Engine, SQLite and jQuery

Software Engineer, TypePad

Wednesday, June 24, 2009

Page 5: Building a desktop app with HTTP::Engine, SQLite and jQuery

cpan: MIYAGAWA

Wednesday, June 24, 2009

Page 6: Building a desktop app with HTTP::Engine, SQLite and jQuery

Acme-DateTime-Duration-Numeric Acme-Module-Authors Acme-Sneeze Acme-Sneeze-JP Apache-ACEProxy Apache-AntiSpam Apache-Clickable Apache-CustomKeywords Apache-DefaultCharset Apache-GuessCharset Apache-JavaScript-

DocumentWrite Apache-No404Proxy Apache-Profiler Apache-Session-CacheAny Apache-Session-Generate-ModUniqueId Apache-Session-Generate-ModUsertrack Apache-Session-PHP Apache-Session-Serialize-YAML Apache-Singleton Apache-StickyQuery Archive-Any-Create Attribute-Profiled Attribute-Protected Attribute-Unimplemented Bundle-Sledge CGI-

Untaint-email CPAN-Mini-Growl Catalyst-Plugin-Authentication-Credential-AOL Catalyst-Plugin-Authentication-Credential-OpenID Catalyst-Plugin-JSONRPC Catalyst-View-JSON Catalyst-View-Jemplate Class-DBI-AbstractSearch Class-DBI-

Extension Class-DBI-Pager Class-DBI-Replication Class-DBI-SQLite Class-DBI-View Class-Trigger Convert-Base32 Convert-DUDE Convert-RACE Data-YUID Date-Japanese-Era Date-Range-Birth DateTime-Span-Birthdate Device-KeyStroke-Mobile Dunce-time Email-Find Email-Valid-Loose Encode-DoubleEncodedUTF8 Encode-First Encode-JP-Mobile Encode-JavaScript-

UCS Encode-Punycode File-Find-Rule-Digest File-Spotlight Geo-Coder-Google HTML-AutoPagerize HTML-Entities-ImodePictogram HTML-RelExtor HTML-ResolveLink HTML-Selector-XPath HTML-XSSLint HTTP-MobileAgent HTTP-ProxyPAC HTTP-Server-Simple-Authen HTTP-Server-Simple-Bonjour IDNA-Punycode Inline-Basic Inline-TT JSON-Syck

Kwiki-Emoticon Kwiki-Export Kwiki-Footnote Kwiki-OpenSearch Kwiki-OpenSearch-Service Kwiki-TypeKey Kwiki-URLBL LWP-UserAgent-Keychain Lingua-JA-Hepburn-Passport Log-Dispatch-Config Log-Dispatch-DBI MSIE-MenuExt Mac-

Macbinary Mail-Address-MobileJp Mail-ListDetector-Detector-Fml Module-Install-Repository Net-DAAP-Server-AAC Net-IDN-Nameprep Net-IPAddr-Find Net-Twitter-OAuth Net-YahooMessenger NetAddr-IP-Find P2P-Transmission-Remote PHP-Session POE-Component-Client-AirTunes POE-Component-Client-Lingr POE-Component-YahooMessenger Path-

Class-URI Plagger RPC-XML-Parser-LibXML Template-Plugin-Clickable Template-Plugin-Comma Template-Plugin-FillInForm Template-Plugin-HTML-Template Template-Plugin-JavaScript Template-Plugin-MobileAgent Template-Plugin-ResolveLink

Template-Plugin-Shuffle Template-Provider-Encoding Term-Encoding Term-TtyRec Test-Synopsis Text-Emoticon Text-Emoticon-GoogleTalk Text-Emoticon-MSN Text-Emoticon-Yahoo Text-MessageFormat TheSchwartz-Simple Time-Duration-Parse Time-Duration-ja URI-Find-UTF8 URI-git URI-tag URI-urn-uuid Video-Subtitle-SRT WWW-Baseball-NPB WWW-Blog-Metadata-MobileLinkDiscovery WWW-Blog-Metadata-OpenID WWW-Blog-Metadata-OpenSearch WWW-Cache-Google

WWW-Mechanize-AutoPager WWW-Mechanize-DecodedContent WWW-NicoVideo-Download WWW-OpenSearch WWW-Shorten-RevCanonical WWW-Shorten-Simple Web-Scraper Web-oEmbed WebService-Bloglines WebService-ChangesXml WebService-Google-Suggest WebService-Lingr XML-Atom XML-Atom-Lifeblog XML-Atom-Stream XML-

Liberal XML-OPML-LibXML abbreviation autobox-DateTime-Duration capitalization plagger

Wednesday, June 24, 2009

Page 7: Building a desktop app with HTTP::Engine, SQLite and jQuery

twitter.com/miyagawa(Slides will be linked. Follow me!)

Wednesday, June 24, 2009

Page 8: Building a desktop app with HTTP::Engine, SQLite and jQuery

Remedie

Wednesday, June 24, 2009

Page 9: Building a desktop app with HTTP::Engine, SQLite and jQuery

remediecode.orgSlides/Video in OSDC.TW

Wednesday, June 24, 2009

Page 10: Building a desktop app with HTTP::Engine, SQLite and jQuery

Building a desktop app with HTTP::Engine, SQLite & jQuery

Tatsuhiko MiyagawaYAPC::NA 2009 Pittsburgh

Wednesday, June 24, 2009

Page 11: Building a desktop app with HTTP::Engine, SQLite and jQuery

Desktop GUI apps

Wednesday, June 24, 2009

Page 12: Building a desktop app with HTTP::Engine, SQLite and jQuery

MFC/.NETVisual C++, VB

Wednesday, June 24, 2009

Page 13: Building a desktop app with HTTP::Engine, SQLite and jQuery

wxWidgets

Wednesday, June 24, 2009

Page 14: Building a desktop app with HTTP::Engine, SQLite and jQuery

Objective-CCocoa

(PyObjC/RubyCocoa)

Wednesday, June 24, 2009

Page 15: Building a desktop app with HTTP::Engine, SQLite and jQuery

Adobe AIR

Wednesday, June 24, 2009

Page 16: Building a desktop app with HTTP::Engine, SQLite and jQuery

I’m a Perl guywho knows JavaScript.

Wednesday, June 24, 2009

Page 17: Building a desktop app with HTTP::Engine, SQLite and jQuery

Web Developers!Wednesday, June 24, 2009

Page 18: Building a desktop app with HTTP::Engine, SQLite and jQuery

Web app!

Wednesday, June 24, 2009

Page 19: Building a desktop app with HTTP::Engine, SQLite and jQuery

“Web 2.0 iPhone App”Steve Jobs at WWDC 2007

Wednesday, June 24, 2009

Page 20: Building a desktop app with HTTP::Engine, SQLite and jQuery

Wednesday, June 24, 2009

Page 21: Building a desktop app with HTTP::Engine, SQLite and jQuery

PhoneGapWednesday, June 24, 2009

Page 22: Building a desktop app with HTTP::Engine, SQLite and jQuery

HTML5geolocation, videos

local storage

Wednesday, June 24, 2009

Page 23: Building a desktop app with HTTP::Engine, SQLite and jQuery

HTML5 = Future??? = 2009

Wednesday, June 24, 2009

Page 24: Building a desktop app with HTTP::Engine, SQLite and jQuery

micro Web app = 2009

Wednesday, June 24, 2009

Page 25: Building a desktop app with HTTP::Engine, SQLite and jQuery

(2 min Demo video)Wednesday, June 24, 2009

Page 26: Building a desktop app with HTTP::Engine, SQLite and jQuery

How I built this

Wednesday, June 24, 2009

Page 27: Building a desktop app with HTTP::Engine, SQLite and jQuery

Building a desktop app with HTTP::Engine, SQLite & jQuery

Tatsuhiko MiyagawaYAPC::NA 2009 Pittsburgh

Wednesday, June 24, 2009

Page 28: Building a desktop app with HTTP::Engine, SQLite and jQuery

“The most important project in Perl recently”

- jrockway

Wednesday, June 24, 2009

Page 29: Building a desktop app with HTTP::Engine, SQLite and jQuery

Based on Catalyst::Engine

Wednesday, June 24, 2009

Page 30: Building a desktop app with HTTP::Engine, SQLite and jQuery

Ruby’s RackPython’s WSGI

Wednesday, June 24, 2009

Page 31: Building a desktop app with HTTP::Engine, SQLite and jQuery

use HTTP::Engine;my $engine = HTTP::Engine‐>new(    interface => {        module => 'ServerSimple',        args   => {            host => 'localhost',            port => 9898,        },    request_handler => \&handle_request,});

$engine‐>run;

Wednesday, June 24, 2009

Page 32: Building a desktop app with HTTP::Engine, SQLite and jQuery

sub handle_request {    my $req = shift;    return HTTP::Engine::Response‐>new(        body => “Hello World”,    );}

Wednesday, June 24, 2009

Page 33: Building a desktop app with HTTP::Engine, SQLite and jQuery

Standalone, ServerSimple, POE, FastCGI, mod_perl

Wednesday, June 24, 2009

Page 34: Building a desktop app with HTTP::Engine, SQLite and jQuery

Desktop app:ServerSimple

POE (non-blocking)

Wednesday, June 24, 2009

Page 35: Building a desktop app with HTTP::Engine, SQLite and jQuery

Serve static files(HTML/CSS/JS)

Wednesday, June 24, 2009

Page 36: Building a desktop app with HTTP::Engine, SQLite and jQuery

sub handle_request {    my($self, $req) = @_;

    my $path = $req‐>path;    my $res = HTTP::Engine::Response‐>new;

    if ($path =~ s!^/static/!!) {        $self‐>serve_static_file($path, $req, $res);    };

    return $res;}

Wednesday, June 24, 2009

Page 37: Building a desktop app with HTTP::Engine, SQLite and jQuery

use Path::Class;sub serve_static_file {    my($self, $path, $req, $res) = @_;

    my $root = $self‐>conf‐>{root};    my $file = file($root, "static", $path);

    my $size  = ‐s _;    my $mtime = (stat(_))[9];    my $ext = ($file =~ /\.(\w+)$/)[0];    $res‐>content_type( MIME::Types‐>new‐>mimeTypeOf($ext)        || "text/plain" );    # ...    open my $fh, "<:raw", $file        or die "$file: $!";    $res‐>headers‐>header('Last‐Modified' =>         HTTP::Date::time2str($mtime));    $res‐>headers‐>header('Content‐Length' => $size);    $res‐>body( join '', <$fh> );}

Wednesday, June 24, 2009

Page 38: Building a desktop app with HTTP::Engine, SQLite and jQuery

See Also:HTTP::Engine::Middleware::Static

Wednesday, June 24, 2009

Page 39: Building a desktop app with HTTP::Engine, SQLite and jQuery

Implement Ajaxbackend actions(JSON-REST)

Wednesday, June 24, 2009

Page 40: Building a desktop app with HTTP::Engine, SQLite and jQuery

sub handle_request {    my($self, $req) = @_;

    my $path = $req‐>path;

    my $res = HTTP::Engine::Response‐>new;

    if ($path =~ s!^/rpc/!!) {        $self‐>dispatch_rpc($path, $req, $res);    }}

Wednesday, June 24, 2009

Page 41: Building a desktop app with HTTP::Engine, SQLite and jQuery

sub dispatch_rpc {    my($self, $path, $req, $res) = @_;

    my @class  = split '/', $path;    my $method = pop @class;    die "Access to non‐public methods" if $method =~ /^_/;

    my $rpc_class = $self‐>load_rpc_class(\@class);    my $rpc = $rpc_class‐>new( conf => $self‐>conf );    my $result = eval { $rpc‐>$method($req, $res) };

    unless ( $res‐>body ) {        $res‐>status(200);        $res‐>content_type("application/json; charset=utf‐8");        $res‐>body( Remedie::JSON‐>encode($result) );    }}

Wednesday, June 24, 2009

Page 42: Building a desktop app with HTTP::Engine, SQLite and jQuery

Problem (1):Dirty API routing

Wednesday, June 24, 2009

Page 43: Building a desktop app with HTTP::Engine, SQLite and jQuery

Path::Router, Path::Dispatcher,HTTP::Dispatcher, JSORB

Wednesday, June 24, 2009

Page 44: Building a desktop app with HTTP::Engine, SQLite and jQuery

Problem (2):Vulnerable (CSRF)

Wednesday, June 24, 2009

Page 45: Building a desktop app with HTTP::Engine, SQLite and jQuery

AuthenticationSpecial Headers

Switch to JSONRPC

Wednesday, June 24, 2009

Page 46: Building a desktop app with HTTP::Engine, SQLite and jQuery

Bonjour(based on HTTP::Server::Simple::Bonjour)

Wednesday, June 24, 2009

Page 47: Building a desktop app with HTTP::Engine, SQLite and jQuery

Auto DiscoveryShare subscriptions

Wednesday, June 24, 2009

Page 48: Building a desktop app with HTTP::Engine, SQLite and jQuery

Wednesday, June 24, 2009

Page 49: Building a desktop app with HTTP::Engine, SQLite and jQuery

Building a desktop app with HTTP::Engine, SQLite & jQuery

Tatsuhiko MiyagawaYAPC::NA 2009 Pittsburgh

Wednesday, June 24, 2009

Page 50: Building a desktop app with HTTP::Engine, SQLite and jQuery

SQL DB choices

Wednesday, June 24, 2009

Page 51: Building a desktop app with HTTP::Engine, SQLite and jQuery

MySQL/PostgreSQLGood for Web apps.

Wednesday, June 24, 2009

Page 52: Building a desktop app with HTTP::Engine, SQLite and jQuery

SQLite:file-based, type-less

Transactional

Wednesday, June 24, 2009

Page 53: Building a desktop app with HTTP::Engine, SQLite and jQuery

SQLite:best for desktop apps

Wednesday, June 24, 2009

Page 54: Building a desktop app with HTTP::Engine, SQLite and jQuery

SQLite for desktop:Firefox, Mail.app, iCal

Wednesday, June 24, 2009

Page 55: Building a desktop app with HTTP::Engine, SQLite and jQuery

End-users don’t want to run *SQL server

Wednesday, June 24, 2009

Page 56: Building a desktop app with HTTP::Engine, SQLite and jQuery

Bonus:Easy backup,

Dropbox sync

Wednesday, June 24, 2009

Page 57: Building a desktop app with HTTP::Engine, SQLite and jQuery

DB Schema

Wednesday, June 24, 2009

Page 58: Building a desktop app with HTTP::Engine, SQLite and jQuery

You don’t need DBA.Make it simple, flexible and extensible.

Wednesday, June 24, 2009

Page 59: Building a desktop app with HTTP::Engine, SQLite and jQuery

Remedie schemaFew indexes

JSON key-values

Wednesday, June 24, 2009

Page 60: Building a desktop app with HTTP::Engine, SQLite and jQuery

CREATE TABLE channel (  id      INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,  type    INTEGER NOT NULL,  parent  INTEGER NOT NULL,  ident   TEXT NOT NULL,  name    TEXT NOT NULL,  props   TEXT);

CREATE UNIQUE INDEX channel_ident ON channel (ident);

Wednesday, June 24, 2009

Page 61: Building a desktop app with HTTP::Engine, SQLite and jQuery

CREATE TABLE item (  id         INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,  channel_id INTEGER NOT NULL,  type       INTEGER NOT NULL,  ident      TEXT NOT NULL,  name       TEXT NOT NULL,  status     INTEGER NOT NULL,  props      TEXT);

CREATE INDEX item_status ON item (status)

CREATE UNIQUE INDEX item_ident ON item (channel_id, ident);

Wednesday, June 24, 2009

Page 62: Building a desktop app with HTTP::Engine, SQLite and jQuery

Key-Value is HOT

Wednesday, June 24, 2009

Page 63: Building a desktop app with HTTP::Engine, SQLite and jQuery

CouchDB, MongoDBTokyoTyrant(Need servers though)

Wednesday, June 24, 2009

Page 64: Building a desktop app with HTTP::Engine, SQLite and jQuery

SQLite for Key-Value

Wednesday, June 24, 2009

Page 65: Building a desktop app with HTTP::Engine, SQLite and jQuery

ORM?

Wednesday, June 24, 2009

Page 66: Building a desktop app with HTTP::Engine, SQLite and jQuery

Anything you like.

Wednesday, June 24, 2009

Page 67: Building a desktop app with HTTP::Engine, SQLite and jQuery

DBIx::ClassRose::DB::Object

Wednesday, June 24, 2009

Page 68: Building a desktop app with HTTP::Engine, SQLite and jQuery

Rose::DB::Object(I wanted some excuse)

Wednesday, June 24, 2009

Page 69: Building a desktop app with HTTP::Engine, SQLite and jQuery

See Also:KiokuDB

Wednesday, June 24, 2009

Page 70: Building a desktop app with HTTP::Engine, SQLite and jQuery

KiokuDBDBI / SQLite backend

Key-Value JSPON

Wednesday, June 24, 2009

Page 71: Building a desktop app with HTTP::Engine, SQLite and jQuery

Building a desktop app with HTTP::Engine, SQLite & jQuery

Tatsuhiko MiyagawaYAPC::NA 2009 Pittsburgh

Wednesday, June 24, 2009

Page 72: Building a desktop app with HTTP::Engine, SQLite and jQuery

Just use normal HTMLand CSS to design UI

Wednesday, June 24, 2009

Page 73: Building a desktop app with HTTP::Engine, SQLite and jQuery

Manipulate DOM$.ajax to do Ajax

Wednesday, June 24, 2009

Page 74: Building a desktop app with HTTP::Engine, SQLite and jQuery

DOM Manipulation sucks.

Wednesday, June 24, 2009

Page 75: Building a desktop app with HTTP::Engine, SQLite and jQuery

jQuery.flydom

Wednesday, June 24, 2009

Page 76: Building a desktop app with HTTP::Engine, SQLite and jQuery

$("#channel‐pane").createAppend(  'div', { className: 'channel‐header',           id: 'channel‐header‐' + channel.id  }, [    'div', { className: 'channel‐header‐thumb' }, [       'img', { src: "/static/images/feed.png",                alt: channel.name }, null    ],  ]);

Wednesday, June 24, 2009

Page 77: Building a desktop app with HTTP::Engine, SQLite and jQuery

jQuery.hotkeys

Wednesday, June 24, 2009

Page 78: Building a desktop app with HTTP::Engine, SQLite and jQuery

$(document).bind(‘keydown’, ‘shift+n’, function(ev){   // ‘N’ is entered});

Wednesday, June 24, 2009

Page 79: Building a desktop app with HTTP::Engine, SQLite and jQuery

jQuery.contextMenu

Wednesday, June 24, 2009

Page 80: Building a desktop app with HTTP::Engine, SQLite and jQuery

Wednesday, June 24, 2009

Page 81: Building a desktop app with HTTP::Engine, SQLite and jQuery

jQuery.corners

Wednesday, June 24, 2009

Page 82: Building a desktop app with HTTP::Engine, SQLite and jQuery

Wednesday, June 24, 2009

Page 83: Building a desktop app with HTTP::Engine, SQLite and jQuery

$.event.trigger$(document).bind

Wednesday, June 24, 2009

Page 84: Building a desktop app with HTTP::Engine, SQLite and jQuery

jQuery UI

Wednesday, June 24, 2009

Page 85: Building a desktop app with HTTP::Engine, SQLite and jQuery

Fancy stufflike Drag & Drop

Wednesday, June 24, 2009

Page 86: Building a desktop app with HTTP::Engine, SQLite and jQuery

jQuery.blockUIjQuery.scrollTojQuery.jgrowl

Wednesday, June 24, 2009

Page 87: Building a desktop app with HTTP::Engine, SQLite and jQuery

Building a desktop app with HTTP::Engine, SQLite & jQuery

Tatsuhiko MiyagawaYAPC::NA 2009 Pittsburgh

Wednesday, June 24, 2009

Page 88: Building a desktop app with HTTP::Engine, SQLite and jQuery

More like“Desktop app”

Wednesday, June 24, 2009

Page 89: Building a desktop app with HTTP::Engine, SQLite and jQuery

Client-Server

Wednesday, June 24, 2009

Page 90: Building a desktop app with HTTP::Engine, SQLite and jQuery

Web Client as “app”

Wednesday, June 24, 2009

Page 91: Building a desktop app with HTTP::Engine, SQLite and jQuery

Site Specific Browser

Wednesday, June 24, 2009

Page 92: Building a desktop app with HTTP::Engine, SQLite and jQuery

FluidPrism

Wednesday, June 24, 2009

Page 93: Building a desktop app with HTTP::Engine, SQLite and jQuery

Wednesday, June 24, 2009

Page 94: Building a desktop app with HTTP::Engine, SQLite and jQuery

CustomizeUserscripts / Userstyles

Wednesday, June 24, 2009

Page 95: Building a desktop app with HTTP::Engine, SQLite and jQuery

Fluid hooksGrowl integration

Dock menu

Wednesday, June 24, 2009

Page 96: Building a desktop app with HTTP::Engine, SQLite and jQuery

Wednesday, June 24, 2009

Page 97: Building a desktop app with HTTP::Engine, SQLite and jQuery

Client-ServerDecoupled via APIs

Wednesday, June 24, 2009

Page 98: Building a desktop app with HTTP::Engine, SQLite and jQuery

More ClientsMore “Views”

Wednesday, June 24, 2009

Page 99: Building a desktop app with HTTP::Engine, SQLite and jQuery

iPhone

Wednesday, June 24, 2009

Page 100: Building a desktop app with HTTP::Engine, SQLite and jQuery

Wednesday, June 24, 2009

Page 101: Building a desktop app with HTTP::Engine, SQLite and jQuery

Wednesday, June 24, 2009

Page 102: Building a desktop app with HTTP::Engine, SQLite and jQuery

120 lines of HTML/JSusing iUI project

Wednesday, June 24, 2009

Page 103: Building a desktop app with HTTP::Engine, SQLite and jQuery

Server as “app”

Wednesday, June 24, 2009

Page 104: Building a desktop app with HTTP::Engine, SQLite and jQuery

Packaging a server

Wednesday, June 24, 2009

Page 105: Building a desktop app with HTTP::Engine, SQLite and jQuery

local::libbuild & install all deps

Wednesday, June 24, 2009

Page 106: Building a desktop app with HTTP::Engine, SQLite and jQuery

Also:Shipwright

Wednesday, June 24, 2009

Page 107: Building a desktop app with HTTP::Engine, SQLite and jQuery

PlatypusMake .app

Wednesday, June 24, 2009

Page 108: Building a desktop app with HTTP::Engine, SQLite and jQuery

Download .zip, copy .app to /Applications, Run it.

Wednesday, June 24, 2009

Page 109: Building a desktop app with HTTP::Engine, SQLite and jQuery

Also:github.com/miyagawa/perl-app-builder

Wednesday, June 24, 2009

Page 110: Building a desktop app with HTTP::Engine, SQLite and jQuery

Summary

• micro web server as a desktop app

• HTTP::Engine, JSONRPC and router

• SQLite to store key-value

• jQuery plugins to enable desktop UIs

• More tools to make it really “.app”

Wednesday, June 24, 2009

Page 111: Building a desktop app with HTTP::Engine, SQLite and jQuery

That’s it!Questions?

Wednesday, June 24, 2009

Page 112: Building a desktop app with HTTP::Engine, SQLite and jQuery

Thank you!twitter.com/miyagawa

Wednesday, June 24, 2009