Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions appengine/flexible/cloudsql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 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 [First 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 sql instances create YOUR_INSTANCE_NAME

1. Set the root password on your Cloud SQL instance:

$ gcloud sql instances set-root-password YOUR_INSTANCE_NAME --password YOUR_INSTANCE_ROOT_PASSWORD

1. Update the connection string in `app.yaml` with your configuration values. These values are used when the application is deployed.

## Run locally

To run locally, you can either run your own MySQL server locally and set the connection string in `app.yaml`, or you can [connect to your CloudSQL instance externally](https://cloud.google.com/sql/docs/external#appaccess).

```sh
cd php-docs-samples/appengine/standard/cloudsql

# set local mysql connection parameters
export MYSQL_DSN="mysql:host=127.0.0.1;port=3306;dbname=guestbook"
export MYSQL_USERNAME=root
export MYSQL_PASSWORD=

php -S localhost:8080
```

> be sure the `MYSQL_` environment variables are appropriate for your mysql instance

Now you can view the app running at [http://localhost:8080](http://localhost:8080)
in your browser.

## Deploy to App Engine

**Prerequisites**

- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).

**Deploy with gcloud**

```
gcloud config set project YOUR_PROJECT_ID
gcloud preview app deploy
gcloud preview app browse
```

The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/`
in your browser.
70 changes: 70 additions & 0 deletions appengine/flexible/cloudsql/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright 2015 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.
*/

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

// create the Silex application
$app = new Application();

$app['pdo'] = function ($app) {
$pdo = new PDO(
$app['mysql.dsn'],
$app['mysql.user'],
$app['mysql.password']
);
$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))');
return $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']);
});

return $app;
5 changes: 5 additions & 0 deletions appengine/flexible/cloudsql/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
runtime: php
vm: true

runtime_config:
document_root: web
10 changes: 10 additions & 0 deletions appengine/flexible/cloudsql/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"require": {
"silex/silex": "^1.3",
"google/apiclient": "~2.0"
},
"require-dev": {
"phpunit/phpunit": "~4",
"google/cloud-tools": "^0.1.0"
}
}
Loading