|
1 | 1 | # SparkJava on App Engine Flexible Environment
|
2 | 2 |
|
3 |
| -This app demonstrates how to use [`gcloud-java`'s Datastore client](https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-datastore#google-cloud-java-client-for-datastore) from within an [App Engine flexible environment](https://cloud.google.com/appengine/docs/flexible/java/hello-world) project using [SparkJava](http://sparkjava.com/). The app allows you to create and modify a database of "users", which contains their ID, name, and email information. |
4 |
| - |
5 |
| -`gcloud-java` is an idiomatic Java client for [Google Cloud Platform](https://cloud.google.com/) services. Read more about the library [here](https://github.com/GoogleCloudPlatform/gcloud-java#google-cloud-java-client). |
| 3 | +This app demonstrates how to use [Datastore with the Google Cloud client |
| 4 | +library](https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-datastore) |
| 5 | +from within an [App Engine flexible |
| 6 | +environment](https://cloud.google.com/appengine/docs/flexible/java/hello-world) |
| 7 | +project using [SparkJava](http://sparkjava.com/). The app allows you to create |
| 8 | +and modify a database of "users", which contains their ID, name, and email |
| 9 | +information. |
| 10 | + |
| 11 | +The Google Cloud client library is an idiomatic Java client for [Google Cloud |
| 12 | +Platform](https://cloud.google.com/) services. Read more about the library |
| 13 | +[here](https://github.com/GoogleCloudPlatform/google-cloud-java). |
6 | 14 |
|
7 | 15 | Setup
|
8 | 16 | -----
|
9 | 17 |
|
10 |
| -1. Create a Google Developers Console project with the Datastore API enabled. [Follow these instructions](https://cloud.google.com/docs/authentication#preparation) to get your project set up. If you wish to deploy this application, you will also need to [enable billing](https://support.google.com/cloud/?rd=2#topic=6288636). |
| 18 | +1. Create a Google Cloud project with the Datastore API enabled. |
| 19 | + [Follow these |
| 20 | + instructions](https://cloud.google.com/docs/authentication#preparation) to |
| 21 | + get your project set up. If you wish to deploy this application, you will |
| 22 | + also need to [enable |
| 23 | + billing](https://support.google.com/cloud/?rd=2#topic=6288636). |
11 | 24 |
|
12 |
| -2. Set up the local development environment by [installing the Google Cloud SDK](https://cloud.google.com/sdk/) and running the following commands in command line: `gcloud auth login` and `gcloud config set project [YOUR PROJECT ID]`. |
| 25 | +2. Set up the local development environment by [installing the Google Cloud |
| 26 | + SDK](https://cloud.google.com/sdk/) and running the following commands in |
| 27 | + command line: `gcloud auth login` and `gcloud config set project [YOUR |
| 28 | + PROJECT ID]`. |
13 | 29 |
|
14 |
| -3. Ensure that you have Maven installed and configured to use Java 8. See installation instructions [here](https://maven.apache.org/install.html). |
| 30 | +3. Ensure that you have Maven installed and configured to use Java 8. See |
| 31 | + installation instructions [here](https://maven.apache.org/install.html). |
15 | 32 |
|
16 | 33 | Running locally
|
17 | 34 | ---------------
|
18 | 35 |
|
19 |
| -Run the application on your local machine by typing the following into your command line from the `sparkjava` directory: `mvn clean package exec:java`. Navigate to `localhost:8080` to view and interact with the application. |
| 36 | +Run the application on your local machine by typing the following into your |
| 37 | +command line from the `sparkjava` directory: `mvn clean package exec:java`. |
| 38 | +Navigate to `localhost:8080` to view and interact with the application. |
20 | 39 |
|
21 | 40 | Deploying
|
22 | 41 | ---------
|
23 | 42 |
|
24 |
| -If you've enabled billing (step 1 in [Setup](#Setup)), you can deploy the application to the web by running `mvn gcloud:deploy` from your command line (from the `sparkjava` directory). |
| 43 | +If you've enabled billing (step 1 in [Setup](#Setup)), you can deploy the |
| 44 | +application to the web by running `mvn gcloud:deploy` from your command line |
| 45 | +(from the `sparkjava` directory). |
25 | 46 |
|
26 | 47 | How does it work?
|
27 | 48 | -----------------
|
28 | 49 |
|
29 |
| -You'll notice that the source code is split into three folders: `appengine`, `java/com/google/appengine/sparkdemo`, and `resource/public`. The `appengine` folder contains a `Dockerfile` and an `app.yaml`, necessary files to [configure the VM environment](https://cloud.google.com/appengine/docs/managed-vms/config). The `java/com/google/appengine/sparkdemo` folder contains the controller code, which uses the `gcloud-java` library to modify the records in the Google Cloud Datastore. Finally, the `resource/public` folder contains the home webpage, which uses jQuery to send HTTP requests to create, remove, and update records. |
30 |
| - |
31 |
| -Spark runs the [`main` method](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/managedvms/sparkjava-demo/src/main/java/com/google/appengine/sparkdemo/Main.java) upon server startup. The `main` method creates the controller, [`UserController`](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/managedvms/sparkjava-demo/src/main/java/com/google/appengine/sparkdemo/UserController.java). The URIs used to send HTTP requests in the [home page](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/managedvms/sparkjava-demo/src/main/resources/public/index.html) correspond to methods in the `UserController` class. For example, the `index.html` code for `create` makes a `POST` request to the path `/api/users` with a body containing the name and email of a user to add. `UserController` contains the following code to process that request: |
| 50 | +You'll notice that the source code is split into three folders: `appengine`, |
| 51 | +`java/com/google/appengine/sparkdemo`, and `resource/public`. The `appengine` |
| 52 | +folder contains a `Dockerfile` and an `app.yaml`, necessary files to [configure |
| 53 | +the VM |
| 54 | +environment](https://cloud.google.com/appengine/docs/managed-vms/config). The |
| 55 | +`java/com/google/appengine/sparkdemo` folder contains the controller code, |
| 56 | +which uses the Google Cloud client library to modify the records in the Google Cloud |
| 57 | +Datastore. Finally, the `resource/public` folder contains the home webpage, |
| 58 | +which uses jQuery to send HTTP requests to create, remove, and update records. |
| 59 | + |
| 60 | +Spark runs the [`main` |
| 61 | +method](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/managedvms/sparkjava-demo/src/main/java/com/google/appengine/sparkdemo/Main.java) |
| 62 | +upon server startup. The `main` method creates the controller, |
| 63 | +[`UserController`](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/managedvms/sparkjava-demo/src/main/java/com/google/appengine/sparkdemo/UserController.java). |
| 64 | +The URIs used to send HTTP requests in the [home |
| 65 | +page](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/managedvms/sparkjava-demo/src/main/resources/public/index.html) |
| 66 | +correspond to methods in the `UserController` class. For example, the |
| 67 | +`index.html` code for `create` makes a `POST` request to the path `/api/users` |
| 68 | +with a body containing the name and email of a user to add. `UserController` |
| 69 | +contains the following code to process that request: |
32 | 70 |
|
33 | 71 | ```java
|
34 | 72 | post("/api/users", (req, res) -> userService.createUser(
|
35 | 73 | req.queryParams("name"),
|
36 | 74 | req.queryParams("email),
|
37 | 75 | ), json());
|
38 | 76 | ```
|
39 |
| -This code snippet gets the name and email of the user from the POST request and passes it to `createUser` (in [`UserService.java`](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/managedvms/sparkjava-demo/src/main/java/com/google/appengine/sparkdemo/UserService.java)) to create a database record using `gcloud-java`. If you want a more in-depth tutorial on using `gcloud-java` Datastore client, see the [Getting Started](https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-datastore#getting-started) section in the `gcloud-java-datastore` documentation. |
40 |
| -
|
41 |
| -Communication with the Google Cloud Datastore requires authentication and setting a project ID. When running locally, `gcloud-java` automatically detects your credentials and project ID because you logged into the Google Cloud SDK and set your project ID. There are also many other options for authenticating and setting a project ID. To read more, see the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) and [Specifying a Project ID](https://github.com/GoogleCloudPlatform/gcloud-java#specifying-a-project-id) sections of the `gcloud-java` documentation. |
42 |
| -
|
43 |
| -You built and ran this application using Maven. To read more about using Maven with Managed VMs, see the [Using Apache Maven documentation](https://cloud.google.com/appengine/docs/java/managed-vms/maven). While this particular project uses Maven, `gcloud-java` can also be accessed using Gradle and SBT. See how to obtain the dependency in the [Quickstart section](https://github.com/GoogleCloudPlatform/gcloud-java#quickstart) of the `gcloud-java` documentation. |
| 77 | +This code snippet gets the name and email of the user from the POST request and |
| 78 | +passes it to `createUser` (in |
| 79 | +[`UserService.java`](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/managedvms/sparkjava-demo/src/main/java/com/google/appengine/sparkdemo/UserService.java)) |
| 80 | +to create a database record using the Google Cloud client library. If you want |
| 81 | +a more in-depth tutorial on using Google Cloud client library Datastore client, |
| 82 | +see the [Getting |
| 83 | +Started](https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-datastore#getting-started) |
| 84 | +section of the client library documentation. |
| 85 | +
|
| 86 | +Communication with the Google Cloud Datastore requires authentication and |
| 87 | +setting a project ID. When running locally, the Google Cloud client library |
| 88 | +automatically detects your credentials and project ID because you logged into |
| 89 | +the Google Cloud SDK and set your project ID. There are also many other options |
| 90 | +for authenticating and setting a project ID. To read more, see the |
| 91 | +[Authentication](https://github.com/GoogleCloudPlatform/google-cloud-java#authentication) |
| 92 | +and [Specifying a Project |
| 93 | +ID](https://github.com/GoogleCloudPlatform/google-cloud-java#specifying-a-project-id) |
| 94 | +sections of the client library documentation. |
| 95 | +
|
| 96 | +You built and ran this application using Maven. To read more about using Maven |
| 97 | +with App Engine flexible environment, see the [Using Apache Maven |
| 98 | +documentation](https://cloud.google.com/appengine/docs/flexible/java/using-maven). |
| 99 | +While this particular project uses Maven, the Google Cloud client library |
| 100 | +packages can also be accessed using Gradle and SBT. See how to obtain the |
| 101 | +dependency in the [Quickstart |
| 102 | +section](https://github.com/GoogleCloudPlatform/google-cloud-java#quickstart) |
| 103 | +of the client library documentation. |
44 | 104 |
|
45 | 105 | License
|
46 | 106 | -------
|
47 | 107 |
|
48 |
| -Apache 2.0 - See [LICENSE](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/LICENSE) for more information. |
| 108 | +Apache 2.0 - See |
| 109 | +[LICENSE](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/LICENSE) |
| 110 | +for more information. |
0 commit comments