{"id":17178920,"url":"https://github.com/ppkrauss/postgrest-writeapi","last_synced_at":"2025-04-13T16:31:36.081Z","repository":{"id":150065868,"uuid":"84238291","full_name":"ppKrauss/PostgREST-writeAPI","owner":"ppKrauss","description":"Translate your OpenAPI specification into a NGinx config-file to implement your PostgREST system","archived":false,"fork":false,"pushed_at":"2017-03-26T11:43:46.000Z","size":1037,"stargazers_count":40,"open_issues_count":3,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-27T07:47:55.820Z","etag":null,"topics":["agile","nginx","openapi","postgresql","postgrest"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ppKrauss.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-07T19:34:37.000Z","updated_at":"2024-12-24T15:08:57.000Z","dependencies_parsed_at":"2023-04-28T20:32:43.199Z","dependency_job_id":null,"html_url":"https://github.com/ppKrauss/PostgREST-writeAPI","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppKrauss%2FPostgREST-writeAPI","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppKrauss%2FPostgREST-writeAPI/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppKrauss%2FPostgREST-writeAPI/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppKrauss%2FPostgREST-writeAPI/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ppKrauss","download_url":"https://codeload.github.com/ppKrauss/PostgREST-writeAPI/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248743825,"owners_count":21154748,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["agile","nginx","openapi","postgresql","postgrest"],"created_at":"2024-10-15T00:09:50.465Z","updated_at":"2025-04-13T16:31:35.043Z","avatar_url":"https://github.com/ppKrauss.png","language":"JavaScript","readme":"We use the best, [Nginx](https://nginx.org/) and [PostgREST](https://postgrest.com), to translate your [OpenAPI specification](https://www.openapis.org/specification) into a **plug-and-play [back-end](https://en.wikipedia.org/wiki/Front_and_back_ends)** system.\n\nThe project offers automatic generation of the main piece for implementation of  your *endpoints*, the *Nginx configuration script* \u0026mdash; that acts as a primary dispatcher or controller, for PostgREST and for your system. In the context of a \"microservices ecosystem\", this controller do the *microservices orchestration*.\n\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; ![](https://github.com/ppKrauss/my-sandbox/raw/master/OpenAPI-Gateway-codegen/assets/Reverse_proxy2.svg.png)\n\nTerminological note: usually this piece of software is named [*reverse proxy*](https://en.wikipedia.org/wiki/Reverse_proxy) or \"**API gateway**\".\n\n## Illustrating usage\n\nAfter check pre-requisites (NodeJS v4.2), `git clone` this repo, you can generate an Nginx config-file with\n```sh\ncd PostgREST-writeAPI\nnode writeApi.js --tpl=01 --api=petstore-expanded | more\n```\nEdit `api-spec/petstore-expanded.json` and `nginx-tpl/tpl01-baseBreak.mustache` to try variations.  What it do: transforms original *localhost endpoints* into specified endpoints. \n\nOriginal endpoint | Expected by [API-specification](api-spec/petstore-expanded.json)\n------------ | -------------\n`http://localhost:3000/pets` | `petstore.swagger.io/api/pets` or `petstore.swagger.io/api/darlings`\n`http://localhost:3000/pets?id=eq.`_id_ | `petstore.swagger.io/api/pets/{id}`\n\nSo *PostgREST-writeAPI* generates the code below, a [Nginx rewrite module script](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html) that implements the expected API\u0026#8209;gateway\u0026nbsp;specification:\n\n```sh\nserver {\n\n\tserver_name petstore.swagger.io;\n\troot /var/www/petstore.swagger.io/html;\n\n\t# publishing by default the HTML for API description and related files for navigation\n\tindex index.html index.htm;\n\n\tlocation / {\n\t\ttry_files $uri $uri/ @proxy;\n\t}\n\n\tlocation @proxy {\n\t\t### endpoints defined by OpenAPI spec of this app:\n\n\t\trewrite    # endpoint \"pets\" for get,post\n\t\t  ^/api/(pets?|darlings?)$\n\t\t  http://localhost:3000/pets\n\t\t  break;\n\n\t\trewrite    # endpoint \"pets/{id}\" for get,delete\n\t\t  ^/api/pets/([0-9]+)\n\t\t  http://localhost:3000/pets?id=eq.$1\n\t\t  break;\n\n\t\t# endpoint insects (automatic PostgREST) for get\n\t\t\n\t\trewrite    # endpoint \"fishes TO OTHER PROXY\" for get\n\t\t  ^/api/fishes\n\t\t  http://localhost:4000\n\t\t  break;\n\n\t\t### default and auxiliar endpoint, for all other requests for PostgREST-queries\n\t\trewrite     ^/api/(.*)$      /$1     break;\n\t\t\n\t\t### proxy configurations:\n\t\tproxy_pass  http://localhost:3000;  # my PostREST is  here!\n\t\t...\n\t}\n}\n```\nIt is obtained by [tpl01-baseBreak](nginx-tpl/tpl01-baseBreak.mustache) template and [this spec](api-spec/petstore-expanded.json) as input, as required by `--tpl=01 --api=petstore-expanded` options.\n\nVariations can be obtained changing the template. Example: to stop to use the *PostgREST queryes* (eg. `petstore.swagger.io/api/otherTable`) you can use `--tpl=02`  in the command line, that will generate script by [tpl02-baseBreak-noQuery](nginx-tpl/tpl02-baseBreak-noQuery.mustache) template, eliminating the last ` rewrite` clause.\n\n## Motivations\n\nPostgREST *endpoints* are ready-for-use, but, sometimes you can't reuse directally its \"complex URL\" endpoints, or need compliance with system requirements \u0026mdash; already expressed in an formal specification.\n \nThis project was developed to simplify this PostgREST use case: to obey the OpenAPI specification of your project.\n\n### Need for fast prototipation\n\nPostgreSQL is not an \"[agile](https://en.wikipedia.org/wiki/Agile_software_development) tool\"?  We can go from scratch to a set of endpoints serving a database, in minutes? We can use only the database, alone, to implement *web-application [prototypes](https://en.wikipedia.org/wiki/Software_prototyping)*?  \n\nWith PostgREST we can (!!) and the goal of this project is to expand the spectrum of applications. We can extend the PostgREST capabilties to \"plug-and-play\" endpoints, by its OpenAPI specifications.\n\n## OpenAPI addictions to specify your system\n\nYour application can use full stack PostgREST (the URL-SQLquery syntax) for default endpoints, \nand two new fields must be included in your OpenAPI spec (your `swagger.json`), to describe the endpoints with non-default PostgREST behaviour:\n\n* `x-rewrite_regex`: the regular expression that in fact represents the endpoint is a concatenation of *basePath*, *localPath* and *rewrite_regex*.\n\n* `x-rewrite_url`: the target-URL with optional regex-variables (eg. $1).\n\n* `x-proxy_url`: the URL of the \"root\" that is handling the `x-rewrite_url`, when is not PostgREST (the default proxy). \n\nWhen one or both are declared at JSON's API description, it is translated to Nginx *rewrite* directive. Else default PostgREST is adopted.\nThe `x-` prefix is the [\"vendor extension\" of OpenAPI](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#vendorExtensions).\n\n## Methodology\n\nAs the project is alpha version, have good methodology but not an automatic procedure. Simple steps:\n\n1. Prepare an OpenAPI specification of your system or application. Use basic PostgREST where is possible, where is not, two alternatives: rewrite to proxy-PostgREST or implement in it in other \"proxy\"... So, express also in the API specification the fields `x-proxy_url`, `x-rewrite_regex` and `x-rewrite_url`.\n\n2. Check what templete you need (or colabore creating a new one!). See [nginx-tpl](nginx-tpl) folder with some examples (or use template `01` as first try).\n\n3. Save your API specification (`swagger.json`)  at [api-spec](api-spec) with your project's name (eg. `myproj.json`), and edit it ading the fields xx and yy.\n\n4. run `node writeApi.js \u003e subdomain.conf` with correct parameters. It will generate the Nginx's script for your server's ` /etc/nginx/sites-available`.\n \n5. do next steps as usual Nginx implementation\n\n## OpenAPI stadanrds and parallel initiatives\n\nPlease help to start, at *Swagger-API* community, via [this *swagger-codegen* approved proposal](https://github.com/swagger-api/swagger-codegen/issues/5034), a new code generator, like here, but plugged into an ecosystem of standard code generators.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fppkrauss%2Fpostgrest-writeapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fppkrauss%2Fpostgrest-writeapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fppkrauss%2Fpostgrest-writeapi/lists"}