All Projects → fbeline → Rooster

fbeline / Rooster

Licence: mit
Erlang REST framework

Programming Languages

erlang
1774 projects

Labels

Projects that are alternatives of or similar to Rooster

Webtau
Webtau (short for web test automation) is a testing API, command line tool and a framework to write unit, integration and end-to-end tests. Test across REST-API, Graph QL, Browser, Database, CLI and Business Logic with consistent set of matchers and concepts. REPL mode speeds-up tests development. Rich reporting cuts down investigation time.
Stars: ✭ 156 (-9.83%)
Mutual labels:  rest-api
Rest Api Slim Php
Example of REST API with Slim PHP Framework.
Stars: ✭ 165 (-4.62%)
Mutual labels:  rest-api
Rest Api Basics
This is a basic guide on how to build a REST API with Django & Python. For much deeper depth, check out our new course on REST API: (https://kirr.co/90kxtx)
Stars: ✭ 171 (-1.16%)
Mutual labels:  rest-api
Hey Jetson
Deep Learning based Automatic Speech Recognition with attention for the Nvidia Jetson.
Stars: ✭ 161 (-6.94%)
Mutual labels:  rest-api
Workshops
Workshops organized to introduce students to security, AI, AR/VR, hardware and software
Stars: ✭ 162 (-6.36%)
Mutual labels:  rest-api
Finale
Create flexible REST endpoints and controllers from Sequelize models in your Express app
Stars: ✭ 167 (-3.47%)
Mutual labels:  rest-api
Scalable Data Science Platform
Content for architecting a data science platform for products using Luigi, Spark & Flask.
Stars: ✭ 158 (-8.67%)
Mutual labels:  rest-api
Forrest
A Laravel library for Salesforce
Stars: ✭ 171 (-1.16%)
Mutual labels:  rest-api
Rpi wordclock
Software to create a Raspberry Pi based wordclock
Stars: ✭ 164 (-5.2%)
Mutual labels:  rest-api
Lucid
High performance and distributed KV store w/ REST API. 🦀
Stars: ✭ 171 (-1.16%)
Mutual labels:  rest-api
Linkis
Linkis helps easily connect to various back-end computation/storage engines(Spark, Python, TiDB...), exposes various interfaces(REST, JDBC, Java ...), with multi-tenancy, high performance, and resource control.
Stars: ✭ 2,323 (+1242.77%)
Mutual labels:  rest-api
Jda
Java wrapper for the popular chat & VOIP service: Discord https://discord.com
Stars: ✭ 2,598 (+1401.73%)
Mutual labels:  rest-api
Movies Restapi
RESTful API to manage movies written in Go and uses MongoDB as storage
Stars: ✭ 168 (-2.89%)
Mutual labels:  rest-api
Pop
Monorepo of the PoP project, including: a server-side component model in PHP, a GraphQL server, a GraphQL API plugin for WordPress, and a website builder
Stars: ✭ 160 (-7.51%)
Mutual labels:  rest-api
Djangorestframework Queryfields
Allows clients to control which fields will be sent in the API response
Stars: ✭ 170 (-1.73%)
Mutual labels:  rest-api
Restcountries
Get information about countries via a RESTful API
Stars: ✭ 2,054 (+1087.28%)
Mutual labels:  rest-api
Ynab Sdk Js
YNAB API JavaScript Library
Stars: ✭ 167 (-3.47%)
Mutual labels:  rest-api
Jkes
A search framework and multi-tenant search platform based on java, kafka, kafka connect, elasticsearch
Stars: ✭ 173 (+0%)
Mutual labels:  rest-api
Rusticsearch
Lightweight Elasticsearch compatible search server.
Stars: ✭ 171 (-1.16%)
Mutual labels:  rest-api
Fiber
⚡️ Express inspired web framework written in Go
Stars: ✭ 17,334 (+9919.65%)
Mutual labels:  rest-api

Rooster Build Status

Simplistic REST framework that runs on top of mochiweb.

Features

  • Routes Composable routing system that supports GET POST PUT and DELETE http verbs.
  • Middleware: Functions that have access to the request and the response, intercepting routes before and/or after execution.
  • Basic Authentication: Authentication module that can be easily integrated with Middleware.
  • HTTPS Support

Installation

  1. Download and install rebar3

  2. Create a new application using rebar

  3. Edit the file rebar.config and add the following lines inside deps:

{deps, [ {rooster, ".*", {git, "git://github.com/fbeline/rooster.git", {branch, "master"}}} ]}.

  1. Compile and download dependencies with rebar3 compile

Quick start

Create an entry file that will be the initialization module of your application:

-module(server).
-export([start/0]).

start() ->
  rooster:start(#{port => 8080},
                #{routes => [hello()]}).

hello() ->
  {'GET', "/hello", fun(_) -> {200, #{message => <<"hello world">>}} end}.

Start it using the command:

erl \
  -pa ebin _build/default/lib/*/ebin \
  -boot start_sasl \
  -s server \
  -s reloader

Run curl localhost:8080/hello and it should return:

{"message": "hello world"}

Routes

Given the following functions, you will find how to define routes using generic or nested definition.

-export([exports/0]).

% custom header
get_products(_Req) ->
  {200, [#{..}, #{..}], [{"custom-header", "foo"}]}.

% request path param
get_product(#{params := params}) ->
  Id = maps:get(id, params),
  {200, #{id => Id, price => 8000}}.

% request payload
save_product(#{body := Body}) ->
  {201, Body}.

Is important to note that the function must have one parameter, that will contain the request information.

Generic definition

The simplest way of defining routes.

exports() ->
  [{'GET', "/products", fun get_products/1, [auth]},
   {'POST', "/products", fun save_product/1, [auth, admin]}
   {'GET', "/products/:id", fun get_product/1, [auth]}].

The exports method will provide the list of available endpoints that this module contains. Each tuple should have {HTTP verb, route path, route handler, list of middleware}, the list of middleware is not a required parameter as a specific route may use none.

Nested definition

For routes that gonna share a specific root path and or middleware, declaring routes in a nested way should be the proper solution.

exports() ->
  [{"/products", [auth],
    [{'GET', fun get_products/1},
     {'POST', fun save_product/1, [admin]}
     {'GET', "/:id", fun get_product/1}]}].

The nested definition should fit on the specification:

{root path, list of middleware,
   [{HTTP verb, *nested path*, route handler, *list of middleware*}]}

Ps: The parameters surround by * are not required.

Request

The request that will be passed to the route handlers is a map as the one bellow:

#{path          => ...,
  method        => ...,
  headers       => ...,
  body          => ...,
  qs            => ...,
  params        => ...,
  cookies       => ...,
  authorization => ...}

Middleware

The middleware map can have both leave and enter keys. The enter function will have access to the request information and will be able to change it, the leave function will have access to the response and will be able to change it as well. At any moment that a middleware returns {break, {status, response}} the chain of execution will terminate and the response will be evaluated as the request result.

middleware

CORS

Simple example using a middleware that intercepts the route handler response and add to it custom headers.

-export([cors/0]).

access_control() ->
  [{"access-control-allow-methods", "*"},
   {"access-control-allow-headers", "*"},
   {"access-control-allow-origin", "*"}].

cors() ->
  #{name  => cors,
    leave => fun({Status, Resp, Headers}) -> {Status, Resp, Headers ++ access_control()} end}.

Basic authentication

Intercepts the http request before the route handler executes and returns 403 if credentials do not match.

-export([auth/0]).

basic_auth(#{authorization := Auth} = Req) ->
  Authorizated = rooster_basic_auth:is_authorized(Auth, {"admin", "admin"}),
  case Authorizated of
    true ->
      Req;
    _ ->
      {break, {403, #{reason => <<"Access Forbidden">>}}}
  end.

auth() ->
  #{name => auth,
    enter => fun basic_auth/1}.

SSL

After generating the SSL certificate for your domain, everything that needs to be done is to pass some extra parameters for the server configuration map: (ssl and ssl_opts).

#{port     => 8080,
  ssl      => {ssl, true},
  ssl_opts => {ssl_opts, [{certfile, "{PATH}/server_cert.pem"},
                          {keyfile, "{PATH}/server_key.pem"}]}}

Dependencies

  • mochiweb: HTTP server
  • jsx: JSON parser

License

MIT

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].