Paste: pastebin-server

Author: dharmatech
Mode: factor
Date: Thu, 13 Nov 2008 19:51:54
Plain Text |
USING: kernel
       combinators
       sequences
       splitting
       accessors
       destructors
       threads
       io
       io.encodings.utf8
       io.sockets
       io.streams.duplex
       prettyprint
       math.parser
       db
       db.sqlite
       webapps.pastebin ;

IN: pastebin-server

! protocol:
! 
! list		Receive a list of the pastes, sans content
! get ID        Get a particular paste by id
! new ...	Submit a new paste

: pastebin-db ( -- db ) "/home/dharmatech/foo.db" <sqlite-db> ;

: stubs/db ( -- seq ) pastebin-db [ pastes [ f >>contents ] map ] with-db ;

: paste/db ( id -- paste ) pastebin-db [ paste ] with-db ;

: extract-id ( string -- id ) " " split second string>number ;

: read-and-respond ( -- )
  readln
  {
    { [ dup "list" =    ] [ drop       stubs/db unparse write ] }
    { [ dup "get" head? ] [ extract-id paste/db unparse write ] }
    { [ t               ] [ drop                              ] }
  }
  cond ;

: start-pastebin-server ( -- )
  T{ inet4 f "127.0.0.1" 9005 } utf8 <server>
  [ accept drop [ [ read-and-respond ] with-stream ] curry in-thread t ]
  curry
  loop ;

New Annotation

Summary:
Author:
Mode:
Body: