-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: refactor postgres sample to use slim app #1099
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
shubha-rajan
merged 15 commits into
GoogleCloudPlatform:master
from
jdpedrie:cloudsql/postgres
Jul 6, 2020
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c31bd09
feat: refactor postgres sample to use slim app
jdpedrie abd5c73
replace MY-DATABASE with INSTANCE-NAME
jdpedrie f003fca
fix style, use hostname for choosing tcp mode, add GAE standard docs
jdpedrie 4019b60
fix copyright and secret manager link
jdpedrie 76e3ec4
Update cloud_sql/postgres/pdo/README.md
jdpedrie a8e2788
fix conflict
jdpedrie 95e45d2
Merge branch 'master' into cloudsql/postgres
jdpedrie 6dedd10
fix sample gae definition
jdpedrie c741d01
address review
jdpedrie 1b728f5
Merge branch 'cloudsql/postgres' of github.com:jdpedrie/php-docs-samp…
jdpedrie d52148d
address review comments
jdpedrie 56325a1
remove guzzle
jdpedrie 6f48414
Address review comments in README.md
shubha-rajan 4c52fea
Remove comment in app.flex.yaml
shubha-rajan 0ea3794
Update README.md
shubha-rajan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Copyright 2020 Google LLC | ||
| # | ||
| # 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. | ||
|
|
||
| runtime: php | ||
| env: flex | ||
|
|
||
| # Remember - storing secrets in plaintext is potentially unsafe. Consider using | ||
| # something like https://cloud.google.com/secret-manager/ to help keep secrets | ||
| # secret. | ||
| env_variables: | ||
| CLOUD_SQL_CONNECTION_NAME: "<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>" | ||
| DB_USER: my-db-user | ||
| DB_PASS: my-db-pass | ||
| DB_NAME: my-db | ||
|
|
||
| beta_settings: | ||
| cloud_sql_instances: "<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>" | ||
|
|
||
| runtime_config: | ||
| document_root: . | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "name": "google/cloud-sql-postgres-example", | ||
| "autoload": { | ||
| "psr-4": { | ||
| "Google\\Cloud\\Samples\\CloudSQL\\Postgres\\": "src" | ||
| } | ||
| }, | ||
| "autoload-dev": { | ||
| "psr-4": { | ||
| "Google\\Cloud\\Samples\\CloudSQL\\Postgres\\Tests\\": "src" | ||
| } | ||
| }, | ||
| "require": { | ||
| "php": ">= 7.2", | ||
| "slim/slim": "^4.5", | ||
| "slim/twig-view": "^3.1", | ||
| "slim/http": "^1.0", | ||
| "slim/psr7": "^1.0", | ||
| "pimple/pimple": "^3.3" | ||
| } | ||
jdpedrie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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 |
|---|---|---|
| @@ -1,40 +1,55 @@ | ||
| <?php | ||
| # Copyright 2018 Google LLC | ||
| # | ||
| # 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. | ||
|
|
||
| require_once 'src/DB.php'; | ||
| require_once 'src/Votes.php'; | ||
|
|
||
| use Google\Cloud\Samples\CloudSQL\Postgres\DB; | ||
| use Google\Cloud\Samples\CloudSQL\Postgres\Votes; | ||
|
|
||
| $votes = new Votes(DB::createPdoConnection()); | ||
|
|
||
| if ($_SERVER['REQUEST_URI'] == '/' && $_SERVER['REQUEST_METHOD'] == 'GET') { | ||
| $list = $votes->list(); | ||
|
|
||
| $vote_count = $votes->count_candidates(); | ||
| $tab_count = $vote_count['tabs']; | ||
| $space_count = $vote_count['spaces']; | ||
|
|
||
| include_once("./template.php"); | ||
| } elseif ($_SERVER['REQUEST_URI'] == '/' && $_SERVER['REQUEST_METHOD'] == 'POST') { | ||
| /* | ||
| * Copyright 2020 Google LLC. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Slim\Psr7\Factory\StreamFactory; | ||
|
|
||
| include __DIR__ . '/vendor/autoload.php'; | ||
|
|
||
| $app = include __DIR__ . '/src/app.php'; | ||
|
|
||
| $app->get('/', function ($request, $response) { | ||
| $this->get('votes')->createTableIfNotExists(); | ||
|
|
||
| return $this->get('view')->render($response, 'template.twig', [ | ||
| 'votes' => $this->get('votes')->listVotes(), | ||
| 'tabCount' => $this->get('votes')->getCountByValue('TABS'), | ||
| 'spaceCount' => $this->get('votes')->getCountByValue('SPACES'), | ||
| ]); | ||
| }); | ||
|
|
||
| $app->post('/', function ($request, $response) { | ||
| $this->get('votes')->createTableIfNotExists(); | ||
|
|
||
| $message = 'Invalid vote. Choose Between TABS and SPACES'; | ||
|
|
||
| if (!empty($_POST['team']) && in_array($_POST['team'], ['SPACES', 'TABS'])) { | ||
| $message = $votes->save($_POST['team']); | ||
| $formData = $request->getParsedBody() + [ | ||
| 'voteValue' => '' | ||
| ]; | ||
|
|
||
| if (in_array($formData['voteValue'], ['SPACES', 'TABS'])) { | ||
| $message = $this->get('votes')->insertVote($formData['voteValue']) | ||
| ? 'Vote cast for ' . $formData['voteValue'] | ||
| : 'An error occurred'; | ||
| } | ||
|
|
||
| echo $message; | ||
| } | ||
| $streamFactory = new StreamFactory; | ||
| return $response->withBody($streamFactory->createStream($message)); | ||
| }); | ||
|
|
||
| $app->run(); |
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,13 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <phpunit colors="true" bootstrap="../../../testing/bootstrap.php"> | ||
| <testsuites> | ||
| <testsuite name="CloudSQLPostgresSample"> | ||
jdpedrie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <directory>tests</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
| <filter> | ||
| <whitelist> | ||
| <directory suffix=".php">src</directory> | ||
| </whitelist> | ||
| </filter> | ||
| </phpunit> | ||
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.