Deploying to GCP
aka Google Cloud Platform (App Engine)
即 Google Cloud Platform(App Engine)
PostGraphile CLI and CloudSQLPostGraphile CLI 与 CloudSQL
Section author: @redaikidoka
本节作者:@redaikidoka
Deploying PostGraphile with nothing more than command-line arguments to the cloud to serve between PostgreSQL hosted in Google Cloud SQL and an Angular App hosted in Google App Engine.
仅通过命令行参数将 PostGraphile 部署到云端,连接托管在 Google Cloud SQL 中的 PostgreSQL 数据库与托管在 Google App Engine 中的 Angular 应用。
Need to use cloud_sql_instances to connect to the PostgreSQL instance.
需要配置 cloud_sql_instances 来连接 PostgreSQL 实例。
Make sure you've got a project with your Cloud SQL PostgreSQL database, with the Google Cloud Admin SQL turned on, and App Engine turned on. Reserve the default service in App Engine for whatever your frontend is.
请确保你的 GCP 项目中已创建 Cloud SQL PostgreSQL 数据库,并已开启 Google Cloud Admin SQL 和 App Engine。同时,请将 App Engine 的默认服务留给前端应用。
Example deployment file app.yaml:
部署配置文件 app.yaml 示例:
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_settings,
cloud_sql_instances: webstr-dev-######:us-central1:webstr-dev=tcp:5432 tells
us that we are opening a unix pipe to a cloud instance in the GCP project, in
this case webstr-dev-###### in 'central region 1', connecting to Cloud SQL
instance websr-dev.
在 beta_settings 下,cloud_sql_instances: webstr-dev-######:us-central1:webstr-dev=tcp:5432 表示打开一条通往 GCP 云实例的 Unix 管道——本例中目标位于 webstr-dev-###### 项目的 us-central1 区域,连接到名为 websr-dev 的 Cloud SQL 实例。
-
The
=tcp:5432maps that unix socket to tcp port 5432.=tcp:5432用于将该 Unix socket 映射到 TCP 5432 端口。 -
I couldn't get using the unix port directly working, which is why the tcp port piece is in there
由于直接使用 Unix 端口未能跑通,这里才加上了 TCP 端口映射。
-
You can get the full instance name from your Cloud SQL Instance in the area of the Cloud SQL interface titled "Connect to this instance"
完整实例连接名可以在 Cloud SQL 控制台实例详情页的“Connect to this instance”区域中找到。
Create a graphile.config.mjs alongside your package.json so PostGraphile can
load the configuration automatically:
在 package.json 同级目录创建 graphile.config.mjs,以便 PostGraphile 自动加载配置:
import { PostGraphileAmberPreset } from "postgraphile/presets/amber";
import { makePgService } from "postgraphile/adaptors/pg";
export default {
extends: [PostGraphileAmberPreset],
pgServices: [makePgService({ connectionString: process.env.DATABASE_URL })],
grafserv: {
host: "0.0.0.0",
port: 8080,
graphqlPath: "/",
// Quick hack for development; use a proper CORS policy in production.
// dangerouslyAllowAllCORSRequests: true,
},
};
In package.json specify postgraphile, some project details, and the start
script. E.g.:
在 package.json 中声明 postgraphile 依赖、项目基本信息及 start 脚本,例如:
{
"name": "myprojectname",
"version": "1.0.0",
"scripts": {
"start": "postgraphile"
},
"engines": {
"node": ">=24"
},
"license": "ISC",
"dependencies": {
"postgraphile": "^5.0.0"
}
}
The project will end up at https://[project-name].appspot.com/
部署完成后,项目访问地址为 https://[project-name].appspot.com/
-
Your GraphQL endpoint will be that URL.
你的 GraphQL 端点即为该 URL。
-
You can access Ruru at
https://[project-name].appspot.com/graphiqlRuru 控制台可通过
https://[project-name].appspot.com/graphiql访问。
In graphile.config.mjs, the settings are:
graphile.config.mjs 中各项配置的含义如下:
-
grafserv.host: "0.0.0.0"allows GAE's nginx to successfully bind to the servicegrafserv.host: "0.0.0.0"允许 Google App Engine 的 Nginx 正常绑定并转发到该服务; -
grafserv.port: 8080binds to port 8080, which is a special port number that Google cloud will automatically expose via the service name, so you can access your PostGraphile service directly athttps://[project-name].appspot.com/grafserv.port: 8080监听 8080 端口,这是 Google Cloud 会通过服务名自动对外暴露的特定端口,因此你可以直接通过https://[project-name].appspot.com/访问 PostGraphile 服务; -
grafserv.graphqlPath: "/"puts the GraphQL endpoint at the root/(rather than/graphqlas is the default)grafserv.graphqlPath: "/"将 GraphQL 端点挂载在根路径/下(而非默认的/graphql)。
Deploying部署步骤
I deployed the project with gcloud.
我使用 gcloud 命令行工具完成部署:
I used gcloud init to setup my connection to my Google Cloud project.
首先执行 gcloud init 初始化并关联目标 Google Cloud 项目;
Then I used gcloud app deploy in this directory to push it up.
随后在该目录下运行 gcloud app deploy 将应用推送部署到云端。
Deploying an express app部署 Express 应用
Section author: @garcianavalon
本节作者:@garcianavalon
GCP configuration:
GCP 配置:
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
-
You will need
flexibleenvironment for websocket support (subscriptions). If you are not interested in real-time features you can usestandardenvironment and save some bucks. In that case, remove thebeta_settingssection如需支持 WebSocket(用于 GraphQL Subscription 实时订阅),必须使用
flexible环境;如果不需要实时功能,可以改用standard环境以节省成本,此时请移除beta_settings配置段。 -
This requires using postgraphile as a library. Minimum setup would be something like:
该方案需要将 PostGraphile 作为库嵌入代码中,最小目录结构如下:
/project
|--package.json
|--/src
|--index.js
Create the following files:
创建以下文件:
{
"scripts": {
"start": "node src/index.mjs"
}
}
import { PostGraphileAmberPreset } from "postgraphile/presets/amber";
import { makePgService } from "postgraphile/adaptors/pg";
export default {
extends: [PostGraphileAmberPreset],
pgServices: [
makePgService({
connectionString: process.env.DATABASE_URL,
}),
],
grafserv: {
host: "0.0.0.0",
port: 8080,
},
};
import express from "express";
import preset from "./graphile.config.mjs";
import { postgraphile } from "postgraphile";
import { grafserv } from "postgraphile/grafserv/express/v4";
const app = express();
const pgl = postgraphile(preset);
const serv = pgl.createServ(grafserv);
await serv.addTo(app);
app.listen(8080);
PostgreSQL authorization with Google Cloud SQLGoogle Cloud SQL 中的 PostgreSQL 权限配置
The postgres user on Google Cloud SQL is not a superuser, unlike the
Postgres user account you've likely been using in development. As such, if you
need it to be able to switch into a different role then you must grant that role
to the postgres user. For example, if you created the role anonymous in your
database, and you want the postgres role to be able to perform
SET LOCAL role TO anonymous; then you could run:
与本地开发环境常用的账号不同,Google Cloud SQL 中的 postgres 用户并不具备 superuser 权限。因此,如果需要让它切换到其他数据库角色,必须先将该角色授予 postgres 用户。例如,你在数据库中创建了 anonymous 角色,并希望 postgres 能够执行 SET LOCAL role TO anonymous;,则需要先运行:
GRANT anonymous TO postgres;
Helpful resources参考资源
See https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/hello-world/flexible for example Node.js project on GCP.
See information about configuring port forwarding: https://cloud.google.com/appengine/docs/flexible/custom-runtimes/configuring-your-app-with-app-yaml