mirror https://github.com/synrc/rest

221V d7f0e3fbea code formatting 1 год назад
example d7f0e3fbea code formatting 1 год назад
src d7f0e3fbea code formatting 1 год назад
.gitignore 92e3a04789 add example, .gitignore 1 год назад
LICENSE f0cc284874 license 11 лет назад
Makefile 562e6c139b add erlang.mk 1 год назад
README.md 6d6d516e05 upd 2 1 год назад
erlang.mk 562e6c139b add erlang.mk 1 год назад
mix.exs 8f5eded56b https://hex.pm/packages/rest 10 лет назад

README.md

REST: framework with typed JSON

Usage

Just plug REST endpoint directly to your Cowboy router:

{"/rest/:resource", rest_cowboy, []},
{"/rest/:resource/:id", rest_cowboy, []},

Module

Sample REST service implementation:

-module(rest_users).
-compile({parse_transform, rest}).
-export([init/0, populate/1, new/0, exists/1, get/0, get/1, post/1, delete/1]).

-record(user, {id, cn, name, type}).
-rest_record(user).

new()                -> #user{}.
init()               -> ets:new(users, [public, named_table, {keypos, #user.id}]).
populate(Users)      -> ets:insert(users, Users).
exists(Id)           -> X = ets:member(users, erlang:binary_to_list(Id)), io:format("Member: ~p~n", [X]), X.
get()                -> ets:tab2list(users).
get(Id)              -> [U] = ets:lookup(users, erlang:binary_to_list(Id)), io:format("User: ~p~n", [U]), U.
delete(Id)           -> ets:delete(users, erlang:binary_to_list(Id)).
post(#user{} = User) -> ets:insert(users, User);
post(Data)           -> post(from_json(Data, #user{})).

Usage

curl -i -X POST -d "id=vlad" localhost:8000/rest/users
curl -i -X POST -d "id=doxtop" localhost:8000/rest/users
curl -i -X GET localhost:8000/rest/users
curl -i -X PUT -d "id=5HT" localhost:8000/rest/users/vlad
curl -i -X GET localhost:8000/rest/users/5HT
curl -i -X DELETE localhost:8000/rest/users/5HT

Credits

  • Dmitry Bushmelev
  • Maxim Sokhatsky

OM A HUM