Skip to content

Commit a10688c

Browse files
1 parent 0df21e2 commit a10688c

File tree

10 files changed

+2770
-0
lines changed

10 files changed

+2770
-0
lines changed

appengine/flexible/cloudsql/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Cloud SQL & Google App Engine Flexible Environment
2+
3+
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).
4+
5+
## Setup
6+
7+
Before running this sample:
8+
9+
## Prerequisites
10+
11+
- Install [`composer`](https://getcomposer.org)
12+
- Install dependencies by running:
13+
14+
```sh
15+
composer install
16+
```
17+
18+
## Setup
19+
20+
Before you can run or deploy the sample, you will need to do the following:
21+
22+
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:
23+
24+
$ gcloud sql instances create YOUR_INSTANCE_NAME
25+
26+
1. Set the root password on your Cloud SQL instance:
27+
28+
$ gcloud sql instances set-root-password YOUR_INSTANCE_NAME --password YOUR_INSTANCE_ROOT_PASSWORD
29+
30+
1. Update the connection string in `app.yaml` with your configuration values. These values are used when the application is deployed.
31+
32+
## Run locally
33+
34+
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).
35+
36+
```sh
37+
cd php-docs-samples/appengine/standard/cloudsql
38+
39+
# set local mysql connection parameters
40+
export MYSQL_DSN="mysql:host=127.0.0.1;port=3306;dbname=guestbook"
41+
export MYSQL_USERNAME=root
42+
export MYSQL_PASSWORD=
43+
44+
php -S localhost:8080
45+
```
46+
47+
> be sure the `MYSQL_` environment variables are appropriate for your mysql instance
48+
49+
Now you can view the app running at [http://localhost:8080](http://localhost:8080)
50+
in your browser.
51+
52+
## Deploy to App Engine
53+
54+
**Prerequisites**
55+
56+
- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).
57+
58+
**Deploy with gcloud**
59+
60+
```
61+
gcloud config set project YOUR_PROJECT_ID
62+
gcloud preview app deploy
63+
gcloud preview app browse
64+
```
65+
66+
The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/`
67+
in your browser.

appengine/flexible/cloudsql/app.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
/**
3+
* Copyright 2015 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use Silex\Application;
19+
use Symfony\Component\HttpFoundation\Request;
20+
use Symfony\Component\HttpFoundation\Response;
21+
22+
// create the Silex application
23+
$app = new Application();
24+
25+
$app['pdo'] = function ($app) {
26+
$pdo = new PDO(
27+
$app['mysql.dsn'],
28+
$app['mysql.user'],
29+
$app['mysql.password']
30+
);
31+
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
32+
$pdo->query('CREATE TABLE IF NOT EXISTS visits ' .
33+
'(time_stamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, user_ip CHAR(64))');
34+
return $pdo;
35+
};
36+
37+
$app->get('/', function (Application $app, Request $request) {
38+
$ip = $request->GetClientIp();
39+
// Keep only the first two octets of the IP address.
40+
$octets = explode($separator = ':', $ip);
41+
if (count($octets) < 2) { // Must be ip4 address
42+
$octets = explode($separator = '.', $ip);
43+
}
44+
if (count($octets) < 2) {
45+
$octets = ['bad', 'ip']; // IP address will be recorded as bad.ip.
46+
}
47+
// Replace empty chunks with zeros.
48+
$octets = array_map(function ($x) { return $x == '' ? '0' : $x; }, $octets);
49+
$user_ip = $octets[0] . $separator . $octets[1];
50+
51+
// Insert a visit into the database.
52+
/** @var PDO $pdo */
53+
$pdo = $app['pdo'];
54+
$insert = $pdo->prepare('INSERT INTO visits (user_ip) values (:user_ip)');
55+
$insert->execute(['user_ip' => $user_ip]);
56+
57+
// Look up the last 10 visits
58+
$select = $pdo->prepare(
59+
'SELECT * FROM visits ORDER BY time_stamp DESC LIMIT 10');
60+
$select->execute();
61+
$visits = ["Last 10 visits:"];
62+
while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
63+
array_push($visits, sprintf('Time: %s Addr: %s', $row['time_stamp'],
64+
$row['user_ip']));
65+
}
66+
return new Response(implode("\n", $visits), 200,
67+
['Content-Type' => 'text/plain']);
68+
});
69+
70+
return $app;

appengine/flexible/cloudsql/app.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
runtime: php
2+
vm: true
3+
4+
runtime_config:
5+
document_root: web
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"require": {
3+
"silex/silex": "^1.3",
4+
"google/apiclient": "~2.0"
5+
},
6+
"require-dev": {
7+
"phpunit/phpunit": "~4",
8+
"google/cloud-tools": "^0.1.0"
9+
}
10+
}

0 commit comments

Comments
 (0)