-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[erlang-server] Usage instructions for using erlang-server stubs missing #1281
Comments
FYI. There's an ongoing work to upgrade the code base to use Cowboy 2.x: #1305 |
@wing328 I think you selected the wrong tag. You selected |
Hi @aufziehvogel #1364 is an PR for upgrade 'erlang-server' from cowboy1.x to cowboy2.x. please help to code review, Thanks! |
@aufziehvogel let us know if you still need any help with the erlang-server generator. |
Disclaimer: I was told by William Cheng to cross-post this here. This is not a real bug report, but probably a request for documentation or clarification.
Description
I am currently trying to develop a REST API using Erlang. I used OpenAPI (Swagger) to define the API and wanted to use their openapi-generator to create the stubs and some helper code. This also works and some code is generated, but I cannot find any documentation on what I have to do from there.
For the explanation of my problem, I will refer to their sample output from code generation for erlang-server.
I have two main problems at the moment:
init/0
orstart/0
function)handle_request
, but the execution path seems a bit weird to me.Which function to implement
In openapi_router they define
openapi_pet_handler
to be the handler for'AddPet'
. In that module, there is a function calledhandle_request_json
, which tries to populate the request and in case of OK executesopenapi_logic_handler:handle_request(LogicHandler, ...)
.openapi_logic_handler:handle_request(Handler, ...)
takes that handler and executesHandler:handle_request(OperationID, Req, Context).
. This leads me to the conclusion, that I probably have to implementopenapi_pet_handler:handle_request
with my custom code.How to start the server
I checked the code and it seems to use
cowboy
as a web server. At least it includes dependencies tocowboy
in the generatedopenapi.app.src
. However, inrebar.config
there is no reference to cowboy.In the cowboy tutorial, they use erlang.mk to build their project. This allows them to run the code with
make run
. With rebar this does not seem to be possible. I did not find a specific command for rebar to start a program. You seem to have to know the entrypoint (init function) if you use rebar.However, I am not able to find any function that looks like it could be an init function in the generated stub code.
Summary
Can somebody explain what has to be done to use the stub generated by openapi-generator for erlang-server? Do I have to setup my own cowboy project and then somehow link the stubs into it? If yes, how?
Further Checks
I also did some further tests and research regarding the problem of starting the application, which I will explain in the section "Steps to reproduce". I will use your petstore erlang-server example for this, so that you can follow along and see whether I made any mistakes or not.
openapi-generator version
3.3.1: openapi-generator-cli-3.3.1.jar
OpenAPI declaration file content or url
Usually molescrape.yml, but to make it simpler for you I worked with openapi.yml (petstore).
Command line used for generation
For the example below, I directly used your generated code. That way it will be easier to follow along.
Steps to reproduce
So, first let's clone the repository:
We have the erlang-server code in
openapi-generator/samples/server/petstore/erlang-server
now. As far as I understand, this is an erlang application, with anopenapi.app(.src)
file. And it uses rebar for compilation. Thus, let's fetch the dependencies and compile the project.Gives us a failure in
jesse
, which is easy to fix.I just opened the file and fixed the deprecated code in line 92 to be newstyle stacktrace.
rebar compile
now works.According to my research, an Erlang Application must have an entrypoint file with a
start/2
and astop/1
method. If Igrep
the source directory of this code, there is nostart
method:Neither does the
openapi.app
file include themod
field that should link to the entrypoint module.Thus, I am not sure if this is supposed to be an Erlang application or not (I am quite new to Erlang). I guess that it can be an application if I want it to be and the stub generator just wants to give me freedom in this decision?
So, I decide this should be an application so that I can run it as a web server. And it this point it got lost in confusion after my research. The Erlang help references to a supervisor behaviour module, which looks very similar in its return type as the ranch Embedded Mode, which in turn uses the
ranch:child_spec
that is also produced byopenapi_server.erl
.Thus, I'd say let's implement an application module and a supervisor module.
And set the application module in
openapi.app.src
:Compile everything with
rebar compile
. And try to start it. For this, I start erlang witherl -pa ebin deps/*/ebin
and then execute there:This then gives me:
Okay, I think we're lacking a dependency here, so let's add it. I changed
rebar.config
to include:I chose version
1.1.2
here, because according to #1135, only Cowboy v1 is supported.And then again I try:
Resulting in:
Seems that my supervisor now tries to start
ranch
multiple times or something like that.I also tried starting all required modules one-by-one and then
application:start(openapi).
, this resulted in the same crash.And here I am stuck now. I do not know how I have to start the server with the
ranch:child_spec
or if I am trying the right thing at all and there does not seem to be documentation on how to use the generated stubs.Before I also reworked the code a bit so that it did not return the result of
ranch:child_spec
, but instead only the routing information forcowboy
and then I started cowboy as given in their tutorial. I think that this brought me to a parsing error withjsx
, which I then circumvented by commenting that part in the code. Then it started the server and crashed as soon as I tried to open a URL (unfortunately without any error). But I won't reproduce these steps here, because I think changing the stub code is not the intended way of using it.If you've reached this point at reading, please don't forget that starting the application is only the first part of the problem. The second one is knowing in which functions I have to implement my custom code.
Related issues/PRs
None.
Suggest a fix/enhancement
README.md
with instructions on usage and which functions have to be implemented with custom code.cowboy
inrebar.config
(at least that also seems to be missing, but I might be wrong)The text was updated successfully, but these errors were encountered: