All Projects → inaka → cowboy_swagger

inaka / cowboy_swagger

Licence: Apache-2.0 License
Swagger integration for Cowboy (built on trails)

Programming Languages

erlang
1774 projects
HTML
75241 projects
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to cowboy swagger

Spot
Spot is a concise, developer-friendly way to describe your API contract.
Stars: ✭ 230 (+111.01%)
Mutual labels:  swagger
Php Crud Api
Single file PHP script that adds a REST API to a SQL database
Stars: ✭ 2,904 (+2564.22%)
Mutual labels:  swagger
wadl2json
Convert a remote WADL file into a JSON equivalent
Stars: ✭ 14 (-87.16%)
Mutual labels:  swagger
Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+3063.3%)
Mutual labels:  swagger
Market monitor
💂 market monitor
Stars: ✭ 246 (+125.69%)
Mutual labels:  swagger
fhir-fuel.github.io
Place to prepare proposal to FHIR about JSON, JSON-Schema, Swagger/OpenAPI, JSON native databases and other JSON-frendly formats (yaml, edn, avro, protobuf etc) and technologies
Stars: ✭ 20 (-81.65%)
Mutual labels:  swagger
Aws Lambda Typescript
This sample uses the Serverless Application Framework to implement an AWS Lambda function in TypeScript, deploy it via CloudFormation, publish it through API Gateway to a custom domain registered on Route53, and document it with Swagger.
Stars: ✭ 228 (+109.17%)
Mutual labels:  swagger
symfony5-jwt-restapi
API for Meeting app development
Stars: ✭ 21 (-80.73%)
Mutual labels:  swagger
Shins
Shins development continues at
Stars: ✭ 250 (+129.36%)
Mutual labels:  swagger
shipengine-openapi
The official OpenAPI 3.0 definitions for ShipEngine™
Stars: ✭ 13 (-88.07%)
Mutual labels:  swagger
Grpc Swagger
Debugging gRPC application with swagger-ui.
Stars: ✭ 242 (+122.02%)
Mutual labels:  swagger
Api
The Up Banking API Specification
Stars: ✭ 248 (+127.52%)
Mutual labels:  swagger
JavaEE-projects
存放一些自己写的还有从不同开源社区fork下来的JavaEE项目,其中就不乏一些很多企业单位都在用的源码。
Stars: ✭ 43 (-60.55%)
Mutual labels:  swagger
Templates
.NET project templates with batteries included, providing the minimum amount of code required to get you going faster.
Stars: ✭ 2,864 (+2527.52%)
Mutual labels:  swagger
bibliothek
The PaperMC downloads API.
Stars: ✭ 13 (-88.07%)
Mutual labels:  swagger
Moreco
moreco 是一个能够为小、中、大型项目提供最合适架构的一条龙生态系统。满足项目从小型到中型至大型的衍变过程。从编码到监控至运维都满足、且各种功能都插件化,支持插件间的切换。支持Spring Boot、Spring Cloud、Axon 无缝升级
Stars: ✭ 231 (+111.93%)
Mutual labels:  swagger
Bus
Bus 是一个基础框架、服务套件,它基于Java8编写,参考、借鉴了大量已有框架、组件的设计,可以作为后端服务的开发基础中间件。代码简洁,架构清晰,非常适合学习使用。
Stars: ✭ 253 (+132.11%)
Mutual labels:  swagger
echoswagger
Swagger UI generator for Echo framework
Stars: ✭ 34 (-68.81%)
Mutual labels:  swagger
openapi-generator-for-spring
Open API v3 Generator for Spring Boot applications
Stars: ✭ 54 (-50.46%)
Mutual labels:  swagger
api
🚀 Automatic SDK generation from an OpenAPI definition
Stars: ✭ 127 (+16.51%)
Mutual labels:  swagger

cowboy-swagger

Swagger integration for Cowboy (built on trails).

build

Contact Us

If you find any bugs or have a problem while using this library, please open an issue in this repo (or a pull request :)).

Requirements

Cowboy Swagger requires Erlang 18+ after 0.1.0 version

Why Cowboy Swagger?

Simple, because there isn't a tool in Erlang to document Cowboy RESTful APIs easy and fast, and to improve development productivity.

With cowboy_swagger is possible to integrate Swagger to your Erlang projects that use Cowboy as a web server. It is extremely easy to use, and with just a few steps you'll have a nice Web documentation for your RESTful APIs.

To learn a bit more about Swagger, please check this blog post.

How to Use it?

This is the best part. It is extremely easy.

1. Document each Cowboy Handler

Because cowboy_swagger runs on top of trails, the first thing that you have to do is document all about your handler within the trails metadata. Keep in mind that all fields defined within each method into the metadata must be compliant with the Swagger specification.

For example, suppose that you have example_echo_handler, so it must implement the trails/0 callback from trails_handler behaviour:

