swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.88k stars 6.03k forks source link

Simple API-Gateway codegen, suggestion #5034

Open ppKrauss opened 7 years ago

ppKrauss commented 7 years ago

Apache and Nginx have a lean language to express API Gateways,

Description

The "API Gateway" (at server-side) acts as a dedicated orchestration layer for all your backend APIs, to separate orchestration from implementation concerns. In this example the gateway is a Nginx script generated from the OpenAPI specifications of each microservice (adding some x- properties when need ... and perhaps some openapi-gateway.json file for complex cases).

EXAMPLE: extracted from here,

server {

   server_name petstore.swagger.io;
   root /var/www/petstore.swagger.io/html;

   # publishing by default the HTML for API description and related files for navigation
   index index.html index.htm;

   location / {
    try_files $uri $uri/ @proxy;
   }

   location @proxy {
    ### endpoints defined by OpenAPI spec of this app:

    rewrite    # endpoint "pets" for get,post
      ^/api/(pets?|darlings?)$
      http://localhost:3000/pets
      break;

    rewrite    # endpoint "pets/{id}" for get,delete
      ^/api/pets/([0-9]+)
      http://localhost:3000/pets?id=eq.$1
      break;

    # endpoint insects (automatic PostgREST) for get

    rewrite    # endpoint "fishes TO OTHER PROXY" for get
      ^/api/fishes
      http://localhost:4000
      break;

    ### default and auxiliar endpoint, for all other requests for PostgREST-queries
    rewrite     ^/api/(.*)$      /$1     break;

    ### proxy configurations:
    proxy_pass  http://localhost:3000;  # my PostREST is  here!
    ...
   }
}

It is obtained by tpl01-baseBreak template and petstore-expanded.json as input.

Swagger-codegen version

Suggestion to add Apache-rewrite and Nginx-rewrite languages as new targets in codegen.

Swagger declaration file content or url

See this this adapted petstore-expanded.json example: with x-rewrite_regex , x-proxy_url and new endpoint adictions. The specific mustache templates generates the code.

Related issues

It is a enhance suggestion... How to discuss this kind of suggestion?

wing328 commented 7 years ago

@ppKrauss thanks for the suggestion. I think it's a good idea 👍 and I'm sure developers are using Apache or Ngnix as API gateway would welcome the new generator to automate the config generation.

Let us know if you need help adding the new generators.

ppKrauss commented 7 years ago

Thanks @wing328 (!)... Well, I need to do some homework and review contrib guide... But, ... hum, yes I need help to adding the new generators.

wing328 commented 7 years ago

@ppKrauss sorry I missed your reply.

There are some client or server code here that is the "most similar" to my mustache needs?

I would suggest you start with the documentation generators, which generate only 1 or several files similar to configuration files that you want to generate.

How to, after git clone swagger-codegen, with simple command line (I have no Jave IDE!), in a UBUNTU 16 LTS, run the generator to do my tests?

Please do the following

git clone https://github.com/swagger-api/swagger-codegen
cd swagger-codegen
(make some changes)
mvn clean install -DskiptTests
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l new_generator_name