Skip to content
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 overhaul #19465

Merged
merged 12 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .github/workflows/samples-erlang.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ name: Samples Erlang
on:
push:
paths:
# comment out due to errors
# ===> Compiling src/openapi_pet_handler.erl failed
# src/openapi_pet_handler.erl:278: function is_authorized/2 already defined
#- samples/server/petstore/erlang-server/**
- samples/server/echo_api/erlang-server/**
- samples/server/petstore/erlang-server/**
- samples/client/petstore/erlang-client/**
- samples/client/petstore/erlang-proper/**
pull_request:
paths:
#- samples/server/petstore/erlang-server/**
- samples/server/echo_api/erlang-server/**
- samples/server/petstore/erlang-server/**
- samples/client/petstore/erlang-client/**
- samples/client/petstore/erlang-proper/**
jobs:
build:
name: Build Erlang projects
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
sample:
#- samples/server/petstore/erlang-server/
- samples/server/echo_api/erlang-server/
- samples/server/petstore/erlang-server/
- samples/client/petstore/erlang-client/
- samples/client/petstore/erlang-proper/
steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
otp-version: '22.2'
rebar3-version: '3.14.3'
otp-version: '27'
rebar3-version: '3.23.0'
- run: rebar3 compile
working-directory: ${{ matrix.sample }}
4 changes: 4 additions & 0 deletions bin/configs/erlang-server-echo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
generatorName: erlang-server
outputDir: samples/server/echo_api/erlang-server
inputSpec: modules/openapi-generator/src/test/resources/3_0/echo_api.yaml
templateDir: modules/openapi-generator/src/main/resources/erlang-server
1 change: 1 addition & 0 deletions docs/generators/erlang-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>fun</li>
<li>if</li>
<li>let</li>
<li>maybe</li>
<li>not</li>
<li>of</li>
<li>or</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public ErlangServerCodegen() {
*/
setReservedWordsLowerCase(
Arrays.asList(
"after", "and", "andalso", "band", "begin", "bnot", "bor", "bsl", "bsr", "bxor", "case",
"catch", "cond", "div", "end", "fun", "if", "let", "not", "of", "or", "orelse", "receive",
"rem", "try", "when", "xor"
"after", "and", "andalso", "band", "begin", "bnot", "bor", "bsl", "bsr", "bxor",
"case", "catch", "cond", "div", "end", "fun", "if", "let", "maybe", "not",
"of", "or", "orelse", "receive", "rem", "try", "when", "xor"
)
);

Expand Down Expand Up @@ -172,10 +172,8 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("router.mustache", "", toSourceFilePath("router", "erl")));
supportingFiles.add(new SupportingFile("api.mustache", "", toSourceFilePath("api", "erl")));
supportingFiles.add(new SupportingFile("server.mustache", "", toSourceFilePath("server", "erl")));
supportingFiles.add(new SupportingFile("utils.mustache", "", toSourceFilePath("utils", "erl")));
supportingFiles.add(new SupportingFile("auth.mustache", "", toSourceFilePath("auth", "erl")));
supportingFiles.add(new SupportingFile("openapi.mustache", "", toPrivFilePath(this.openApiSpecName, "json")));
supportingFiles.add(new SupportingFile("default_logic_handler.mustache", "", toSourceFilePath("default_logic_handler", "erl")));
supportingFiles.add(new SupportingFile("logic_handler.mustache", "", toSourceFilePath("logic_handler", "erl")));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")
.doNotOverwrite());
Expand Down Expand Up @@ -223,7 +221,7 @@ public String getHelp() {
@Override
public String toApiName(String name) {
if (name.length() == 0) {
return this.packageName + "_default_handler";
return this.packageName + "_handler";
}
return this.packageName + "_" + underscore(name) + "_handler";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,33 @@

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

Dependency: [Cowboy](https://github.com/ninenines/cowboy)
Dependencies: Erlang OTP/27 and rebar3. Also:
- [Cowboy](https://hex.pm/packages/cowboy)
- [Ranch](https://hex.pm/packages/ranch)
- [Jesse](https://hex.pm/packages/jesse)

## Prerequisites

TODO

## Getting started
Use erlang-server with erlang.mk

1, Create an application by using erlang.mk
$ mkdir http_server
$ cd http_server
$ wget https://erlang.mk/erlang.mk
$ make -f erlang.mk bootstrap bootstrap-rel
$ make run

2, Modify the Makefile in the http_server directory to the following to introduce the dependency library:
PROJECT = http_server
PROJECT_DESCRIPTION = New project
PROJECT_VERSION = 0.1.0

DEPS = cowboy jesse jsx
dep_cowboy_commit = 2.5.0
dep_jesse_commit = 1.5.2
dep_jsx_commit = 2.9.0
DEP_PLUGINS = cowboy jesse jsx

PACKAGES += rfc3339
pkg_rfc3339_name = rfc3339
pkg_rfc3339_description = an erlang/elixir rfc3339 lib
pkg_rfc3339_homepage = https://github.com/talentdeficit/rfc3339
pkg_rfc3339_fetch = git
pkg_rfc3339_repo = https://github.com/talentdeficit/rfc3339
pkg_rfc3339_commit = master

include erlang.mk

3, Generate erlang-server project using openapi-generator
Use erlang-server with rebar3

1, Create an application by using rebar3
$ rebar3 new app http_server

2, Generate erlang-server project using openapi-generator
https://github.com/OpenAPITools/openapi-generator#2---getting-started

4, Copy erlang-server file to http_server project,Don't forget the 'priv' folder.
3, Copy erlang-server file to http_server project, and don't forget the 'priv' folder.

5, Start in the http_server project:
4, Start in the http_server project:
1, Introduce the following line in the http_server_app:start(_Type, _Args) function
openapi_server:start(http_server, #{ip=>{127,0,0,1}, port=>8080, net_opts=>[]})
2, Compilation http_server project
$ make
openapi_server:start(http_server, #{ip => {127,0,0,1}, port => 8080})
2, Compile your http_server project
$ rebar3 compile
3, Start erlang virtual machine
$erl -pa ./deps/cowboy/ebin -pa ./deps/cowlib/ebin -pa ./deps/ranch/ebin -pa ./deps/jsx/ebin -pa ./deps/jesse/ebin -pa ./deps/rfc3339/ebin -pa ./ebin
$ rebar3 shell
4, Start project
application:ensure_all_started(http_server).

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.
Loading
Loading