-
Notifications
You must be signed in to change notification settings - Fork 1k
Add samples for CloudSQL postgres #390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e7655ea
initial commit for CloudSQL postgres
bshaffer b76d7a4
Proper namespace for Exception thrown in ProjectIdTrait (#389)
bshaffer fa01cb6
Merge branch 'add-postgres-to-flex-cloudsql' of https://github.com/Go…
ryanmats 053930d
Created MySQL / Postgres samples and made sure tests work
ryanmats 76264ca
Fixed code review and linting issues
ryanmats 3aca294
Renamed CLOUDSQL_CONNECTION_NAME for Travis
ryanmats File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # Cloud SQL & Google App Engine Flexible Environment | ||
|
|
||
| This sample application demonstrates how to use [Cloud SQL with Google App Engine Flexible Environment](https://cloud.google.com/appengine/docs/flexible/php/using-cloud-sql). | ||
|
|
||
| ## Setup | ||
|
|
||
| Before running this sample: | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Install [`composer`](https://getcomposer.org) | ||
| - Install dependencies by running: | ||
|
|
||
| ```sh | ||
| composer install | ||
| ``` | ||
|
|
||
| ## Setup | ||
|
|
||
| Before you can run or deploy the sample, you will need to do the following: | ||
|
|
||
| 1. Create a [Second Generation Cloud SQL](https://cloud.google.com/sql/docs/create-instance) instance. You can do this from the [Cloud Console](https://console.developers.google.com) or via the [Cloud SDK](https://cloud.google.com/sdk). To create it via the SDK use the following command: | ||
|
|
||
| $ gcloud beta sql instances create YOUR_INSTANCE_NAME --tier=db-f1-micro --activation-policy=ALWAYS | ||
|
|
||
| > Note: the `--tier` option is required to create a `Second Generation` instance. See the | ||
| full list of available tiers by running `gcloud sql tiers list` | ||
|
|
||
| 2. Set the root password on your Cloud SQL instance: | ||
|
|
||
| $ gcloud sql instances set-root-password YOUR_INSTANCE_NAME --password YOUR_INSTANCE_ROOT_PASSWORD | ||
|
|
||
| 3. Install and run the [CloudSQL Proxy](https://cloud.google.com/sql/docs/mysql-connect-proxy) | ||
|
|
||
| 4. Create a database for this example | ||
|
|
||
| $ mysql -h 127.0.0.1 -u root -p -e "CREATE DATABASE <YOUR_DATABASE_NAME>;" | ||
|
|
||
| ## Deploy to App Engine | ||
|
|
||
| **Prerequisites** | ||
|
|
||
| - Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/). | ||
|
|
||
| **Deploy with gcloud** | ||
|
|
||
| 1. Update `app.yaml` with the configuration values for `USER`, `PASSWORD`, and | ||
| `DATABASE` with the values you used during setup. | ||
|
|
||
| 1. Get the CloudSQL connection name | ||
|
|
||
| $ gcloud beta sql instances describe YOUR_INSTANCE_NAME | grep connectionName | ||
|
|
||
| 1. Update `app.yaml` with the configuration value for `CONNECTION_NAME` you retrieved | ||
| at the end up setup. | ||
|
|
||
| ``` | ||
| gcloud config set project YOUR_PROJECT_ID | ||
| gcloud app deploy | ||
| gcloud app browse | ||
| ``` | ||
|
|
||
| The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/` | ||
| in your browser. | ||
|
|
||
| ## Run locally | ||
|
|
||
| 1. Ensure the [CloudSQL Proxy](https://cloud.google.com/sql/docs/external#proxy) is | ||
| installed and running. | ||
|
|
||
| 1. Set the following environment variables with the configuration values for | ||
| `USER`, `PASSWORD`, `DATABASE`, and `CONNECTION_NAME` you used during setup: | ||
|
|
||
| ```sh | ||
| # set local mysql connection parameters | ||
| export POSTGRES_DSN="pgsql:host=127.0.0.1;port=5432;dbname=DATABASE" | ||
| export POSTGRES_USER=USER | ||
| export POSTGRES_PASSWORD=PASSWORD | ||
| ``` | ||
|
|
||
| 1. Run the application | ||
|
|
||
| ```sh | ||
| cd php-docs-samples/appengine/flexible/cloudsql | ||
| php -S localhost:8080 | ||
| ``` | ||
|
|
||
| Now you can view the app running at [http://localhost:8080](http://localhost:8080) | ||
| in your browser. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2016 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| # [START example] | ||
| use Silex\Application; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
|
|
||
| // create the Silex application | ||
| $app = new Application(); | ||
|
|
||
| // Create the PDO object for CloudSQL Postgres. | ||
| $dsn = getenv('POSTGRES_DSN'); | ||
| $user = getenv('POSTGRES_USER'); | ||
| $password = getenv('POSTGRES_PASSWORD'); | ||
| $pdo = new PDO($dsn, $user, $password); | ||
|
|
||
| // Create the database if it doesn't exist | ||
| $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
| $pdo->query('CREATE TABLE IF NOT EXISTS visits ' . | ||
| '(time_stamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, user_ip CHAR(64))'); | ||
|
|
||
| // Add the PDO object to our Silex application. | ||
| $app['pdo'] = $pdo; | ||
|
|
||
| $app->get('/', function (Application $app, Request $request) { | ||
| $ip = $request->GetClientIp(); | ||
| // Keep only the first two octets of the IP address. | ||
| $octets = explode($separator = ':', $ip); | ||
| if (count($octets) < 2) { // Must be ip4 address | ||
| $octets = explode($separator = '.', $ip); | ||
| } | ||
| if (count($octets) < 2) { | ||
| $octets = ['bad', 'ip']; // IP address will be recorded as bad.ip. | ||
| } | ||
| // Replace empty chunks with zeros. | ||
| $octets = array_map(function ($x) { | ||
| return $x == '' ? '0' : $x; | ||
| }, $octets); | ||
| $user_ip = $octets[0] . $separator . $octets[1]; | ||
|
|
||
| // Insert a visit into the database. | ||
| /** @var PDO $pdo */ | ||
| $pdo = $app['pdo']; | ||
| $insert = $pdo->prepare('INSERT INTO visits (user_ip) values (:user_ip)'); | ||
| $insert->execute(['user_ip' => $user_ip]); | ||
|
|
||
| // Look up the last 10 visits | ||
| $select = $pdo->prepare( | ||
| 'SELECT * FROM visits ORDER BY time_stamp DESC LIMIT 10'); | ||
| $select->execute(); | ||
| $visits = ["Last 10 visits:"]; | ||
| while ($row = $select->fetch(PDO::FETCH_ASSOC)) { | ||
| array_push($visits, sprintf('Time: %s Addr: %s', $row['time_stamp'], | ||
| $row['user_ip'])); | ||
| } | ||
| return new Response(implode("\n", $visits), 200, | ||
| ['Content-Type' => 'text/plain']); | ||
| }); | ||
| # [END example] | ||
|
|
||
| return $app; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| runtime: php | ||
| env: flex | ||
|
|
||
| #[START env] | ||
| env_variables: | ||
| # Replace USER, PASSWORD, DATABASE, and CONNECTION_NAME with the | ||
| # values obtained when configuring your Cloud SQL instance. | ||
| POSTGRES_USER: USER | ||
| POSTGRES_PASSWORD: PASSWORD | ||
| POSTGRES_DSN: pgsql:dbname=DATABASE;host=/cloudsql/CONNECTION_NAME | ||
| #[END env] | ||
|
|
||
| #[START cloudsql_settings] | ||
| # Use the connection name obtained when configuring your Cloud SQL instance. | ||
| beta_settings: | ||
| cloud_sql_instances: "CONNECTION_NAME" | ||
| #[END cloudsql_settings] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "require": { | ||
| "silex/silex": "^1.3", | ||
| "google/apiclient": "^2.0" | ||
| }, | ||
| "require-dev": { | ||
| "google/cloud-tools": "^0.6", | ||
| "paragonie/random_compat": "^2.0", | ||
| "phpunit/phpunit": "~4" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't actually run the
DeployTeston travis, these are run from Jenkins. But that being said, we still want to rename these. nice job!