* Revert "v7.12.0 release" This reverts commit 073723cb4d41187f839fbb46565d109293fa22d7. * set version to v7.13.0-SNAPSHOT * update samples * update doc
OpenAPI server library for Erlang
Overview
An Erlang server stub generated by OpenAPI Generator given an OpenAPI spec.
Prerequisites
-
Erlang libraries:
-
OpenAPI generator script
openapi-generator-cli(for more information see OpenAPI Generator - Getting Started ) -
OpenAPI specification file in the current folder (for example petstore.yaml)
Getting started
Use erlang-server with rebar3
-
Create a folder with an Erlang application by using
rebar3$ rebar3 new app http_server -
Generate OpenAPI
erlang-serverproject usingopenapi-generator$ openapi-generator-cli generate -g erlang-server -i petstore.yaml -o ./http_server --additional-properties packageName=openapi -
Go into the
http_serverproject folder$ cd http_serverNOTE: The following generated files are now in the folder "http_server":
-
src/http_server*.erl,http_server.app.src-- Erlang application modules generated byrebar3 -
src/openapi*.erl,openapi.app.src-- REST API request handling modules generated byopenapi-generator-cli -
priv/openapi.json-- OpenAPI data in JSON format created byopenapi-generator-cli -
rebar.config-- Erlang project configuration file generated byopenapi-generator-cli
-
-
Add the following line to the
start/2function in thesrc/http_server_app.erl:
openapi_server:start(http_server,
#{transport_opts => [{ip,{127,0,0,1}},
{port,8080}
]})
The updated start/2 in src/http_server_app.erl should look like this:
start(_StartType, _StartArgs) ->
openapi_server:start(http_server,
#{transport_opts => [{ip,{127,0,0,1}},
{port,8080}
]}),
http_server_sup:start_link().
-
Update application configuration file
http_server.app.src(in thesrcsubfolder):-
Copy application name from
http_server.app.srctoopenapi.app.src -
Copy
{mod,...}rule from thehttp_server.app.srctoopenapi.app.src -
Copy
openapi.app.srcoverhttp_server.app.src$ cp src/openapi.app.src src/http_server.app.src -
Remove
openapi.app.src$ rm src/openapi.app.src
-
The updated src/http_server.app.src must be the only configuration file in the project and it should look like this:
{application, http_server,
[ {description, "This is a sample petstore server"},
{vsn, "1.0.0"},
{registered, []},
{mod, {http_server_app, []}},
{applications, [kernel, stdlib, public_key, ssl, inets, ranch, cowboy]},
{env, []},
{modules, []},
{licenses, ["Apache-2.0"]},
{links, []}
]}.
-
Compile your
http_serverproject$ rebar3 compile -
Start Erlang virtual machine
$ rebar3 shell -
Start the application by running a following command in the
rebar3shell1> application:ensure_all_started(http_server).Alternatively, you could start your application with the
rebar3shell by adding the following lines to therebar.config:{shell, [ {apps, [http_server]} ]}.
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)
# OpenAPI Generator Ignore
rebar.config
To implement your own business logic, create a module called http_server_logic that implements the
behaviour openapi_logic_handler. Refer to openapi_logic_handler documentation for details.