Skip to content

Commit 8bd861f

Browse files
Merge pull request #104 from paulo-ferraz-oliveira/feature/improved-doc-and-example
Improved doc. and example
2 parents 944e9e9 + 3915b20 commit 8bd861f

File tree

16 files changed

+253
-308
lines changed

16 files changed

+253
-308
lines changed

.github/workflows/ci.yml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,40 @@ jobs:
2121
with:
2222
otp-version: ${{matrix.otp_vsn}}
2323
rebar3-version: '3.14'
24-
- run: rebar3 test
24+
- run: |
25+
rm -rf example/_checkouts # Prevent long running analysis
26+
rebar3 test
27+
example:
28+
name: Makes sure our example run over ${{matrix.otp_vsn}} and ${{matrix.os}}
29+
runs-on: ${{matrix.os}}
30+
strategy:
31+
matrix:
32+
otp_vsn: [21, 22, 23, 24]
33+
os: [ubuntu-latest]
34+
steps:
35+
- uses: actions/checkout@v2
36+
- uses: erlef/setup-beam@v1
37+
with:
38+
otp-version: ${{matrix.otp_vsn}}
39+
rebar3-version: '3.14'
40+
- name: test our example
41+
run: |
42+
cd example
43+
rebar3 release
44+
_build/default/rel/example/bin/example daemon
45+
R=$(curl -s -w "%{http_code}" -XPUT "localhost:8080/poor-kv/some-key/some-value" -H "content-type: text/plain")
46+
[[ $R == "some-value201" ]] || exit 1 # This becomes some-value200 after the first PUT
47+
R=$(curl -s -w "%{http_code}" "localhost:8080/poor-kv/some-key" -H "content-type: text/plain")
48+
[[ $R == "some-value200" ]] || exit 1
49+
R=$(curl -s -w "%{http_code}" "localhost:8080/poor-kv/non-existing-key" -H "content-type: text/plain")
50+
[[ $R == "404" ]] || exit 1
51+
R=$(curl -s -w "%{http_code}" -XDELETE "localhost:8080/poor-kv/some-key" -H "content-type: text/plain")
52+
[[ $R == "204" ]] || exit 1
53+
R=$(curl -s -w "%{http_code}" -XDELETE "localhost:8080/poor-kv/some-key" -H "content-type: text/plain")
54+
[[ $R == "404" ]] || exit 1
55+
_build/default/rel/example/bin/example stop
56+
- name: check our example
57+
run: |
58+
cd example
59+
rm -rf _checkouts # Prevent long running analysis
60+
rebar3 test

