Skip to content

Commit fa0c6a1

Browse files
authored
Move samples from firebase-appengine-backend to java-docs-samples. (GoogleCloudPlatform#4369)
1 parent f259d63 commit fa0c6a1

File tree

9 files changed

+803
-0
lines changed

9 files changed

+803
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Build a mobile app using Firebase and App Engine flexible environment
2+
3+
![Kokoro Build Status](https://storage.googleapis.com/cloud-devrel-kokoro-resources/java/badges/firebase-appengine-backend.svg)
4+
5+
This repository contains Android client sample code for the [Build a Mobile App
6+
Using Firebase and App Engine Flexible
7+
Environment](https://cloud.google.com/solutions/mobile/mobile-firebase-app-engine-flexible)
8+
solution. You can find the sample code for the Android client code in the
9+
[firebase-android-client](../../../firebase-android-client) repository.
10+
11+
## Deployment requirements
12+
13+
- Enable the following services in the [Google Cloud Platform
14+
console](https://console.cloud.google.com):
15+
- Google App Engine
16+
- Google Compute Engine
17+
- Sign up for [Firebase](https://firebase.google.com/) and create a new project
18+
in the [Firebase console](https://console.firebase.google.com/).
19+
- Install the following tools in your development environment:
20+
- [Java 8](https://java.com/en/download/)
21+
- [Apache Maven](https://maven.apache.org/)
22+
- [Google Cloud SDK](https://cloud.google.com/sdk/)
23+
24+
> **Note**: Firebase is a Google product, independent from Google Cloud
25+
> Platform.
26+
27+
A Java application deployed to App Engine Flexible Environment [needs to use Java 8 Runtime](https://cloud.google.com/appengine/docs/flexible/java/setting-up-environment).
28+
However, in your local development environment you can
29+
use JDK 8 or newer as long as your JDK is able to produce Java 8 class files.
30+
31+
## Google Cloud SDK setup
32+
33+
Configure the SDK to access the Google Cloud Platform by using the following
34+
command:
35+
36+
```bash
37+
gcloud auth login
38+
```
39+
40+
Get the project ID from the settings page of your Firebase project. Use the
41+
following command to set your Firebase project as the active project for the
42+
SDK:
43+
44+
```bash
45+
gcloud config set project [project-id]
46+
```
47+
48+
## Configuration
49+
50+
Enable the Google sign-in provider by following these steps:
51+
52+
1. Sign in to the [Firebase console](https://console.firebase.google.com) and
53+
select your project.
54+
1. In the **Develop** section, select **Authentication**.
55+
1. In the **Authentication** page, select **Sign-in Method**.
56+
1. Select and enable the **Google** sign-in provider.
57+
58+
Follow these steps to configure a service account for the backend application:
59+
60+
1. Go to your project settings page on the [Firebase
61+
console](https://console.firebase.google.com).
62+
1. Click the **Settings** gear next to 'Project Overview' and then **Project settings**.
63+
1. Select **Service accounts** and click the link **Manage service account permissions**.
64+
1. In the **IAM & admin** page click **Create service account**.
65+
1. In the dialog, create an account with the following parameters:
66+
* Enter *playchat-servlet* in the **Service account name** field.
67+
* Select **Project** > **Owner** in the **Role** menu.
68+
> **Caution**: The owner role gives the service account full access to all
69+
> resources in the project. In a production app, you should change the role
70+
> to the minimum access that your service account requires.
71+
1. After the service account is created, click it and choose **Create new key** in the **ADD KEY** dropdown button.
72+
* Choose **JSON** as the key type.
73+
* Click **CREATE** to download the key.
74+
1. After you finish creating the account, your browser downloads the service
75+
account's private key to your computer as a JSON file. Move the file to the
76+
`src/main/webapp/WEB-INF` folder in the backend project.
77+
1. From the left menu of the [Firebase
78+
console](https://console.firebase.google.com),
79+
select **Database** in the **Develop** group.
80+
81+
1. In the **Database** page, click **Create database** in the **Realtime Database** section.
82+
83+
1. In the **Security rules for Realtime Database** dialog, select **Start in
84+
test mode** and click **Enable**.
85+
86+
Caution: Test mode allows anyone with your database reference to perform
87+
read and write operations to your database. If test mode isn't appropriate
88+
for your purposes, you can write security rules to manage access to your
89+
data. For more information, see
90+
[Get Started with Database Rules](https://firebase.google.com/docs/database/security/quickstart)
91+
in the Firebase documentation.
92+
93+
This step displays the data you’ve stored in Firebase. In later steps of
94+
this tutorial, you can revisit this web page to see data added and updated
95+
by the client app and backend servlet.
96+
1. In the **Rules** tab of the database, make sure you have the security rules for read/write. For example:
97+
```json
98+
{
99+
"rules": {
100+
".read": true,
101+
".write": true
102+
}
103+
}
104+
```
105+
1. Make a note of the Firebase URL for your project, which is in the form
106+
`https://[project-id].firebaseio.com/` and appears next to a
107+
link icon.
108+
1. Open the `src/main/webapp/WEB-INF/web.xml` file and do the following:
109+
* Replace the `JSON_FILE_NAME` placeholder with the JSON file from that
110+
stores the service account's private key.
111+
* Replace the `FIREBASE_URL` placeholder with the URL of the Realtime
112+
Database from the previous step.
113+
114+
The following example shows the placeholders in the `web.xml` file:
115+
```xml
116+
<init-param>
117+
<param-name>credential</param-name>
118+
<param-value>/WEB-INF/JSON_FILE_NAME</param-value>
119+
</init-param>
120+
<init-param>
121+
<param-name>databaseUrl</param-name>
122+
<param-value>FIREBASE_URL</param-value>
123+
</init-param>
124+
```
125+
126+
127+
## Build and deploy
128+
129+
To build and run the backend module locally:
130+
131+
```bash
132+
mvn clean package appengine:run
133+
```
134+
135+
To deploy the backend module to App Engine:
136+
137+
```bash
138+
mvn clean package appengine:deploy
139+
```
140+
141+
## View user event logs
142+
143+
Run the Android client app, perform some activities such as signing in and
144+
switching channels, and go to the following URL to view user event logs:
145+
146+
```bash
147+
https://[project-id].appspot.com/printLogs
148+
```
149+
150+
## License
151+
152+
Copyright 2018 Google LLC. All Rights Reserved.
153+
154+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
155+
this file except in compliance with the License. You may obtain a copy of the
156+
License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
157+
applicable law or agreed to in writing, software distributed under the License
158+
is distributed on an "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
159+
KIND, either express or implied. See the License for the specific language
160+
governing permissions and limitations under the License.
161+
162+
This is not an official Google product.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.example.appengine</groupId>
5+
<artifactId>firebase-backend</artifactId>
6+
<packaging>war</packaging>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>App Engine Backend module for Firebase</name>
9+
<url>https://cloud.google.com</url>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
17+
<archiveClasses>true</archiveClasses>
18+
<failOnMissingWebXml>false</failOnMissingWebXml>
19+
</properties>
20+
<prerequisites>
21+
<maven>3.5</maven>
22+
</prerequisites>
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.21</version>
31+
</parent>
32+
33+
<dependencyManagement>
34+
<dependencies>
35+
<dependency>
36+
<groupId>com.google.cloud</groupId>
37+
<artifactId>libraries-bom</artifactId>
38+
<version>10.0.0</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
</dependencies>
43+
</dependencyManagement>
44+
45+
<dependencies>
46+
47+
<!-- Compile/runtime dependencies -->
48+
<dependency>
49+
<groupId>com.google.cloud</groupId>
50+
<artifactId>google-cloud-logging</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>javax.servlet</groupId>
54+
<artifactId>javax.servlet-api</artifactId>
55+
<version>3.1.0</version>
56+
<scope>provided</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>javax.servlet.jsp</groupId>
60+
<artifactId>javax.servlet.jsp-api</artifactId>
61+
<version>2.3.1</version>
62+
<scope>provided</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>jstl</groupId>
66+
<artifactId>jstl</artifactId>
67+
<version>1.2</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.fasterxml.jackson.core</groupId>
71+
<artifactId>jackson-annotations</artifactId>
72+
<version>2.7.4</version>
73+
</dependency>
74+
<dependency>
75+
<groupId>com.google.firebase</groupId>
76+
<artifactId>firebase-server-sdk</artifactId>
77+
<version>[3.0.0,)</version>
78+
</dependency>
79+
<dependency>
80+
<groupId>junit</groupId>
81+
<artifactId>junit</artifactId>
82+
<version>4.13.1</version>
83+
<scope>test</scope>
84+
</dependency>
85+
</dependencies>
86+
87+
<build>
88+
<finalName>backend</finalName>
89+
<outputDirectory>target/${project.artifactId}-${project.version}/WEB-INF/classes</outputDirectory>
90+
<plugins>
91+
<plugin>
92+
<groupId>org.apache.maven.plugins</groupId>
93+
<version>3.8.1</version>
94+
<artifactId>maven-compiler-plugin</artifactId>
95+
<configuration>
96+
<source>1.8</source>
97+
<target>1.8</target>
98+
</configuration>
99+
</plugin>
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-war-plugin</artifactId>
103+
<version>3.1.0</version>
104+
<configuration>
105+
<archiveClasses>true</archiveClasses>
106+
<webResources>
107+
<!-- in order to interpolate version from pom into appengine-web.xml -->
108+
<resource>
109+
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
110+
<filtering>true</filtering>
111+
<targetPath>WEB-INF</targetPath>
112+
</resource>
113+
</webResources>
114+
</configuration>
115+
</plugin>
116+
<plugin>
117+
<groupId>com.google.cloud.tools</groupId>
118+
<artifactId>appengine-maven-plugin</artifactId>
119+
<version>2.3.0</version>
120+
<configuration>
121+
<projectId>GCLOUD_CONFIG</projectId>
122+
<version>GCLOUD_CONFIG</version>
123+
<promote>true</promote>
124+
</configuration>
125+
</plugin>
126+
</plugins>
127+
</build>
128+
</project>

0 commit comments

Comments
 (0)