bimap-server- Two-column database server.

Safe HaskellNone



A bimap server is basically a server that stores a one-to-one correspondence between two sets of values. You can think of it as a table with two columns, where each column has elements of the same type. You can lookup the table and update it using JSON based HTTP requests.

This is how you run the server:

bimapServer (Proxy :: Proxy Int) -- This proxy specifies the type of the values in the left column
            (Proxy :: Proxy String) -- This proxy specifies the type of the values in the right column
             5000 -- Server will be running in port 5000
             60 -- This is the number of seconds between saves

In this example, the server will save in the file "saved.bimap" the table every 60 seconds. Keep in mind that bimapServer will install a handle for SIGTERM signals, meaning that a kill signal will be catched by the process, triggering a save of the table. After saving the table, the server will stop running. However, the port that the server was using may still not be available until the program using bimapServer is closed.

The interface of the server is as follows:

  • [GET] /list: Returns the list of rows in the table. The format is [[a1,b1],...,[aN,bN]]. You can use the aeson's encoding of the type [(a,b)] for decoding it.
  • [GET] /left-lookup: Lookup an element in the table by searching in the left column. It returns the element in the right column (if any) in JSON format as described by the ToJSON instance. The element to lookup is specified using its JSON encoded form as the body of the HTTP request.
  • [GET] /right-lookup: Just as /left-lookup, except that using the right column for searching, and returning the element in the left column (if any).
  • [POST] /insert: Insert a pair of values in the table, replacing any occurences. The pair is sent in the body of the HTTP request, in JSON format, using the aeson's encoding of pairs (tuples of size 2).
  • [DELETE] /left-delete: Delete a row by searching in the left column. The value to look for in the left column is passed JSON-encoded as the body of the HTTP request.
  • [DELETE] /right-delete: Just as /left-delete, but searching in the right column.



bimapServer Source


:: (Binary a, Binary b, FromJSON a, FromJSON b, ToJSON a, ToJSON b, Ord a, Ord b) 
=> Proxy a

Type of left keys

-> Proxy b

Type of right keys

-> Int

Port to run the server

-> Int

Number of seconds between saves

-> IO () 

Function to start a bimap server. The Binary instances are used for saving the table to file. FromJSON and ToJSON instances are used for the HTTP interface. The Ord instances are used to implement fast lookups.

data Proxy t :: k -> *

A concrete, poly-kinded proxy type




Monad (Proxy *) 
Functor (Proxy *) 
Applicative (Proxy *) 
Foldable (Proxy *) 
Bounded (Proxy k s) 
Enum (Proxy k s) 
Eq (Proxy k s) 
Ord (Proxy k s) 
Read (Proxy k s) 
Show (Proxy k s) 
Ix (Proxy k s) 
Generic (Proxy * t) 
Monoid (Proxy k s) 
type Rep (Proxy k t) = D1 D1Proxy (C1 C1_0Proxy U1)