package sihl

  1. Overview
  2. Docs
The Sihl web framework

Install

Dune Dependency

Authors

Maintainers

Sources

0.5.0.tar.gz
md5=efe50e8f7c33f76810c6312f13a54a2a
sha512=6f3a6a6abbcce2d3569e83cfd564ed9a964efa7665956cb355b3ff85d0ce6eb80eba8ae9876d71c5e2f0ce070933e7233117c356601387dbaeccd5778a5cb289

doc/sihl/Sihl/Web/Rest/index.html

Module Web.RestSource

This module allows you to build RESTful web pages quickly.

Sourcetype action = [
  1. | `Index
  2. | `Create
  3. | `New
  4. | `Edit
  5. | `Show
  6. | `Update
  7. | `Destroy
]
Sourcetype form = (string * string option * string option) list

form represents a validated and decoded form. One element consists of (name, input, error).

name is the schema field name and the HTML input name.

input is the input of the form that was submitted, it might not be present. Use it to restore form values after it was submitted and validation or decoding failed.

error is the error message of the validation or decoding.

Sourcemodule type SERVICE = sig ... end

The SERVICE interface has to be implemented by a CRUD service that drives the resource with its business logic.

Sourcemodule type VIEW = sig ... end

The VIEW interface needs to be implemented by a module that renders HTML.

Sourcemodule type CONTROLLER = sig ... end

A module of type CONTROLLER can be used to create a resource with resource_of_controller. Use a controller instead of a service and a view if you need low level control.

Sourceval resource_of_service : ?only:action list -> string -> ('meta, 'ctor, 'resource) Conformist.t -> view:(module VIEW with type t = 'resource) -> (module SERVICE with type t = 'resource) -> router list

resource_of_service ?only name schema ~view service returns a list of routers that can be combined with choose that represent a resource.

A resource pizzas creates following 7 routers:

GET       /pizzas          `Index  Display a list of all pizzas
GET       /pizzas/new      `New    Return an HTML form for creating a new pizza
POST      /pizzas          `Create Create a new pizza
GET       /pizzas/:id      `Show   Display a specific pizza
GET       /pizzas/:id/edit `Edit   Return an HTML form for editing a pizza
PATCH/PUT /pizzas/:id      `Update Update a specific pizza
DELETE    /pizzas/:id      `Delete Delete a specific pizza

only is an optional list of actions. If only is set, routers are created only for the actions listed.

name is the name of the resource. It is good practice to use plural as the name is used to build the resource path of the URL.

schema is the conformist schema of the resource.

view is the view service of type VIEW of the resource. The view renders HTML.

service is the underlying CRUD service of type SERVICE of the resource.

Sourceval resource_of_controller : ?only:action list -> string -> ('meta, 'ctor, 'resource) Conformist.t -> (module CONTROLLER with type t = 'resource) -> router list

resource_of_controller ?only name schema controller returns the same list of routers as resource_of_service. resource_of_controller takes one module of type CONTROLLER instead of a view and a service.

If you implement your own controller you have to do all the wiring yourself, but you gain more control.

Sourceval find_form : string -> form -> string option * string option

find_form name form returns the (value, error) of a form input element with the name.

The value is the submitted value of the input element. The value is set even if the submitted form failed to decode or validate. Use the submitted values to populate the form that can be fixed and re-submitted by the user

The error message comes from either the decoding, validation or CRUD service. It can be shown to the user.

OCaml

Innovation. Community. Security.