graphile / graphile.github.io

PostGraphile (PostGraphQL) and Graphile-Build website - contributions very welcome!
https://www.graphile.org/
26 stars 127 forks source link

WIP Google Cloud Platform deployment (App Engine) #161

Closed garcianavalon closed 5 years ago

garcianavalon commented 5 years ago

I'm on a rush and I haven't had time to finish a full guide, but as a quick reference for the future, this is the configuration required to run postgraphile on App Engine

runtime: nodejs
env: flex

env_variables:
  PGUSER: 'your-database-user'
  PGHOST: '/cloudsql/your-cloudsql-instance-connection-string'
  PGPASSWORD: 'your-password'
  PGDATABASE: 'your-database-name'

beta_settings:
  cloud_sql_instances: your-cloudsql-instance-connection-string
  1. You will need flexible environment for websocket support (subscriptions and live queries). If you are not interested in real-time features you can use standard environment and save some bucks. In that case, remove the beta_settings section
  2. This requires using postgraphile as a library. Minimum setup would be something like
/project
|--package.json
|--/src
   |--index.js

in package.json

{
  "scripts": {
     "start": "node src/index.js",
  }
}

In index.js

const express = require('express')
const { postgraphile } = require('postgraphile')

const app = express()

// POSTGRAPHILE CONF https://www.graphile.org/postgraphile/usage-library/
const pgConfig = {
  host: process.env.PGHOST || 'localhost',
  port: process.env.PGPORT || 5432,
  user: process.env.PGUSER,
  database: process.env.PGDATABASE,
  password: process.env.PGPASSWORD,
}
const postgraphileOptions = {
  // your stuff...
}
app.use(postgraphile(pgConfig, 'public', postgraphileOptions))

app.listen(8080)
benjie commented 5 years ago

See https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/hello-world/flexible for example Node.js project.

Not sure why (if) port 8080 works without configuration. See information about configuring port forwarding: https://cloud.google.com/appengine/docs/flexible/custom-runtimes/configuring-your-app-with-app-yaml

redaikidoka commented 5 years ago

All righty, well!

I just wanted to deploy postGraphile with nothing more than command-line arguments to the cloud to serve between my psql hosted in Google Cloud SQL and an Angular App hosted in Google App Engine.

In the past, I've used Google App Engine to host middle-tiers. After MUCH struggle, I figured out that I need to use cloud_sql_instances to connect to the psql instance, and that I needed to set the host and port in the command line.

Make sure you've got a project with your Cloud SQL postgres database, with the Google Cloud Admin SQL turned on, and App Engine turned on. Reserve the default service in App Engine for whatever your front-end is.

Here's my app.yaml, the deployment file:

beta_settings:
  cloud_sql_instances: webstr-dev-######:us-central1:webstr-dev=tcp:5432

# [START runtime]
runtime: nodejs
env: flex
threadsafe: yes
service: wgraphile

manual_scaling:
  instances: 1
resources:
 cpu: .5
 memory_gb: .5
 disk_size_gb: 10

health_check:
  enable_health_check: False

# [END runtime]

handlers:
  - url: /(.*)
    static_files: ./\1
    upload: ./(.*)

#  settings to keep gcloud from uploading files not required for deployment
skip_files:
  - ^node_modules$
  - ^README\..*
  - ^package-lock.json
  - \.gitignore
  - \.es*
  - ^\.git$
  - ^errors\.log

under beta_set

In my package.json, I specify postgraphile, and some project details, and the start script. For short:

{
  "name": "myprojectname",
  "version": "1.0.0",
  "scripts": {
    "start": "postgraphile --port 8080 -o --watch --enhance-graphiql -o -q / --host 0.0.0.0 -c postgres://user:password@172.17.0.1:5432/str_dev",

  },
  "engines": {
    "node": "^10.15",
    "npm": "^6.9"
  },
  "license": "ISC",
  "dependencies": {
    "postgraphile": "^4.4.3"
  }
}

The project will end up at https://[project-name].appspot.com/

Regarding the start command, the saving graces are:

DEPLOYING: I deployed the project with gcloud. I used gcloud init to setup my connection to my google cloud project. Then I used gcloud app deploy in this directory to push it up.

benjie commented 5 years ago

Thanks Pol!

redaikidoka commented 5 years ago

My pleasure! :) Maybe the website could have a section of tutorials to deploy postGraphile. Maybe for supporters? :)

On Wed, Sep 18, 2019 at 1:20 AM Benjie Gillam notifications@github.com wrote:

Thanks Pol!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/graphile/graphile.github.io/issues/161?email_source=notifications&email_token=ABE6KQOQYWNNSFW43EDU6UTQKHQEDA5CNFSM4IXN3G62YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD67HPUQ#issuecomment-532576210, or mute the thread https://github.com/notifications/unsubscribe-auth/ABE6KQPGHVB2DAKKI3M2LIDQKHQEDANCNFSM4IXN3G6Q .

--

-=-=-==-=-=-=-==-=-=-=-=-=-=-=-=-=- Pol Stafford Storyteller, Writer, Geek, Sensei

benjie commented 5 years ago

I was thinking we could add this to Guides and when we have enough of them we'd split to it's own section "deployment".

Screenshot_20190919_083043

redaikidoka commented 5 years ago

Sounds awesome! Would have helped me a ton! :) I spent 4 days = 32 hours = $4000 on this, so I'm guessing it's valuable. :)

benjie commented 5 years ago

I've shipped this; if someone wants to edit it please be my guest, it's just a hotchpotch of the above content for now:

https://github.com/graphile/graphile.github.io/blob/develop/src/pages/postgraphile/deploying-gcp.md

rentinity commented 4 years ago

Is there a way to run this on Cloud Run? i.e. a Cloud Run PostGraphile server on a Cloud Postgres instance

benjie commented 4 years ago

I doubt many people are following this issue; recommend you ask in our chat instead.

petrbela commented 2 years ago

Cloud Run works without any additional configuration, much simpler than App Engine. Just link to your github and let it deploy. Plus subscriptions (WebSockets) work out of the box, too.