Skip to content
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
11 changes: 7 additions & 4 deletions .github/workflows/run_test_cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ name: Run test cases

on: [push, pull_request]

jobs:
jobs:
run_test_cases:
runs-on: ubuntu-latest

container:
image: ghcr.io/emqx/emqx-builder/5.3-13:1.15.7-26.2.5.2-1-ubuntu22.04

steps:
- uses: actions/checkout@v4
- name: check formatting
run: make fmt-check
- name: run xref checks
run: make xref
- name: run test cases
run: |
make xref
make eunit
make ct
make cover
Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ REBAR := rebar3
.PHONY: all
all: compile

.PHONY: compile
compile:
$(REBAR) compile

Expand All @@ -25,10 +26,18 @@ eunit: compile
ct: compile
$(REBAR) as test ct -v

.PHONY: cover
cover:
$(REBAR) cover

.PHONY: dialyzer
dialyzer:
$(REBAR) dialyzer

.PHONY: fmt
fmt:
$(REBAR) fmt --verbose -w

.PHONY: fmt-check
fmt-check:
$(REBAR) fmt --verbose --check
73 changes: 38 additions & 35 deletions include/minirest.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,60 @@

-define(API_SPEC, api_spec).

-type path() :: string().
-type mete_data() :: map().
-type callback_function() :: atom().
-type path() :: string().
-type mete_data() :: map().
-type callback_function() :: atom().

-type api() :: {path(), mete_data(), callback_function()}.
-type apis() :: [api()].
-type api() :: {path(), mete_data(), callback_function()}.
-type apis() :: [api()].

-type api_schema() :: map().
-type api_schemas() :: [api_schema()].
-type api_schema() :: map().
-type api_schemas() :: [api_schema()].

-type api_spec() :: {apis(), api_schemas()}.
-type api_spec() :: {apis(), api_schemas()}.

-type http_method() :: get | post | put | head | delete | patch | options | connect | trace.
-type http_method() :: get | post | put | head | delete | patch | options | connect | trace.

-type status_code() :: integer().
-type headers() :: map()
| list().
-type status_code() :: integer().
-type headers() ::
map()
| list().

-type file_path() :: binary() | string().
-type file_name() :: binary() | string().
-type file_content() :: binary().
-type file_path() :: binary() | string().
-type file_name() :: binary() | string().
-type file_content() :: binary().

-type data_response_file() :: {file, file_path()} | {file_binary, file_name(), file_content()}.

-type form_data_key() :: atom() | binary() | string().
-type form_data_key() :: atom() | binary() | string().
-type form_data_value() :: atom() | binary() | string() | data_response_file().

-type form_data_part() :: {form_data_key(), form_data_value()}.
-type form_data_part() :: {form_data_key(), form_data_value()}.

-type response_body() :: binary()
| jsx:json_term()
| data_response_file()
| {form_data, [form_data_part()]}.
-type response_body() ::
binary()
| jsx:json_term()
| data_response_file()
| {form_data, [form_data_part()]}.

-type error_code() :: atom() | binary().
-type error_message() :: binary().
-type error_code() :: atom() | binary().
-type error_message() :: binary().

-type response() :: status_code()
| {status_code()}
| {status_code(), response_body()}
| {status_code(), headers(), response_body()}
| {status_code(), error_code(), error_message()}.
-type response() ::
status_code()
| {status_code()}
| {status_code(), response_body()}
| {status_code(), headers(), response_body()}
| {status_code(), error_code(), error_message()}.

-record(handler, {
method :: http_method(),
module :: atom(),
function :: atom(),
filter :: fun(),
authorization :: {Module :: atom(), Function :: atom()} | undefined,
log_meta :: map(),
error_codes :: list(error_code())
method :: http_method(),
module :: atom(),
function :: atom(),
filter :: fun(),
authorization :: {Module :: atom(), Function :: atom()} | undefined,
log_meta :: map(),
error_codes :: list(error_code())
}).

