GithubHelp home page GithubHelp logo

isabella232 / postgres-postgrest-cloudflared-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cloudflare/postgres-postgrest-cloudflared-example

0.0 0.0 0.0 4 KB

Create a PostgreSQL database with a REST API, exposed to the internet securely with Cloudflare Tunnel

License: MIT License

postgres-postgrest-cloudflared-example's Introduction

Postgrest Docker example

An example docker-compose setup for configuring a PostgreSQL database and the associated PostgREST endpoint, which is exposed to the internet using cloudflared.

Initialize the containers

You can start up all the services in this project by running docker-compose up:

$ docker-compose up -d

Note: depending on how your Docker installation is set up, you may need to preface the above command (and any other docker-compose examples) with sudo.

Example data

You can generate some example data by running the create-example-data.sql script inside of your running docker container:

$ docker-compose exec postgres psql -U user -d db -f /scripts/create-example-data.sql 

This will create a users table and a single user with the name Kristian.

Making requests

A cloudflared tunnel will be generated when you run docker-compose up, such as https://honolulu-devices-update-drink.trycloudflare.com. You can find the currently active tunnel URL by looking through the docker-compose logs:

$ docker-compose logs cloudflared | grep trycloudflare
cloudflared_1 | INFO[2021-06-14T17:26:05Z] | https://honolulu-devices-update-drink.trycloudflare.com

This tunnel allows access to the PostgREST endpoint. You can make a few example requests to see data correctly returned from PostgreSQL to PostgREST, and then to your terminal:

$ curl https://honolulu-devices-update-drink.trycloudflare.com/users
[{"id":1,"name":"Kristian"}]

$ curl https://honolulu-devices-update-drink.trycloudflare.com/users?id=eq.1
[{"id":1,"name":"Kristian"}]

$ curl https://honolulu-devices-update-drink.trycloudflare.com/users?id=eq.2
[]

You can also create records using the PostgREST API, though you should go through the PostgREST tutorial on user authentication to secure your API:

$ curl https://honolulu-devices-update-drink.trycloudflare.com/users \
  -X POST \
  -H "Content-type: application/json" \
  -d '{"name": "Dog"}'

$ curl https://honolulu-devices-update-drink.trycloudflare.com/users?name=eq.Dog
[{"id":2, "name":"Dog"}]

Tunnel configuration

If you'd like to create a permanent cloudflared tunnel at a custom endpoint, you can configure your tunnel. The provided cloudflared directory will be exposed to the Docker container, so you can add config.yml and any relevant JSON/certificate files to authenticate and configure your tunnel.

Thanks

This repository and the (soon-to-come) tutorial for integrating with this on Cloudflare Workers was inspired by @eidam's great tutorial and sample codebase for running an authenticated and secure PostgreSQL, PostgREST, and cloudflared integration on Google Cloud, available here.

postgres-postgrest-cloudflared-example's People

Contributors

codewithkristian avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.