Skip to content

Commit 38bb4bc

Browse files
committed
update samples
1 parent e3c8338 commit 38bb4bc

File tree

1 file changed

+110
-20
lines changed
  • samples/server/echo_api/erlang-server

1 file changed

+110
-20
lines changed

samples/server/echo_api/erlang-server/README.md

+110-20
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,123 @@
44

55
An Erlang server stub generated by [OpenAPI Generator](https://openapi-generator.tech) given an OpenAPI spec.
66

7-
Dependencies: Erlang OTP/27 and rebar3. Also:
8-
- [Cowboy](https://hex.pm/packages/cowboy)
9-
- [Ranch](https://hex.pm/packages/ranch)
10-
- [Jesse](https://hex.pm/packages/jesse)
11-
127
## Prerequisites
138

9+
1. [Erlang/OTP (v27)](https://www.erlang.org/)
10+
11+
2. [rebar3](https://rebar3.org/)
12+
13+
3. Erlang libraries:
14+
- [Cowboy](https://hex.pm/packages/cowboy)
15+
- [Ranch](https://hex.pm/packages/ranch)
16+
- [Jesse](https://hex.pm/packages/jesse)
17+
18+
4. OpenAPI generator script `openapi-generator-cli`
19+
(for more information see [OpenAPI Generator - Getting Started](https://github.com/OpenAPITools/openapi-generator#2---getting-started) )
20+
21+
5. OpenAPI specification file in the current folder (for example [petstore.yaml](https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml))
22+
23+
1424
## Getting started
15-
Use erlang-server with rebar3
25+
Use `erlang-server` with `rebar3`
26+
27+
1. Create a folder with an Erlang application by using `rebar3`
28+
29+
`$ rebar3 new app http_server`
30+
31+
2. Generate OpenAPI `erlang-server` project using `openapi-generator`
32+
33+
`$ openapi-generator-cli generate -g erlang-server -i petstore.yaml -o ./http_server --additional-properties packageName=openapi`
34+
35+
3. Go into the `http_server` project folder
36+
37+
`$ cd http_server`
38+
39+
NOTE: The following generated files are now in the folder "http_server":
40+
41+
- `src/http_server*.erl`, `http_server.app.src` -- Erlang application modules generated by `rebar3`
42+
43+
- `src/openapi*.erl`, `openapi.app.src` -- REST API request handling modules generated by `openapi-generator-cli`
44+
45+
- `priv/openapi.json` -- OpenAPI data in JSON format created by `openapi-generator-cli`
1646

17-
1, Create an application by using rebar3
18-
$ rebar3 new app http_server
47+
- `rebar.config` -- Erlang project configuration file generated by `openapi-generator-cli`
1948

20-
2, Generate erlang-server project using openapi-generator
21-
https://github.com/OpenAPITools/openapi-generator#2---getting-started
49+
4. Add the following line to the `start/2` function in the `src/http_server_app.erl`:
2250

23-
3, Copy erlang-server file to http_server project, and don't forget the 'priv' folder.
51+
```erlang
52+
openapi_server:start(http_server,
53+
#{transport_opts => [{ip,{127,0,0,1}},
54+
{port,8080}
55+
]})
56+
```
2457

25-
4, Start in the http_server project:
26-
1, Introduce the following line in the http_server_app:start(_Type, _Args) function
27-
openapi_server:start(http_server, #{ip => {127,0,0,1}, port => 8080})
28-
2, Compile your http_server project
29-
$ rebar3 compile
30-
3, Start erlang virtual machine
31-
$ rebar3 shell
32-
4, Start project
33-
application:ensure_all_started(http_server).
58+
The updated `start/2` in `src/http_server_app.erl` should look like this:
59+
60+
```erlang
61+
start(_StartType, _StartArgs) ->
62+
openapi_server:start(http_server,
63+
#{transport_opts => [{ip,{127,0,0,1}},
64+
{port,8080}
65+
]}),
66+
http_server_sup:start_link().
67+
```
68+
69+
5. Update application configuration file `http_server.app.src` (in the `src` subfolder):
70+
71+
1. Copy application name from `http_server.app.src` to `openapi.app.src`
72+
73+
2. Copy `{mod,...}` rule from the `http_server.app.src` to `openapi.app.src`
74+
75+
3. Copy `openapi.app.src` over `http_server.app.src`
76+
77+
`$ cp src/openapi.app.src src/http_server.app.src`
78+
79+
4. Remove `openapi.app.src`
80+
81+
`$ rm src/openapi.app.src`
82+
83+
The updated `src/http_server.app.src` must be the only configuration file in the project and it should look like this:
84+
85+
```
86+
{application, http_server,
87+
[ {description, "This is a sample petstore server"},
88+
{vsn, "1.0.0"},
89+
{registered, []},
90+
{mod, {http_server_app, []}},
91+
{applications, [kernel, stdlib, public_key, ssl, inets, ranch, cowboy]},
92+
{env, []},
93+
{modules, []},
94+
{licenses, ["Apache-2.0"]},
95+
{links, []}
96+
]}.
97+
```
98+
99+
6. Compile your `http_server` project
100+
101+
`$ rebar3 compile`
102+
103+
7. Start Erlang virtual machine
104+
`$ rebar3 shell`
105+
106+
8. Start the application by running a following command in the `rebar3` shell
107+
108+
`1> application:ensure_all_started(http_server).`
109+
110+
Alternatively, you could start your application with the `rebar3` shell by adding the following lines to the `rebar.config`:
111+
```
112+
{shell, [
113+
{apps, [http_server]}
114+
]}.
115+
```
116+
117+
Note: If you need to repeat code generation using `openapi-generator-cli`, but don't want to rewrite changes in files made manually, you could use file `.openapi-generator-ignore` in the project root folder. For example, such `.openapi-generator-ignore` will preserve manual changes done in the file `rebar.conf` (Point 8)
118+
119+
```
120+
# OpenAPI Generator Ignore
121+
rebar.config
122+
```
34123
35124
To implement your own business logic, create a module called `http_server_logic` that implements the
36125
behaviour `openapi_logic_handler`. Refer to `openapi_logic_handler` documentation for details.
126+

0 commit comments

Comments
 (0)