package opium

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

An opium app provides a set of convenience functions and types to construct a rock app.

  • Re-exporting common functions needed in handlers
  • Easy handling of routes and bodies
  • Automatic generation of a command line app
type t

An opium app is a simple builder wrapper around a rock app

val sexp_of_t : t -> Sexplib0.Sexp.t
val empty : t

A basic empty app

type builder = t -> t

A builder is a function that transforms an app by adding some functionality. Builders are usuallys composed with a base app using (|>) to create a full app

val sexp_of_builder : builder -> Sexplib0.Sexp.t
val port : int -> builder
val ssl : cert:string -> key:string -> builder
val cmd_name : string -> builder
type route = string -> Opium_kernel.Rock.Handler.t -> builder

A route is a function that returns a buidler that hooks up a handler to a url mapping

val sexp_of_route : route -> Sexplib0.Sexp.t

not_found accepts a regular Opium handler that will be used instead of the default 404 handler.

val get : route

Method specific routes

val post : route
val delete : route
val put : route
val patch : route

Less common method specific routes

val options : route
val head : route
val any : Cohttp.Code.meth list -> route

any methods will bind a route to any http method inside of methods

val all : route

all methods will bind a route to a URL regardless of the http method. You may escape the actual method used from the request passed.

val action : Cohttp.Code.meth -> route
val to_rock : t -> Opium_kernel.Rock.App.t

Convert an opium app to a rock app

val start : t -> unit Lwt.t

Start an opium server. The thread returned can be cancelled to shutdown the server

val run_command : t -> unit

Create a cmdliner command from an app and run lwt's event loop

val run_command' : t -> [> `Ok of unit Lwt.t | `Error | `Not_running ]
type body = [
  1. | `Html of string
  2. | `Json of Ezjsonm.t
  3. | `Xml of string
  4. | `String of string
  5. | `Streaming of string Lwt_stream.t
]

Convenience functions for a running opium app

val json_of_body_exn : Opium_kernel.Rock.Request.t -> Ezjsonm.t Lwt.t
val string_of_body_exn : Opium_kernel.Rock.Request.t -> string Lwt.t
val urlencoded_pairs_of_body : Opium_kernel.Rock.Request.t -> (string * string list) list Lwt.t

Parse a request body encoded according to the application/x-www-form-urlencoded content type (typically from POST requests with form data) into an association list of key-value pairs. An exception is raised on invalid data.

val param : Opium_kernel.Rock.Request.t -> string -> string
val splat : Opium_kernel.Rock.Request.t -> string list
val create_stream : unit -> (?headers:Cohttp.Header.t -> ?code:Cohttp.Code.status_code -> unit Lwt.t -> Opium_kernel.Rock.Response.t Lwt.t) * (string -> unit)
val redirect : ?headers:Cohttp.Header.t -> Uri.t -> Opium_kernel.Rock.Response.t
val redirect' : ?headers:Cohttp.Header.t -> Uri.t -> Opium_kernel.Rock.Response.t Lwt.t
OCaml

Innovation. Community. Security.