-type handler() :: #handler{}.
Expand Down
127 changes: 63 additions & 64 deletions include/minirest_http.hrl
Original file line number Diff line number Diff line change
@@ -1,68 +1,67 @@
-define(RESPONSE_CODE_CONTINUE, 100).
-define(RESPONSE_CODE_SWITCHING_PROTOCOLS, 101).
-define(RESPONSE_CODE_EARLY_HINTS, 103).
-define(RESPONSE_CODE_OK, 200).
-define(RESPONSE_CODE_CREATED, 201).
-define(RESPONSE_CODE_ACCEPTED, 202).
-define(RESPONSE_CODE_NON_AUTHORITATIVE_INFORMATION, 203).
-define(RESPONSE_CODE_NO_CONTENT, 204).
-define(RESPONSE_CODE_RESET_CONTENT, 205).
-define(RESPONSE_CODE_PARTIAL_CONTENT, 206).
-define(RESPONSE_CODE_MULTIPLE_CHOICES, 300).
-define(RESPONSE_CODE_MOVED_PERMANENTLY, 301).
-define(RESPONSE_CODE_FOUND, 302).
-define(RESPONSE_CODE_SEE_OTHER, 303).
-define(RESPONSE_CODE_NOT_MODIFIED, 304).
-define(RESPONSE_CODE_TEMPORARY_REDIRECT, 307).
-define(RESPONSE_CODE_PERMANENT_REDIRECT, 308).
-define(RESPONSE_CODE_BAD_REQUEST, 400).
-define(RESPONSE_CODE_UNAUTHORIZED, 401).
-define(RESPONSE_CODE_PAYMENT_REQUIRED, 402).
-define(RESPONSE_CODE_FORBIDDEN, 403).
-define(RESPONSE_CODE_NOT_FOUND, 404).
-define(RESPONSE_CODE_METHOD_NOT_ALLOWED, 405).
-define(RESPONSE_CODE_NOT_ACCEPTABLE, 406).
-define(RESPONSE_CODE_PROXY_AUTHENTICATION_REQUIRED, 407).
-define(RESPONSE_CODE_REQUEST_TIMEOUT, 408).
-define(RESPONSE_CODE_CONFLICT, 409).
-define(RESPONSE_CODE_GONE, 410).
-define(RESPONSE_CODE_LENGTH_REQUIRED, 411).
-define(RESPONSE_CODE_PRECONDITION_FAILED, 412).
-define(RESPONSE_CODE_PAYLOAD_TOO_LARGE, 413).
-define(RESPONSE_CODE_URI_TOO_LONG, 414).
-define(RESPONSE_CODE_UNSUPPORTED_MEDIA_TYPE, 415).
-define(RESPONSE_CODE_RANGE_NOT_SATISFIABLE, 416).
-define(RESPONSE_CODE_EXPECTATION_FAILED, 417).
-define(RESPONSE_CODE_IM_A_TEAPOT, 418).
-define(RESPONSE_CODE_UNPROCESSABLE_ENTITY, 422).
-define(RESPONSE_CODE_TOO_EARLY, 425).
-define(RESPONSE_CODE_UPGRADE_REQUIRED, 426).
-define(RESPONSE_CODE_PRECONDITION_REQUIRED, 428).
-define(RESPONSE_CODE_TOO_MANY_REQUESTS, 429).
-define(RESPONSE_CODE_REQUEST_HEADER_FIELDS_TOO_LARGE, 431).
-define(RESPONSE_CODE_UNAVAILABLE_FOR_LEGAL_REASONS, 451).
-define(RESPONSE_CODE_INTERNAL_SERVER_ERROR, 500).
-define(RESPONSE_CODE_NOT_IMPLEMENTED, 501).
-define(RESPONSE_CODE_BAD_GATEWAY, 502).
-define(RESPONSE_CODE_SERVICE_UNAVAILABLE, 503).
-define(RESPONSE_CODE_GATEWAY_TIMEOUT, 504).
-define(RESPONSE_CODE_HTTP_VERSION_NOT_SUPPORTED, 505).
-define(RESPONSE_CODE_VARIANT_ALSO_NEGOTIATES, 506).
-define(RESPONSE_CODE_INSUFFICIENT_STORAGE, 507).
-define(RESPONSE_CODE_LOOP_DETECTED, 508).
-define(RESPONSE_CODE_NOT_EXTENDED, 510).
-define(RESPONSE_CODE_NETWORK_AUTHENTICATION_REQUIRED, 511).

