All Projects → emqx → minirest

emqx / minirest

Licence: Apache-2.0 license
A Mini RESTful API Framework

Programming Languages

erlang
1774 projects
Makefile
30231 projects

Projects that are alternatives of or similar to minirest

emqx-lwm2m
EMQ X LwM2M Gateway
Stars: ✭ 29 (-9.37%)
Mutual labels:  emqx
emqx-auth-http
EMQ X HTTP Authentication/ACL Plugin
Stars: ✭ 42 (+31.25%)
Mutual labels:  emqx
emqx-auth-username
EMQ X Authentication with Username and Password
Stars: ✭ 16 (-50%)
Mutual labels:  emqx
emqx-rule-engine
EMQ X Rule Engine
Stars: ✭ 63 (+96.88%)
Mutual labels:  emqx
dgiot-dashboard
DG-IoT平台行业应用扩展插件 DG-IoT for application plugin
Stars: ✭ 229 (+615.63%)
Mutual labels:  emqx
emqx-docs-en
EMQ X Broker Documentation
Stars: ✭ 12 (-62.5%)
Mutual labels:  emqx
mqtt5.0-cn
MQTT Version 5.0 Chinese
Stars: ✭ 22 (-31.25%)
Mutual labels:  emqx
emqx-chart
emqx kubernetes helm
Stars: ✭ 18 (-43.75%)
Mutual labels:  emqx
emqx-auth-mysql
Authentication, ACL with MySQL Database
Stars: ✭ 52 (+62.5%)
Mutual labels:  emqx
emqx-dashboard-frontend
EMQ X Dashboard Frontend
Stars: ✭ 27 (-15.62%)
Mutual labels:  emqx
qmqtt-client
MQTT Client GUI Written with Qt
Stars: ✭ 94 (+193.75%)
Mutual labels:  emqx

minirest

A mini RESTful API framework built on cowboy and swagger

UseAge

Create erlang application

rebar3 new app my_server

Add dep

open rebar.config and add minirest in deps

{deps, [{minirest, {git, "https://github.com/emqx/minirest", {tag, "1.1.2"}}}]}.

After add dep, rebar.config file should be like

{erl_opts, [debug_info]}.

{deps, [{minirest, {git, "https://github.com/emqx/minirest", {tag, "1.1.2"}}}]}.

{shell, [
    {apps, [my_server]}
]}.

Write an API provider module, example.erl

-module(example).

-behavior(minirest_api).

%% API
-export([api_spec/0]).

-export([hello/2]).

api_spec() ->
  {
    [hello_api()],
    []
  }.

hello_api() ->
    MetaData = #{
        get => #{
            description => "hello world",
            responses => #{
            <<"200">> => #{
                content => #{
                    'application/json' => #{
                        schema => #{
                            type => object,
                            properties => #{
                                msg => #{
                                    type => string}}}},
                  'text/plain' => #{
                        schema => #{
                            type => string}}}}}}},
  {"/hello", MetaData, hello}.

hello(get, #{bindings := Bindins,
             body := Body,
             query_string := QueryString,
             headers := Headers}) ->
    Content = maps:get(<<"accept">>, Headers),
    Body =
        case Content of
            <<"text/plain">> ->
                <<"hello, minirest">>;
             <<"application/json">> ->
                #{msg => <<"hello minirest">>}
        end,
    {200, #{<<"content-type">> => Content},  Body}.


% Supports callback functions for 2/3 parameters
% The first parameter is Method
% The second argument is the parsed parameters, including (bindings, query_string, headers, body)
% The third argument is the request of cowboy
-export([hello/3]).
hello(Method, #{bindings := Bindins,
                body := Body,
                query_string := QueryString,
                headers := Headers}, Request) ->
    Content = maps:get(<<"accept">>, Headers),
    Body =
        case Content of
            <<"text/plain">> ->
                <<"hello, minirest">>;
             <<"application/json">> ->
                #{msg => <<"hello minirest">>}
        end,
    {200, #{<<"content-type">> => Content},  Body}.

Start your HTTP server

    ServerName = example_server,
    App = my_server, %% or your app name
    {ok, _} = application:ensure_all_started(minirest),
    Options = #{
        port => 8088,
        apps => [App]
    },
    minirest:start(ServerName, Options).

Now, Visit http://localhost:8088/api-docs and see what happened

Example

See detail by example/my_server

TODO

  • Request filter

    query & headers

  • Parameters check

  • Test suite

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].