README.md

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
[![Stories in Ready](https://badge.waffle.io/inaka/cowboy-trails.png?label=ready&title=Ready)](https://waffle.io/inaka/cowboy-trails)
2-
31
<img src="https://lh5.googleusercontent.com/-Y1n1Vh4FjLE/TXDZiQ_zSVI/AAAAAAAAAJY/h47az_0MxO0/s1600/Western+backdrop+04.png" height="200" width="100%" />
42

53
# cowboy-trails
@@ -9,22 +7,24 @@
97
Cowboy routes on steroids!
108

119
## Contact Us
10+
1211
If you find any **bugs** or have a **problem** while using this library, please
13-
[open an issue](https://github.com/inaka/elvis/issues/new) in this repo
14-
(or a pull request :)).
12+
[open an issue](https://github.com/inaka/cowboy-trails/issues/new) in this repo
13+
(or a pull request 😄).
1514

16-
And you can check all of our open-source projects at [inaka.github.io](http://inaka.github.io).
15+
And you can check out all of our open-source projects at [inaka.github.io](http://inaka.github.io).
1716

1817
## Why Cowboy Trails?
19-
**Cowboy-Trails** enables you to:
2018

21-
* Add information to `cowboy` routes, which can be used later to interact with
22-
the server in a higher abstraction level.
19+
**Cowboy-Trails** enables you to:
2320

24-
* Define the server routes directly within the module that implements them.
21+
* add information to `cowboy` routes, which can later be used to interact with
22+
the server in a higher abstraction level,
23+
* define the server routes directly within the module that implements them.
2524

2625
## How to Use it?
27-
The first use case for `cowboy_trails` is to compile `cowboy` routes.
26+
27+
The most common use case for `cowboy_trails` is to compile `cowboy` routes.
2828

2929
Normally with `cowboy` you compile routes in the following way:
3030

@@ -38,18 +38,18 @@ Routes = [{'_',
3838
cowboy_router:compile(Routes),
3939
```
4040

41-
Trails is also fully compatible with `cowboy` routes, so you can pass the same
42-
routes in order to be processed by trails:
41+
Trails is fully compatible with `cowboy` routes, so you can pass the same
42+
routes in order to be processed by Trails:
4343

4444
```erlang
4545
trails:compile(Routes),
4646
```
4747

48-
So far it seems like there is not any difference, right? But the most common case
49-
with `cowboy` is that you usually work with a single host, even though you're
48+
So far it seems like there's no difference, right? But most of the time,
49+
with `cowboy`, you usually work with only a single host, but you're
5050
required to keep defining the host parameter within the routes (`[{'_', [...]}]`).
5151

52-
Well, with trails you have another useful function to compile single host routes:
52+
Well, with Trails you have another useful function to compile single host routes:
5353

5454
```erlang
5555
%% You only define the routes/paths
@@ -59,13 +59,15 @@ Routes = [ {"/resource1", resource1_handler, []}
5959
trails:single_host_compile(Routes),
6060
```
6161

62-
Now, let's suppose that you want to add additional information (metadata) to
63-
cowboy routes related with the semantics of each HTTP method.
62+
Now, let's suppose you want to add metadata to
63+
`cowboy` routes related with the semantics of each HTTP method.
64+
65+
You'd do something like:
6466

6567
```erlang
6668
Metadata = #{put => #{description => "PUT method"},
67-
post => #{ description => "POST method"},
68-
get => #{ description => "GET method"}},
69+
post => #{description => "POST method"},
70+
get => #{description => "GET method"}},
6971
Trail = trails:trail("/",
7072
cowboy_static,
7173
{private_file, "index2.html"},
@@ -75,9 +77,9 @@ Trail = trails:trail("/",
7577
Metadata = trails:metadata(Trail),
7678
```
7779

78-
This can be used later to generate documentation related to each endpoint.
80+
This can then be used to generate documentation related to each endpoint.
7981

80-
Normally, when you work with `cowboy` you have to define all routes in one place:
82+
Also, when you work with `cowboy`, you have to define all routes in one place:
8183

8284
```erlang
8385
Routes =
@@ -100,9 +102,10 @@ Routes =
100102
Dispatch = cowboy_router:compile(Routes),
101103
```
102104

103-
But now with `trails` you're able to define the routes on each resource handler.
104-
The handler must implement the callback `trails/0` or `trails/1` and return the specific
105-
routes for that handler. For a better understanding, you can check out the
105+
But now, with `trails`, you're able to define the routes on each of your resource handlers,
106+
separately.
107+
These handlers must implement callback `trails/0` or `trails/1` and return the specific
108+
routes that define them. For a better understanding, you can check out the
106109
examples in the `test` folder ([trails_test_handler](./test/trails_test_handler.erl)).
107110

108111
Once you have implemented the `trails/0` or `trails/1` callback on your handlers, you can do
@@ -128,26 +131,10 @@ Trails =
128131
trails:single_host_compile(Trails),
129132
```
130133

131-
This way each handler keeps their own routes, as it should be, and you can
134+
This way each handler maintains their own routes, as it should be, and you can
132135
merge them easily.
133136

134137
## Example
135138

136139
For more information about `cowboy_trails`, how to use it and the different
137-
functions that it exposes, please check this [Example](./example).
138-
139-
## Testing
140-
141-
This project's test suites include [meta testing](http://inaka.net/blog/2015/11/13/erlang-meta-test-revisited/).
142-
Therefore, in order to run the tests, it requires a proper plt.
143-
Otherwise, when you try `rebar3 ct`, you'll get an error similar to:
144-
145-
```erlang
146-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
147-
ktn_meta_SUITE:dialyzer failed on line 60
148-
Reason: {test_case_failed,No plts at ../../*.plt - you need to at least have one}
149-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
150-
```
151-
152-
To generate the required plt, just run `rebar3 dialyzer` once and then you can
153-
run `rebar3 ct` as many times as you like.
140+
functions that it exposes, please check this [example](./example).

example/.gitignore

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
*.beam
2-
*.log
3-
*.plt
4-
*pem
5-
.DS_Store
6-
all.coverdata
7-
bin
8-
/deps/mixer
9-
ebin
1+
.rebar3
2+
_build
103
erl_crash.dump
114
log
12-
logs
13-
.erlang.mk.packages.v2
5+
rebar3.crashdump

example/README.md

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,64 @@
1-
# Cowboy Trails Example
1+
# Cowboy Trails Basic Example
22

33
To try this example, you need `git` and `Erlang` in your PATH.
44

5-
```erlang
6-
7-
rebar3 compile
5+
```shell
6+
$ rebar3 compile
87
```
98

109
To start the example server in a interactive way do this:
11-
```erlang
12-
13-
rebar3 shell
14-
```
15-
16-
## Handlers Description
17-
18-
if you run this `curl` command, you will see all trails description of this server.
1910

11+
```shell
12+
$ rebar3 shell
2013
```
21-
curl -i http://localhost:8080/description
2214

23-
[#{constraints => [],
24-
handler => cowboy_static,
25-
metadata => #{get => #{desc => "Static Data"}},
26-
options => {priv_dir,example,[],[{mimetypes,cow_mimetypes,all}]},
27-
path_match => <<"/[...]">>},
28-
#{constraints => [],
29-
handler => example_echo_handler,
30-
metadata => #{get => #{desc => "Gets echo var in the server"},
31-
put => #{desc => "Sets echo var in the server"}},
32-
options => [],
33-
path_match => <<"/message/[:echo]">>},
34-
#{constraints => [],
35-
handler => example_description_handler,
15+
## Handlers' Description
16+
17+
If you run this `curl` command, you will see all trails' description of this server.
18+
19+
```shell
20+
$ curl -i http://localhost:8080/description
21+
[#{constraints => [],handler => example_poor_kv_handler,
22+
metadata =>
23+
#{delete =>
24+
#{'content-type' => "text/plain",
25+
desc => "Unsets an env var in the server"},
26+
get =>
27+
#{'content-type' => "text/plain",
28+
desc => "Gets en env var from the server"},
29+
put =>
30+
#{'content-type' => "text/plain",
31+
desc => "Sets an env var in the server"}},
32+
options => [],path_match => "/poor-kv/:key/[:value]"},
33+
#{constraints => [],handler => example_description_handler,
3634
metadata => #{get => #{desc => "Retrives trails's server description"}},
37-
options => [],
38-
path_match => <<"/description">>}]
35+
options => [],path_match => <<"/description">>}]
3936
```
4037

41-
## Poor-Man's Key-Value Store
38+
## Poor Man's Key-Value Store
4239

43-
There is an additional endpoint that implement a really simple key-value store
44-
that uses the application's env variable as its backend. It is possible to `PUT`,
40+
There is an additional endpoint that implements a really simple key-value store
41+
that uses the application's env. variable as its backend. It is possible to `PUT`,
4542
`GET` and `DELETE` values in the following way:
4643

47-
```
44+
```shell
4845
$ curl -XPUT "localhost:8080/poor-kv/some-key/some-value" -H "content-type: text/plain"
49-
5046
some-value
5147

52-
$ curl -XGET "localhost:8080/poor-kv/some-key" -H "content-type: text/plain"
53-
48+
$ curl "localhost:8080/poor-kv/some-key" -H "content-type: text/plain"
5449
some-value
5550

56-
$ curl -v -XGET "localhost:8080/poor-kv/non-existing-key" -H "content-type: text/plain"
57-
51+
$ curl -v "localhost:8080/poor-kv/non-existing-key" -H "content-type: text/plain"
5852
...
5953
< HTTP/1.1 404 Not Found
6054
...
6155

6256
$ curl -v -XDELETE "localhost:8080/poor-kv/some-key" -H "content-type: text/plain"
63-
6457
...
6558
< HTTP/1.1 204 No Content
6659
...
6760

6861
$ curl -v -XDELETE "localhost:8080/poor-kv/some-key" -H "content-type: text/plain"
69-
7062
...
7163
< HTTP/1.1 404 Not Found
7264
...

example/_checkouts/trails

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../.

example/elvis.config

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)