• Stars
    star
    70
  • Rank 432,360 (Top 9 %)
  • Language
    Erlang
  • License
    Apache License 2.0
  • Created almost 9 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A couple of improvements over Cowboy Routes

cowboy-trails

build

Cowboy routes on steroids!

Contact Us

If you find any bugs or have a problem while using this library, please open an issue in this repo (or a pull request πŸ˜„).

And you can check out all of our open-source projects at inaka.github.io.

Why Cowboy Trails?

Cowboy-Trails enables you to:

  • add information to cowboy routes, which can later be used to interact with the server in a higher abstraction level,
  • define the server routes directly within the module that implements them.

How to Use it?

The most common use case for cowboy_trails is to compile cowboy routes.

Normally with cowboy you compile routes in the following way:

Routes = [{'_',
           [ {"/resource1", resource1_handler, []}
           , {"/resource2/[:id]", resource2_handler, []}
           ]
          }
         ],
cowboy_router:compile(Routes),

Trails is fully compatible with cowboy routes, so you can pass the same routes in order to be processed by Trails:

trails:compile(Routes),

So far it seems like there's no difference, right? But most of the time, with cowboy, you usually work with only a single host, but you're required to keep defining the host parameter within the routes ([{'_', [...]}]).

Well, with Trails you have another useful function to compile single host routes:

%% You only define the routes/paths
Routes = [ {"/resource1", resource1_handler, []}
         , {"/resource2/[:id]", resource2_handler, []}
         ],
trails:single_host_compile(Routes),

Now, let's suppose you want to add metadata to cowboy routes related with the semantics of each HTTP method.

You'd do something like:

Metadata = #{put => #{description => "PUT method"},
             post => #{description => "POST method"},
             get => #{description => "GET method"}},
Trail = trails:trail("/",
                     cowboy_static,
                     {private_file, "index2.html"},
                     Metadata,
                     []),
%% You can later retrieve the metadata:
Metadata = trails:metadata(Trail),

This can then be used to generate documentation related to each endpoint.

Also, when you work with cowboy, you have to define all routes in one place:

Routes =
  [{'_',
    [ {"/", cowboy_static, {file, "www/index.html"}}
    , {"/favicon.ico", cowboy_static, {file, "www/assets/favicon.ico"}}
    , {"/assets/[...]", cowboy_static, {dir, "www/assets"}}
    , {"/game/:game_id", cowboy_static, {file, "www/game.html"}}
    , {"/api/status", spts_status_handler,  []}
    , {"/api/games", spts_games_handler, []}
    , {"/api/games/:game_id", spts_single_game_handler, []}
    , {"/api/games/:game_id/serpents", spts_serpents_handler, []}
    , { "/api/games/:game_id/serpents/:token"
      , spts_single_serpent_handler, []
      }
    , {"/api/games/:game_id/news", lasse_handler, [spts_news_handler]}
    ]
   }
  ],
Dispatch = cowboy_router:compile(Routes),

But now, with trails, you're able to define the routes on each of your resource handlers, separately. These handlers must implement callback c:trails_handler:trails/0 or c:trails_handler:trails/1 and return the specific routes that define them. For a better understanding, you can check out the examples in the test folder (trails_test_handler).

Once you have implemented the c:trails_handler:trails/0 or c:trails_handler:trails/1 callback on your handlers, you can do something like this:

