blob: ad668a0d3b92ef228e257e60680dcc005912f623 [file] [log] [blame] [view]
David Chenc4af8282016-01-20 11:06:35 +00001---
Damien Martin-Guillerez95a54b92016-07-28 12:47:11 +00002layout: documentation
3title: Tutorial - Build the Backend Server
David Chenc4af8282016-01-20 11:06:35 +00004---
Damien Martin-Guillerez95a54b92016-07-28 12:47:11 +00005
6# Tutorial - Build the Backend Server
7
8The backend server is a simple web application that runs on Google App Engine
9and responds to incoming HTTP requests from the sample Android and iOS apps.
10
11Here, you'll do the following:
12
13* Review the source files for the app
14* Update the `WORKSPACE` file
15* Create the `appengine.BUILD` file
16* Create a `BUILD` file
17* Run the build
18* Find the build outputs
19* Deploy to a local development server
20* Deploy to Google App Engine
21
22Bazel provides a set of [App Engine build rules](/docs/be/appengine.html)
23written using the [Skylark](/docs/skylark/index.html) framework. You'll use
24these in the steps below to build the application.
25
26## Review the source files
27
28The source files for the backend server are located in `$WORKSPACE/backend/`.
29
30The key files and directories are:
31
32<table class="table table-condensed table-striped">
33<thead>
34<tr>
35<td>Name</td>
36<td>Location</td>
37</tr>
38</thead>
39<tbody>
40<tr>
41<td>Source file directory</td>
42<td><code>src/main/java/com/google/bazel/example/app/</code></td>
43</tr>
44<tr>
45<td>Web application metadata directory</td>
46<td><code>webapp/WEB-INF/</code></td>
47</tr>
48</tbody>
49</table>
50
51## Update the WORKSPACE file
52
53As with the Android app, you must add references to
54[external dependencies](http://bazel.io/docs/external.html) to your `WORKSPACE`
55file. For the backend server, these are references to the App Engine SDK,
56the Java Servlet SDK and other libraries needed to build the App Engine
57applications.
58
59### Add a new\_http\_archive rule
60
61When you built the Android app, you added a reference to the location on your
62filesystem where you downloaded and installed the Android SDK. For the
63backend server, however, you'll give Bazel instructions for downloading the
64required App Engine SDK package from a remote server. This is optional. You
65can also download and install the SDK manually on your filesystem and reference
66it from that location as described in the
67[App Engine rule documentation](/docs/be/appengine.html).
68
69Add the following to your `WORKSPACE` file:
70
71```python
72git_repository(
73 name = "io_bazel_rules_appengine",
74 remote = "https://github.com/bazelbuild/rules_appengine.git",
75 tag = "0.0.2",
76)
77load("@io_bazel_rules_appengine//appengine:appengine.bzl", "appengine_repositories")
78appengine_repositories()
79```
80
81[`git_repository`](/docs/be/workspace.html#git_repository) downloads the
82AppEngine rules from GitHub, then the next two lines use the
83`appengine_repositories` function defined in these rules to download the
84libraries and SDK needed to build AppEngine applications.
85
86Now, save and close the file. You can compare your `WORKSPACE` file to the
87[completed example](https://github.com/bazelbuild/examples//blob/master/tutorial/WORKSPACE)
88in the `master` branch of the GitHub repo.
89
90## Create a BUILD file
91
92Now that you have set up the external dependencies, you can go ahead and create
93the `BUILD` file for the backend server, as you did previously for the sample
94Android and iOS apps.
95
96Open your new `BUILD` file for editing:
97
98```bash
99$ vi $WORKSPACE/backend/BUILD
100```
101
102### Add a java_binary rule
103
104Add the following to your `BUILD` file:
105
106```python
107java_binary(
108 name = "app",
109 srcs = glob(["src/main/java/**/*.java"]),
110 main_class = "does.not.exist",
111 deps = [
112 "@io_bazel_rules_appengine//appengine:javax.servlet.api",
113 ],
114)
115```
116
117The [`java_binary`](/docs/be/java.html#java_binary) tells Bazel
118how to build a Java `.jar` library for your application, plus a wrapper shell
119script that launches the application code from the specified main class. Here,
120we're using this rule instead of the
121[`java_library`](/docs/be/java.html#java_library) because we need
122the `.jar` file to contain all the dependencies required to build the final
123App Engine `.war` file. For this reason, we specify a bogus class name
124for the `main_class` attribute.
125
126### Add an appengine_war rule
127
128Add the following to your `BUILD` file:
129
130```python
131load("@io_bazel_rules_appengine//appengine:appengine.bzl", "appengine_war")
132
133appengine_war(
134 name = "backend",
135 data = [":webapp"],
136 data_path = "/backend/webapp",
137 jars = [":app_deploy.jar"],
138)
139
140filegroup(
141 name = "webapp",
142 srcs = glob(["webapp/**/*"]),
143)
144```
145
146The [`appengine_war`](/docs/be/appengine.html#appengine_war)
147rule builds the final App Engine `war` file from the library `.jar` file and web
148application metadata files in the `webapp` directory.
149
150Save and close the file. Again, the
151[completed example](https://github.com/google/bazel-examples/blob/master/tutorial/backend/BUILD)
152is in the `master` branch of the GitHub repo.
153
154## Run the build
155
156Make sure that your current working directory is inside your Bazel workspace:
157
158```bash
159$ cd $WORKSPACE
160```
161
162Now, enter the following to build the sample app:
163
164```bash
165$ bazel build //backend:backend
166```
167
168Bazel now launches and builds the sample app. During the build process, its
169output will appear similar to the following:
170
171```bash
172INFO: Found 1 target...
173Target //backend:backend up-to-date:
174 bazel-bin/backend/backend.war
175 bazel-bin/backend/backend.deploy
176 bazel-bin/backend/backend
177INFO: Elapsed time: 56.867s, Critical Path: 2.72s
178```
179
180## Find the build outputs
181
182The `.war` file and other outputs are located in the
183`$WORKSPACE/bazel-bin/backend` directory.
184
185## Deploy to a local development server
186
187The `appengine_war` rule generates an upload script that you can use to deploy
188your backend server on Google App Engine. Here, you'll start a local App Engine
189development server in your environment and deploy your application there.
190
191To deploy the application, enter the following:
192
193```bash
194$ bazel-bin/backend/backend --port=12345
195```
196
197Your application URL will be `http://localhost:12345`
198
199## Deploy to Google App Engine
200
201You can also deploy the application to the live App Engine serving
202environment on Google Cloud Platform. For this scenario, you must first create
203a project in the
204[Google Developers Console](https://console.developers.google.com).
205
206To deploy the application, enter the following:
207
208```bash
209$ $WORKSPACE/bazel-bin/backend/backend.deploy <project-id>
210```
211
212The deployment script prompts you to authorize access to Google Cloud Platform.
213After you have authorized access the first time, you can deploy the application
214using the `bazel` command and the following rule target:
215
216```bash
217$ bazel run //backend:backend.deploy <project-id>
218```
219
220Your application URL will be `http://<project-id>.appspot.com`.
221
222## What's next
223
224Now let's [review](review.md) the tutorial steps.