package resp-server

  1. Overview
  2. Docs
A server that communicates using the REdis Serialization Protocol

Install

Dune Dependency

Authors

Maintainers

Sources

v0.2.tar.gz
sha256=d079b9a32cbdd48a1376ece0e39a0721bde1a9332686e64e774349771433d82c
md5=40364d304770cf880797972c6307b98b

Description

resp-server allows you to build RESP servers quickly, without having to worry about any server implementation details.

Tags

redis

Published: 17 Apr 2018

README

resp-server — An OCaml library for building servers that speak RESP

%%VERSION%%

resp-server is an OCaml library for building servers that communicate using the Redis serialization protocol.

resp-server is distributed under the ISC license.

Homepage: https://github.com/zshipko/resp-server

Installation

resp-server can be installed with opam:

opam install resp-server

If you don't use opam consult the opam file for build instructions.

Documentation

To generate documentation locally run odig odoc resp-server - then to view run odig doc in the root of the project.

See src/resp_server.mli for the commented interface description.

Getting started

To create a new server using resp-server you need to define a few modules.

As an example we can create a simple counter server that has keys with integer values that can be incremented and decremented:

  1. BACKEND - defines the request context and client types

module Backend = struct
    (** This is the global request context type *)
    type t = (string, int) Hashtbl.t

    (** The client type is the per-client request context type *)
    type client = ()

    type new_client _ctx = ()
end
  1. AUTH - defines authentication types

module Auth = struct
    type t = string
    let check t cmd =
        Array.length cmd > 0 && cmd.(0) = t
end
  1. Use Make to create the new server

module Server = Make(Auth)(Backend)
  1. Define some commands

let modify_value db args f =
    match args with
    | [| String key |] ->
        (match Hashtbl.find_opt srv key with
        | Some i -> Hashtbl.replace srv key (f i)
        | None -> Hashtbl.replace srv key (f 0)
        end;
        Server.ok)
    | _ -> Server.error "Invalid arguments"

let _incr db _cli _cmd args =
    modify_value db args (fun a -> a + 1)

let _decr db _cli _cmd args =
    modify_value db args (fun a -> a - 1)

let commands = [
    "incr", _incr;
    "decr", _decr;
]
  1. Create and run the server

let main =
    let db = Hashtbl.create 16 in
    let auth = "password" in
    let srv = Server.create ~auth ~commands (`TCP (`Port 1234)) db in
    Server.run srv

let () = Lwt.main.run main

Tests

In the distribution sample programs and tests are located in the test directory. They can be built and run with:

jbuilder runtest

Dependencies (4)

  1. conduit-lwt-unix >= "1.0" & < "2.0.0"
  2. hiredis <= "0.7"
  3. jbuilder >= "1.0+beta7"
  4. ocaml >= "4.05.0"

Dev Dependencies

None

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.