-define(RESPONSE_CODE_CONTINUE, 100).
-define(RESPONSE_CODE_SWITCHING_PROTOCOLS, 101).
-define(RESPONSE_CODE_EARLY_HINTS, 103).
-define(RESPONSE_CODE_OK, 200).
-define(RESPONSE_CODE_CREATED, 201).
-define(RESPONSE_CODE_ACCEPTED, 202).
-define(RESPONSE_CODE_NON_AUTHORITATIVE_INFORMATION, 203).
-define(RESPONSE_CODE_NO_CONTENT, 204).
-define(RESPONSE_CODE_RESET_CONTENT, 205).
-define(RESPONSE_CODE_PARTIAL_CONTENT, 206).
-define(RESPONSE_CODE_MULTIPLE_CHOICES, 300).
-define(RESPONSE_CODE_MOVED_PERMANENTLY, 301).
-define(RESPONSE_CODE_FOUND, 302).
-define(RESPONSE_CODE_SEE_OTHER, 303).
-define(RESPONSE_CODE_NOT_MODIFIED, 304).
-define(RESPONSE_CODE_TEMPORARY_REDIRECT, 307).
-define(RESPONSE_CODE_PERMANENT_REDIRECT, 308).
-define(RESPONSE_CODE_BAD_REQUEST, 400).
-define(RESPONSE_CODE_UNAUTHORIZED, 401).
-define(RESPONSE_CODE_PAYMENT_REQUIRED, 402).
-define(RESPONSE_CODE_FORBIDDEN, 403).
-define(RESPONSE_CODE_NOT_FOUND, 404).
-define(RESPONSE_CODE_METHOD_NOT_ALLOWED, 405).
-define(RESPONSE_CODE_NOT_ACCEPTABLE, 406).
-define(RESPONSE_CODE_PROXY_AUTHENTICATION_REQUIRED, 407).
-define(RESPONSE_CODE_REQUEST_TIMEOUT, 408).
-define(RESPONSE_CODE_CONFLICT, 409).
-define(RESPONSE_CODE_GONE, 410).
-define(RESPONSE_CODE_LENGTH_REQUIRED, 411).
-define(RESPONSE_CODE_PRECONDITION_FAILED, 412).
-define(RESPONSE_CODE_PAYLOAD_TOO_LARGE, 413).
-define(RESPONSE_CODE_URI_TOO_LONG, 414).
-define(RESPONSE_CODE_UNSUPPORTED_MEDIA_TYPE, 415).
-define(RESPONSE_CODE_RANGE_NOT_SATISFIABLE, 416).
-define(RESPONSE_CODE_EXPECTATION_FAILED, 417).
-define(RESPONSE_CODE_IM_A_TEAPOT, 418).
-define(RESPONSE_CODE_UNPROCESSABLE_ENTITY, 422).
-define(RESPONSE_CODE_TOO_EARLY, 425).
-define(RESPONSE_CODE_UPGRADE_REQUIRED, 426).
-define(RESPONSE_CODE_PRECONDITION_REQUIRED, 428).
-define(RESPONSE_CODE_TOO_MANY_REQUESTS, 429).
-define(RESPONSE_CODE_REQUEST_HEADER_FIELDS_TOO_LARGE, 431).
-define(RESPONSE_CODE_UNAVAILABLE_FOR_LEGAL_REASONS, 451).
-define(RESPONSE_CODE_INTERNAL_SERVER_ERROR, 500).
-define(RESPONSE_CODE_NOT_IMPLEMENTED, 501).
-define(RESPONSE_CODE_BAD_GATEWAY, 502).
-define(RESPONSE_CODE_SERVICE_UNAVAILABLE, 503).
-define(RESPONSE_CODE_GATEWAY_TIMEOUT, 504).
-define(RESPONSE_CODE_HTTP_VERSION_NOT_SUPPORTED, 505).
-define(RESPONSE_CODE_VARIANT_ALSO_NEGOTIATES, 506).
-define(RESPONSE_CODE_INSUFFICIENT_STORAGE, 507).
-define(RESPONSE_CODE_LOOP_DETECTED, 508).
-define(RESPONSE_CODE_NOT_EXTENDED, 510).
-define(RESPONSE_CODE_NETWORK_AUTHENTICATION_REQUIRED, 511).

