-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GAE standard + firebase tictactoe sample
- Loading branch information
Jerjou Cheng
committed
Oct 13, 2016
1 parent
012874e
commit ce7de41
Showing
16 changed files
with
1,275 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,59 @@ | ||
# Tic Tac Toe on Google App Engine Standard using Firebase | ||
|
||
This directory contains a project that implements a realtime two-player game of | ||
Tic Tac Toe on Google [App Engine Standard][standard], using the [Firebase] database | ||
for realtime notifications when the board changes. | ||
|
||
[Firebase]: https://firebase.google.com | ||
[standard]: https://cloud.google.com/appengine/docs/about-the-standard-environment | ||
|
||
## Prerequisites | ||
|
||
* Install [Apache Maven][maven] 3.0.5 or later | ||
* Install the [Google Cloud SDK][sdk] | ||
* Create a project in the [Firebase Console][fb-console] | ||
* In the [Overview section][fb-overview] of the Firebase console, click 'Add | ||
Firebase to your web app' and replace the contents of the file | ||
`src/main/webapp/WEB-INF/view/firebase_config.jspf` with that code snippet. | ||
* If using the development appserver to run the app locally, you must supply | ||
credentials that would otherwise be inferred from the App Engine environment. | ||
Download [service account credentials][creds] and set the | ||
`GOOGLE_APPLICATION_CREDENTIALS` environment variable to its path: | ||
|
||
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials.json | ||
|
||
|
||
[fb-console]: https://console.firebase.google.com | ||
[sdk]: https://cloud.google.com/sdk | ||
[creds]: https://console.firebase.google.com/iam-admin/serviceaccounts/project?project=_&consoleReturnUrl=https:%2F%2Fconsole.firebase.google.com%2Fproject%2F_%2Fsettings%2Fgeneral%2F | ||
[fb-overview]: https://console.firebase.google.com/project/_/overview | ||
|
||
|
||
## Run the sample | ||
|
||
* To run the app locally using the development appserver: | ||
|
||
```sh | ||
$ mvn appengine:run | ||
``` | ||
|
||
## Troubleshooting | ||
|
||
* If you see the error `Google Cloud SDK path was not provided ...`: | ||
* Make sure you've installed the [Google Cloud SDK][sdk] | ||
* Make sure the Google Cloud SDK's `bin/` directory is in your `PATH`. If | ||
you prefer it not to be, you can also set the environment variable | ||
`GOOGLE_CLOUD_SDK_HOME` to point to where you installed the SDK: | ||
|
||
```sh | ||
export GOOGLE_CLOUD_SDK_HOME=/path/to/google-cloud-sdk | ||
``` | ||
|
||
## Contributing changes | ||
|
||
See [CONTRIBUTING.md](../../CONTRIBUTING.md). | ||
|
||
## Licensing | ||
|
||
See [LICENSE](../../LICENSE). | ||
|
This file contains 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,129 @@ | ||
<!-- | ||
Copyright 2015 Google Inc. All Rights Reserved. | ||
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. | ||
--> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.example.appengine</groupId> | ||
<artifactId>appengine-firebase</artifactId> | ||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>doc-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../..</relativePath> | ||
</parent> | ||
<properties> | ||
<objectify.version>5.1.13</objectify.version> | ||
<servlet-api.version>2.5</servlet-api.version> | ||
<gson.version>2.7</gson.version> | ||
<guava.version>19.0</guava.version> | ||
<google-api-client.version>1.22.0</google-api-client.version> | ||
<junit.version>4.12</junit.version> | ||
<mockito.version>1.10.19</mockito.version> | ||
<google-truth.version>0.30</google-truth.version> | ||
<appengine-maven.version>1.0.0</appengine-maven.version> | ||
</properties> | ||
<!-- [START set_versions] --> | ||
<prerequisites> | ||
<maven>3.3.9</maven> | ||
</prerequisites> | ||
<!-- [END set_versions] --> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-1.0-sdk</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>${servlet-api.version}</version> | ||
<type>jar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.7</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.googlecode.objectify</groupId> | ||
<artifactId>objectify</artifactId> | ||
<version>${objectify.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>${guava.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.api-client</groupId> | ||
<artifactId>google-api-client-appengine</artifactId> | ||
<version>${google-api-client.version}</version> | ||
</dependency> | ||
|
||
|
||
<!-- Test Dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>${mockito.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-testing</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-stubs</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-tools-sdk</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>${google-truth.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<!-- for hot reload of the web application --> | ||
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes | ||
</outputDirectory> | ||
<plugins> | ||
<plugin> | ||
<groupId>com.google.cloud.tools</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>${appengine-maven.version}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
46 changes: 46 additions & 0 deletions
46
...gine/firebase-tictactoe/src/main/java/com/example/appengine/firetactoe/DeleteServlet.java
This file contains 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,46 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.example.appengine.firetactoe; | ||
|
||
import com.google.appengine.api.users.UserService; | ||
import com.google.appengine.api.users.UserServiceFactory; | ||
import com.googlecode.objectify.Objectify; | ||
import com.googlecode.objectify.ObjectifyService; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* Handler that deletes the Firebase database that serves as the realtime communication channel. | ||
* This handler should be invoked after a game has finished, to clean up the channel. | ||
*/ | ||
public class DeleteServlet extends HttpServlet { | ||
@Override | ||
public void doPost(HttpServletRequest request, HttpServletResponse response) | ||
throws IOException { | ||
String gameId = request.getParameter("gameKey"); | ||
Objectify ofy = ObjectifyService.ofy(); | ||
Game game = ofy.load().type(Game.class).id(gameId).safe(); | ||
|
||
UserService userService = UserServiceFactory.getUserService(); | ||
String currentUserId = userService.getCurrentUser().getUserId(); | ||
|
||
game.deleteChannel(currentUserId); | ||
} | ||
} |
Oops, something went wrong.