Skip to content
Draft
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
162 changes: 162 additions & 0 deletions appengine-java21/firebase-backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Build a mobile app using Firebase and App Engine flexible environment

![Kokoro Build Status](https://storage.googleapis.com/cloud-devrel-kokoro-resources/java/badges/firebase-appengine-backend.svg)

This repository contains Android client sample code for the [Build a Mobile App
Using Firebase and App Engine Flexible
Environment](https://cloud.google.com/solutions/mobile/mobile-firebase-app-engine-flexible)
solution. You can find the sample code for the Android client code in the
[firebase-android-client](../../../firebase-android-client) repository.

## Deployment requirements

- Enable the following services in the [Google Cloud Platform
console](https://console.cloud.google.com):
- Google App Engine
- Google Compute Engine
- Sign up for [Firebase](https://firebase.google.com/) and create a new project
in the [Firebase console](https://console.firebase.google.com/).
- Install the following tools in your development environment:
- [Java 21](https://www.oracle.com/java/technologies/downloads/)
- [Apache Maven](https://maven.apache.org/)
- [Google Cloud SDK](https://cloud.google.com/sdk/)

> **Note**: Firebase is a Google product, independent from Google Cloud
> Platform.

A Java application deployed to App Engine Flexible Environment [needs to use Java 21 Runtime](https://cloud.google.com/appengine/docs/flexible/java/setting-up-environment).
However, in your local development environment you can
use JDK 21 or newer as long as your JDK is able to produce Java 21 class files.

## Google Cloud SDK setup

Configure the SDK to access the Google Cloud Platform by using the following
command:

```bash
gcloud auth login
```

Get the project ID from the settings page of your Firebase project. Use the
following command to set your Firebase project as the active project for the
SDK:

```bash
gcloud config set project [project-id]
```

## Configuration

Enable the Google sign-in provider by following these steps:

1. Sign in to the [Firebase console](https://console.firebase.google.com) and
select your project.
1. In the **Develop** section, select **Authentication**.
1. In the **Authentication** page, select **Sign-in Method**.
1. Select and enable the **Google** sign-in provider.

Follow these steps to configure a service account for the backend application:

1. Go to your project settings page on the [Firebase
console](https://console.firebase.google.com).
1. Click the **Settings** gear next to 'Project Overview' and then **Project settings**.
1. Select **Service accounts** and click the link **Manage service account permissions**.
1. In the **IAM & admin** page click **Create service account**.
1. In the dialog, create an account with the following parameters:
* Enter *playchat-servlet* in the **Service account name** field.
* Select **Project** > **Owner** in the **Role** menu.
> **Caution**: The owner role gives the service account full access to all
> resources in the project. In a production app, you should change the role
> to the minimum access that your service account requires.
1. After the service account is created, click it and choose **Create new key** in the **ADD KEY** dropdown button.
* Choose **JSON** as the key type.
* Click **CREATE** to download the key.
1. After you finish creating the account, your browser downloads the service
account's private key to your computer as a JSON file. Move the file to the
`src/main/webapp/WEB-INF` folder in the backend project.
1. From the left menu of the [Firebase
console](https://console.firebase.google.com),
select **Database** in the **Develop** group.

1. In the **Database** page, click **Create database** in the **Realtime Database** section.

1. In the **Security rules for Realtime Database** dialog, select **Start in
test mode** and click **Enable**.

Caution: Test mode allows anyone with your database reference to perform
read and write operations to your database. If test mode isn't appropriate
for your purposes, you can write security rules to manage access to your
data. For more information, see
[Get Started with Database Rules](https://firebase.google.com/docs/database/security/quickstart)
in the Firebase documentation.

This step displays the data you’ve stored in Firebase. In later steps of
this tutorial, you can revisit this web page to see data added and updated
by the client app and backend servlet.
1. In the **Rules** tab of the database, make sure you have the security rules for read/write. For example:
```json
{
"rules": {
".read": true,
".write": true
}
}
```
1. Make a note of the Firebase URL for your project, which is in the form
`https://[project-id].firebaseio.com/` and appears next to a
link icon.
1. Open the `src/main/webapp/WEB-INF/web.xml` file and do the following:
* Replace the `JSON_FILE_NAME` placeholder with the JSON file from that
stores the service account's private key.
* Replace the `FIREBASE_URL` placeholder with the URL of the Realtime
Database from the previous step.

The following example shows the placeholders in the `web.xml` file:
```xml
<init-param>
<param-name>credential</param-name>
<param-value>/WEB-INF/JSON_FILE_NAME</param-value>
</init-param>
<init-param>
<param-name>databaseUrl</param-name>
<param-value>FIREBASE_URL</param-value>
</init-param>
```


## Build and deploy

To build and run the backend module locally:

```bash
mvn clean package appengine:run
```

To deploy the backend module to App Engine:

```bash
mvn clean package appengine:deploy
```

## View user event logs

Run the Android client app, perform some activities such as signing in and
switching channels, and go to the following URL to view user event logs:

```bash
https://[project-id].appspot.com/printLogs
```

## License

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.

This is not an official Google product.
128 changes: 128 additions & 0 deletions appengine-java21/firebase-backend/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.appengine</groupId>
<artifactId>firebase-backend</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>App Engine Backend module for Firebase</name>
<url>https://cloud.google.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
<archiveClasses>true</archiveClasses>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<prerequisites>
<maven>3.5</maven>
</prerequisites>
<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.28.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

<!-- Compile/runtime dependencies -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-logging</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-server-sdk</artifactId>
<version>[3.0.0,]</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>backend</finalName>
<outputDirectory>target/${project.artifactId}-${project.version}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>3.11.0</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<archiveClasses>true</archiveClasses>
<webResources>
<!-- in order to interpolate version from pom into appengine-web.xml -->
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.5.0</version>
<configuration>
<projectId>GCLOUD_CONFIG</projectId>
<version>GCLOUD_CONFIG</version>
<promote>true</promote>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading