The content of this chapter is in the 06-http-api branch. https://gitlab.com/marianoguerra/tanodb/tree/06-http-api

How it Works We are adding a simple HTTP API to our system, it will run on all nodes and allow us to interact with it from the outside. We will use the Cowboy 2.0 HTTP Server.

Implementing it We need to add cowboy as a dependency on rebar.config and tanodb.app.src. Then in tanodb_app.erl we need to start the HTTP API: setup_http_api () -> Dispatch = cowboy_router : compile ([{ '_' , [{ "/kv/:bucket/:key" , tanodb_h_kv , []}]}]), HttpPort = application : get_env ( tanodb , http_port , 8080 ), HttpAcceptors = application : get_env ( tanodb , http_acceptors , 100 ), HttpMaxConnections = application : get_env ( tanodb , http_max_connections , infinity ), lager : info ( "Starting HTTP API at port ~p " , [ HttpPort ]), { ok , _} = cowboy : start_clear ( tanodb_http_listener , [{ port , HttpPort }, { num_acceptors , HttpAcceptors }, { max_connections , HttpMaxConnections }], #{ env => #{ dispatch => Dispatch }}), ok . We get the configuration from the environment, which is set by cuttlefish. The API handler module tanodb_h_kv's main code: init ( ReqIn = #{ method : = << "GET" >> }, State ) -> { Bucket , Key } = bindings ( ReqIn ), reply ( ReqIn , State , tanodb : get ( Bucket , Key )); init ( ReqIn = #{ method : = << "POST" >> }, State ) -> { Bucket , Key } = bindings ( ReqIn ), { ok , Value , Req1 } = read_all_body ( ReqIn ), reply ( Req1 , State , tanodb : put ( Bucket , Key , Value )); init ( ReqIn = #{ method : = << "DELETE" >> }, State ) -> { Bucket , Key } = bindings ( ReqIn ), reply ( ReqIn , State , tanodb : delete ( Bucket , Key )).