|
| 1 | +# PostgREST Starter Kit |
| 2 | + |
| 3 | +Base project and tooling for authoring REST API backends with [PostgREST](https://postgrest.com). |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +## Purpose |
| 9 | + |
| 10 | +PostgREST enables a different way of building data driven API backends. It does "one thing well" and that is to provide you with a REST api over your database, however to build a complex production system that does things like talk to 3rd party systems, sends emails, implements real time updates for browsers, write integration tests, implement authentication, you need additional components. For this reason, some developers either submit feature requests that are not the scope of PostgREST or think of it just as a prototyping utility and not a powerful/flexible production component with excellent performance. This repository aims to be a starting point for all PostgREST based projects and bring all components together under a well defined structure. We also provide tooling that will aid you with iterating on your project and tools/scripts to enable a build pipeline to push everything to production. There are quite a few components in the stack but you can safely comment out pg_amqp_bridge/rabbitmq (or even openresty) instances in docker-compose.yml if you don't need the features/functionality they provide. |
| 11 | + |
| 12 | +## PostgREST+ as a service |
| 13 | +Run your PostgREST instance in [subZero cloud](https://subzero.cloud/postgrest-plus.html) and get additional features to the OS version ( [free plan](https://subzero.cloud/pricing.html) available). ALternatively, deploy an [enhanced version](https://docs.subzero.cloud/postgrest-plus/) of PostgREST on your infrastructure using binary and docker distributions. |
| 14 | + |
| 15 | +✓ **Fully Managed** — subZero automates every part of setup, running and scaling of PostgREST. Let your team focus on what they do best - building your product. Leave PostgREST management and monitoring to the experts.<br> |
| 16 | +✓ **Faster Queries** — Run an [enhanced PostgREST](https://docs.subzero.cloud/postgrest-plus/) version that uses prepared statements instead of inline queries. This results in up to 30% faster response times.<br> |
| 17 | +✓ **Custom Relations** — Define [custom relations](https://docs.subzero.cloud/postgrest-plus/) when automatic detection does not work. This allows you to use the powerful embedding feature even with the most complicated views<br> |
| 18 | +✓ **GraphQL API** — In addition to the REST API you get a GraphQL api with no additional coding. Leverage all the powerful tooling, libraries and integrations for GraphQL in your frontend.<br> |
| 19 | + |
| 20 | +## Features |
| 21 | + |
| 22 | +✓ Cross-platform development on macOS, Windows or Linux inside [Docker](https://www.docker.com/)<br> |
| 23 | +✓ [PostgreSQL](https://www.postgresql.org/) database schema boilerplate with authentication and authorization flow<br> |
| 24 | +✓ [OpenResty](https://openresty.org/en/) configuration files for the reverse proxy<br> |
| 25 | +✓ [RabbitMQ](https://www.rabbitmq.com/) integration through [pg-amqp-bridge](https://github.com/subzerocloud/pg-amqp-bridge)<br> |
| 26 | +✓ [Lua](https://www.lua.org/) functions to hook into each stage of the HTTP request and add custom logic (integrate 3rd party systems)<br> |
| 27 | +✓ Debugging and live code reloading (sql/configs/lua) functionality using [subzero-cli](https://github.com/subzerocloud/subzero-cli)<br> |
| 28 | +✓ Full migration management (migration files are automatically created) through [subzero-cli](https://github.com/subzerocloud/subzero-cli)/[sqitch](http://sqitch.org/)/[apgdiff](https://github.com/subzerocloud/apgdiff)<br> |
| 29 | +✓ SQL unit test using [pgTAP](http://pgtap.org/)<br> |
| 30 | +✓ Integration tests with [SuperTest / Mocha](https://github.com/visionmedia/supertest)<br> |
| 31 | +✓ Docker files for building production images<br> |
| 32 | +✓ Community support on [Slack](https://slack.subzero.cloud/)<br> |
| 33 | +✓ Compatible with [subZero Starter Kit](https://github.com/subzerocloud/subzero-starter-kit) if you need a GraphQL API and a few [more features](https://github.com/subzerocloud/subzero-starter-kit#features) with no additional work<br> |
| 34 | + |
| 35 | + |
| 36 | +## Directory Layout |
| 37 | + |
| 38 | +```bash |
| 39 | +. |
| 40 | +├── db # Database schema source files and tests |
| 41 | +│ └── src # Schema definition |
| 42 | +│ ├── api # Api entities avaiable as REST endpoints |
| 43 | +│ ├── data # Definition of source tables that hold the data |
| 44 | +│ ├── libs # A collection modules of used throughout the code |
| 45 | +│ ├── authorization # Application level roles and their privileges |
| 46 | +│ ├── sample_data # A few sample rows |
| 47 | +│ └── init.sql # Schema definition entry point |
| 48 | +├── openresty # Reverse proxy configurations and Lua code |
| 49 | +│ ├── lua # Application Lua code |
| 50 | +│ ├── nginx # Nginx configuration files |
| 51 | +│ ├── html # Static frontend files |
| 52 | +│ └── Dockerfile # Dockerfile definition for building production images |
| 53 | +├── tests # Tests for all the components |
| 54 | +│ ├── db # pgTap tests for the db |
| 55 | +│ └── rest # REST interface tests |
| 56 | +├── docker-compose.yml # Defines Docker services, networks and volumes |
| 57 | +└── .env # Project configurations |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +## Installation |
| 64 | + |
| 65 | +### Prerequisites |
| 66 | +* [Docker](https://www.docker.com) |
| 67 | +* [Node.js](https://nodejs.org/en/) |
| 68 | +* [subZero CLI](https://github.com/subzerocloud/subzero-cli#install) |
| 69 | + |
| 70 | +### Create a New Project |
| 71 | +subzero-cli provides you with a `base-project` command that lets you create a new project structure: |
| 72 | + |
| 73 | +```bash |
| 74 | +subzero base-project |
| 75 | + |
| 76 | +? Enter the directory path where you want to create the project . |
| 77 | +? Choose the starter kit (Use arrow keys) |
| 78 | + subzero-starter-kit (REST & GraphQL) |
| 79 | +❯ postgrest-starter-kit (REST) |
| 80 | +``` |
| 81 | + |
| 82 | +After the files have been created, you can bring up your application (API). |
| 83 | +In the root folder of application, run the docker-compose command |
| 84 | + |
| 85 | +```bash |
| 86 | +docker-compose up -d |
| 87 | +``` |
| 88 | + |
| 89 | +The API server will become available at the following endpoint: |
| 90 | + |
| 91 | +- REST [http://localhost:8080/api/](http://localhost:8080/api/) |
| 92 | + |
| 93 | +Try a simple request |
| 94 | + |
| 95 | +```bash |
| 96 | +curl http://localhost:8080/api/todos?select=id,todo |
| 97 | +``` |
| 98 | + |
| 99 | + |
| 100 | +## Development workflow and debugging |
| 101 | + |
| 102 | +Execute `subzero dashboard` in the root of your project.<br /> |
| 103 | +After this step you can view the logs of all the stack components (SQL queries will also be logged) and |
| 104 | +if you edit a sql/conf/lua file in your project, the changes will immediately be applied. |
| 105 | + |
| 106 | + |
| 107 | +## Testing |
| 108 | + |
| 109 | +The starter kit comes with a testing infrastructure setup. |
| 110 | +You can write pgTAP tests that run directly in your database, useful for testing the logic that resides in your database (user privileges, Row Level Security, stored procedures). |
| 111 | +Integration tests are written in JavaScript. |
| 112 | + |
| 113 | +Here is how you run them |
| 114 | + |
| 115 | +```bash |
| 116 | +npm install # Install test dependencies |
| 117 | +npm test # Run all tests (db, api) |
| 118 | +npm run test_db # Run pgTAP tests |
| 119 | +npm run test_api # Run integration tests |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | +## Deployment |
| 124 | +* [subZero Cloud](http://docs.subzero.cloud/production-infrastructure/subzero-cloud/) |
| 125 | +* [Amazon ECS+RDS](http://docs.subzero.cloud/production-infrastructure/aws-ecs-rds/) |
| 126 | +* [Amazon Fargate+RDS](http://docs.subzero.cloud/production-infrastructure/aws-fargate-rds/) |
| 127 | +* [Dedicated Linux Server](https://docs.subzero.cloud/production-infrastructure/ubuntu-server/) |
| 128 | + |
| 129 | +## Contributing |
| 130 | + |
| 131 | +Anyone and everyone is welcome to contribute. |
| 132 | + |
| 133 | +## Support and Documentation |
| 134 | +* [Documentation](https://docs.subzero.cloud/postgrest-starter-kit/) |
| 135 | +* [PostgREST API Referance](https://postgrest.com/en/stable/api.html) |
| 136 | +* [PostgreSQL Manual](https://www.postgresql.org/docs/current/static/index.html) |
| 137 | +* [Slack](https://slack.subzero.cloud/) — Get help, share ideas and feedback |
| 138 | +* [GitHub Issues](https://github.com/subzerocloud/postgrest-starter-kit/issues) — Check open issues, send feature requests |
| 139 | + |
| 140 | +## License |
| 141 | + |
| 142 | +Copyright © 2017-present subZero Cloud, LLC.<br /> |
| 143 | +This source code is licensed under [MIT](https://github.com/subzerocloud/postgrest-starter-kit/blob/master/LICENSE.txt) license<br /> |
| 144 | +The documentation to the project is licensed under the [CC BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/) license. |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +## CSRF prevention |
| 149 | +[OWASP CSRF cheatsheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#Checking_The_Referer_Header) |
0 commit comments