-define(DEFAULT_RESPONSE_HEADERS, #{<<"content-type">> => <<"application/json">>}).

-define(FILE_CONTENT_TYPE_MAP, #{
<<".html">> => <<"text/html">>,
<<".config">> => <<"application/octet-stream">>,
<<".log">> => <<"application/octet-stream">>,
<<".json">> => <<"application/json">>,
<<".zip">> => <<"application/zip">>,
<<".txt">> => <<"text/plain">>,
<<".gif">> => <<"image/gif">>,
<<".jpeg">> => <<"image/jpeg">>
}).
<<".html">> => <<"text/html">>,
<<".config">> => <<"application/octet-stream">>,
<<".log">> => <<"application/octet-stream">>,
<<".json">> => <<"application/json">>,
<<".zip">> => <<"application/zip">>,
<<".txt">> => <<"text/plain">>,
<<".gif">> => <<"image/gif">>,
<<".jpeg">> => <<"image/jpeg">>
}).
34 changes: 20 additions & 14 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@
{cowboy_swagger, {git, "https://github.com/inaka/cowboy_swagger", {tag, "2.7.0"}}}
]}.

{plugins, [
{erlfmt, "1.6.0"}
]}.

{edoc_opts, [{preprocess, true}]}.

{erl_opts, [warn_unused_vars,
warn_shadow_vars,
warn_unused_import,
warn_obsolete_guard,
debug_info,
{d, 'APPLICATION', minirest}
]}.
{erl_opts, [
warn_unused_vars,
warn_shadow_vars,
warn_unused_import,
warn_obsolete_guard,
debug_info,
{d, 'APPLICATION', minirest}
]}.

{xref_checks, [undefined_function_calls,
undefined_functions,
locals_not_used,
deprecated_function_calls,
warnings_as_errors,
deprecated_functions
]}.
{xref_checks, [
undefined_function_calls,
undefined_functions,
locals_not_used,
deprecated_function_calls,
warnings_as_errors,
deprecated_functions
]}.

{cover_enabled, true}.
{cover_opts, [verbose]}.
Expand Down
29 changes: 14 additions & 15 deletions src/minirest.app.src
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{application, minirest,
[{description, "A mini RESTful API framework built on cowboy and swagger"},
{vsn, "git"},
{registered, []},
{applications,
[ kernel
, stdlib
, cowboy_swagger
]},
{env,[]},
{modules, []},
{licenses, ["Apache 2.0"]},
{links, ["Github", "https://github.com/emqx/minirest"]}
]
}.
{application, minirest, [
{description, "A mini RESTful API framework built on cowboy and swagger"},
{vsn, "git"},
{registered, []},
{applications, [
kernel,
stdlib,
cowboy_swagger
]},
{env, []},
{modules, []},
{licenses, ["Apache 2.0"]},
{links, ["Github", "https://github.com/emqx/minirest"]}
]}.
Loading