trails() ->
  Metadata =
    #{get =>
      #{tags => ["echo"],
        description => "Gets echo var from the server",
        produces => ["text/plain"]
      },
      put =>
      #{tags => ["echo"],
        description => "Sets echo var in the server",
        produces => ["text/plain"],
        parameters => [
          #{name => <<"echo">>,
            description => <<"Echo message">>,
            in => <<"path">>,
            required => false,
            type => <<"string">>}
        ]
      }
    },
  [trails:trail("/message/[:echo]", example_echo_handler, [], Metadata)].

To get a better idea of how your handler should look like, please check here.

2. Include cowboy_swagger in your app

First, you need to include cowboy_swagger_handler module in your list of trails to be compiled.

% Include cowboy_swagger_handler in the trails list
Trails = trails:trails([example_echo_handler,
                        example_description_handler,
                        cowboy_swagger_handler]),
% store them
trails:store(Trails),
% and then compile them
Dispatch = trails:single_host_compile(Trails),

The snippet of code above is usually placed when you start cowboy. Check it here.

Then add cowboy_swagger to the list of apps to be loaded in your *.app.src file.

{application, example,
 [
  {description, "Cowboy Swagger Basic Example."},
  {vsn, "0.1"},
  {applications,
   [kernel,
    stdlib,
    jsx,
    cowboy,
    trails,
    cowboy_swagger
   ]},
  {modules, []},
  {mod, {example, []}},
  {registered, []},
  {start_phases, [{start_trails_http, []}]}
 ]
}.

And that's it, you got it. Now start your application and then you will have access to the API docs under the path /api-docs. Supposing that you're running the app on localhost:8080, that will be http://localhost:8080/api-docs.

Configuration

Additionally, cowboy_swagger can be configured/customized from a *.config file:

app.config

[
 %% Other apps ...

 %% cowboy_swagger config
 {cowboy_swagger,
  [
   %% `static_files`: Static content directory. This is where Swagger-UI
   %% is located. Default: `priv/swagger`.
   %% Remember that Swagger-UI is embedded into `cowboy-swagger` project,
   %% within `priv/swagger` folder. BUT you have to reference that path,
   %% and depending on how you're using `cowboy-swagger` it will be different.
   %% For example, assuming that you want to run your app which has
   %% `cowboy-swagger` as dependency from the console, `static_files` will be:
   {static_files, "./deps/cowboy_swagger/priv/swagger"},

   %% `global_spec`: Global fields for Swagger specification.
   %% If these fields are not set, `cowboy_swagger` will set default values.
   {global_spec,
    #{swagger => "2.0",
      info => #{title => "Example API"},
      basePath => "/api-docs"
     }
   }
  ]
 }
].

Definitions

Definitions can be used for describing parameters, responses and security schemas.

For adding definitions to your app, you have 2 choices:

  1. Add a definitions key to your cowboy_swagger global_spec map.
  2. Add them by calling cowboy_swagger:add_definition/2 and send the definition's name and properties.

Let's say you want to describe a POST call to a newspapers endpoint that requires name and description fields only, you can do it like this:

Option 1:

[ ... % other configurations
, { cowboy_swagger
  , [ { global_spec
      , #{ swagger => "2.0"
         , info => #{title => "My app API"}
         , definitions => #{
             "RequestBody" =>
               #{ "name" =>
                   #{ "type" => "string"
                    , "description" => "Newspaper name"
                    }
                , "description" =>
                    #{ "type" => "string"
                     , "description" => "Newspaper description"
                     }
                }
           }
         }
      }
    ]
  }
]

Option 2:

For the second choice, you can do it for example in one or several start_phases, directly in your handler or any other place you want.

-spec trails() -> trails:trails().
trails() ->
  DefinitionName = <<"RequestBody">>,
  DefinitionProperties =
    #{ <<"name">> =>
         #{ type => <<"string">>
          , description => <<"Newspaper name">>
          }
     , <<"description">> =>
         #{ type => <<"string">>
          , description => <<"Newspaper description">>
          }
     },
  % Add the definition
  ok = cowboy_swagger:add_definition(DefinitionName, DefinitionProperties),
  ...

Now in your handler's trails callback function you can use it:

...
  RequestBody =
    #{ name => <<"request body">>
     , in => body
     , description => <<"request body (as json)">>
     , required => true
       % Use the previously created `RequestBody' definition
     , schema => cowboy_swagger:schema(<<"RequestBody">>)
     },
  Metadata =
    #{ get =>
       #{ tags => ["newspapers"]
        , description => "Returns the list of newspapers"
        , produces => ["application/json"]
        }
     , post =>
       # { tags => ["newspapers"]
         , description => "Creates a new newspaper"
         , consumes => ["application/json"]
         , produces => ["application/json"]
         , parameters => [RequestBody] % and then use that parameter here
         }
     },
  Path = "/newspapers",
  Options = #{path => Path},
  [trails:trail(Path, newspapers_handler, Options, Metadata)].

What this does for you is add a nice response, parameter or security model in swagger-ui, so client developers will know exactly what parameters the API expects for every endpoint.

Example

For more information about cowboy_swagger and how to use it, please check this Example.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].