Handlers =
  [ spts_status_handler
  , spts_games_handler
  , spts_single_game_handler
  , spts_serpents_handler
  , spts_single_serpent_handler
  , spts_news_handler
  , {support_params_handler, #{key => value}}
  ],
Trails =
  [ {"/", cowboy_static, {file, "www/index.html"}}
  , {"/favicon.ico", cowboy_static, {file, "www/assets/favicon.ico"}}
  , {"/assets/[...]", cowboy_static, {dir, "www/assets"}}
  , {"/game/:game_id", cowboy_static, {file, "www/game.html"}}
  | trails:trails(Handlers)
  ],
trails:single_host_compile(Trails),

This way each handler maintains their own routes, as it should be, and you can merge them easily.

Example

For more information about cowboy_trails, how to use it and the different functions that it exposes, please check this example.

More Repositories

1

erlang_guidelines

Inaka's Erlang Coding Guidelines
Erlang
618
star
2

EventSource

A simple Swift client library for the Server Sent Events (SSE)
Swift
453
star
3

galgo

When you want your logs to be displayed on screen
Java
428
star
4

elvis

Erlang Style Reviewer
Erlang
415
star
5

apns4erl

Apple Push Notification Server for Erlang
Erlang
370
star
6

TinyTask

A Tiny Task Library
Java
322
star
7

worker_pool

Erlang worker pool
Erlang
272
star
8

sumo_db

Erlang Persistency Framework
Erlang
173
star
9

shotgun

For the times you need more than just a gun.
Erlang
165
star
10

Dayron

A repository `similar` to Ecto.Repo that maps to an underlying http client, sending requests to an external rest api instead of a database
Elixir
159
star
11

cowboy_swagger

Swagger integration for Cowboy (built on trails)
Erlang
116
star
12

gold_fever

A Treasure Hunt for Erlangers
Erlang
86
star
13

Jayme

Abstraction layer that eases RESTful interconnections in Swift
Swift
81
star
14

guidelines

General Inaka Guidelines
74
star
15

jem.js

Just Erlang Maps for Javascript
JavaScript
69
star
16

sheldon

Very Simple Erlang Spell Checker
Erlang
62
star
17

niffy

Inline C code in Erlang modules to build NIFs
Erlang
60
star
18

sumo_rest

Generic cowboy handlers to work with Sumo
Erlang
59
star
19

serpents

Multi-Player Game on top of HDP protocol
Erlang
57
star
20

elvis_core

The core of an Erlang linter
Erlang
52
star
21

xref_runner

Erlang Xref Runner (inspired in rebar xref)
Erlang
50
star
22

erlang-github

Github API client
Erlang
46
star
23

lasse

SSE handler for Cowboy
Erlang
45
star
24

beam_olympics

Let's find the fastest beamer!
Erlang
39
star
25

fiar

Four in a Row - A game to learn Erlang
Erlang
36
star
26

zipper

Generic Zipper implementation in Erlang
Erlang
34
star
27

ios-xmpp-sample

Blog post sample project.
Swift
33
star
28

kotlillon

Android Kotlin Examples
Kotlin
33
star
29

katana-test

Meta Testing Utilities for common_test
Erlang
32
star
30

match_stream

A sample project to show in our scale blog post
JavaScript
30
star
31

phoenix_passwordless_login

Phoenix Passwordless Login
Elixir
29
star
32

KillerTask

Android AsyncTask wrapper library, written in Kotlin
Kotlin
26
star
33

canillita

Simple Paperboy-themed PubSub
Erlang
26
star
34

lewis

Rock your Android
Java
22
star
35

tirerl

Erlang interface to Elastic Search
Erlang
19
star
36

itweet

Twitter Stream API on ibrowse
Erlang
18
star
37

katana-code

Code Utilities for Erlang
Erlang
17
star
38

pusherman

queuing system for push notifications
Erlang
17
star
39

galgo-ios

When you want your logs to be displayed on screen
Objective-C
16
star
40

FadeButton

Fading effects for UIButtons made simple
Swift
16
star
41

lsl

NIM in Erlang
Erlang
15
star
42

credo_server

Credo Server
Elixir
15
star
43

Jolly

Jolly Chimp that keeps track of our Github Repos
Swift
12
star
44

rpsls

Rock Paper Scissors Lizzard Spock World Championship in Erlang
Erlang
12
star
45

ikbot

An elixir based customizable hipchat bot
Elixir
12
star
46

nconf

Nested Configuration Manager for Erlang Applications
Erlang
12
star
47

pushito

APNS over HTTP/2
Elixir
11
star
48

rest_guidelines

REST API Design Guidelines
11
star
49

fetjaba

From Erlang To Java and Back Again
Erlang
9
star
50

sumo_db_pgsql

PostgreSQL adapter for sumo_db.
Erlang
9
star
51

jinterface_stdlib

Erlang stdlib implementation on Java, based on JInterface
Java
9
star
52

IKCapture

Snapchat-Like Image Capture Library
Objective-C
8
star
53

PictureViewMaster

Interactive image projector.
Swift
8
star
54

toy_kv

A simple and reduced Key-Value Store written in Erlang
Erlang
7
star
55

ColorPicker

Color Picker for Swift
Swift
7
star
56

MediaPickerController

Neat API for presenting the classical action sheet for picking an image or video from the device or camera.
Swift
7
star
57

swift_guidelines

Inaka's Swift Coding Guidelines
7
star
58

spellingci

Spelling CI Server
Erlang
7
star
59

talks

Sources and pdfs of our talks and speeches
TeX
6
star
60

bookmarks

A collection of bookmarks for Inakos
6
star
61

IKJayma

RESTful API abstraction for Server Interconnection
Objective-C
6
star
62

hexer

Hex.pm integration in escript format.
Erlang
6
star
63

hexer.mk

erlang.mk plugin for hexer
Makefile
5
star
64

android_guidelines

Inaka's Android Development Guidelines
5
star
65

elvis.mk

3rd party erlang.mk plug-in for Elvis
Shell
5
star
66

plixir

Poker + Elixir + Phoenix
CSS
5
star
67

sumo_db_elasticsearch

ElasticSearch adapter for sumo_db
Erlang
4
star
68

tele_sign

Node.js library to send messages through http://www.telesign.com/
JavaScript
4
star
69

ios_guidelines

Inaka's iOS Coding Guidelines
Objective-C
3
star
70

sumo_db_riak

Riak adapter for sumo_db
Erlang
3
star
71

android-excercises

Quick test for Android candidates
3
star
72

sumo_db_mysql

MySQL adapter for sumo_db
Erlang
2
star
73

inaka.mk

erlang.mk extras that we generally use in all of our projects
Makefile
2
star
74

sumo_db_mongo

MongoDB adapter for sumo_db
Erlang
2
star
75

gold_fever-solver

A solver for the http://github.com/inaka/gold_fever game
Erlang
2
star
76

Otec

A swift app to showcase our best open-source libraries
Swift
2
star
77

inaka.github.io

Inaka's Open Source Projects
HTML
2
star
78

g2x

Graffle to XCode
Objective-C
2
star
79

beam_olympics-extended

Internal repo to keep secret beam_olympics tasks from the public view
Erlang
2
star
80

beam_olympics-solver

Solutions for beam_olympics
Elixir
2
star
81

ruby_guidelines

Our own guidelines when it comes to ruby development
1
star
82

pokedex

Dumb repo to prove what we can do with sumo{_db|rest_}
Erlang
1
star
83

homebrew-formulas

Homebrew formulas for some of our tools
Ruby
1
star
84

updike

Run, rabbit, run
1
star
85

ios-scripts

Helper scripts that you can use in your iOS apps
Shell
1
star
86

INSocketListener

SSE Socket Listener for Objective-C
Objective-C
1
star
87

emarkdown

Based on https://github.com/devinus/markdown - but for Erlang :)
C
1
star