lab02 : Spring Boot and Heroku Hello World

num ready? description assigned due
lab02 true Spring Boot and Heroku Hello World Wed 10/02 06:15PM Wed 10/09 11:59PM

Look here for formatted version: http://ucsb-cs56.github.io/f19/lab/lab02

This is an individual lab on the topic of Java web apps on Heroku.

You may cooperate with one or more pair partners from your team to help in debugging and understanding the lab, but each person should complete the lab separately for themselves.

Step 0: If you are working on your own machine

If you are working on CSIL, you can skip this step.

But if you are working on your own machine, you’ll need to install a few things before proceeding.

Here are some commands to let you be familiar with Maven in 5 mins! https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Step 1: Understanding what we are trying to do

What are we trying to accomplish again in this lab?

Why use Heroku?

Limitations of the free plan of Heroku

TL;DR: You should NOT need to enter a credit card into Heroku. If you are asked for one, something has gone wrong.

Web Apps vs. Static Web Pages

You may already have some experience with creating static web pages, and/or with creating web applications (e.g. using PHP, Python (Django or Flask) or Ruby on Rails.) If so, then the “Learn More” section will be basic review.

If you are new to writing software for the web, you are strongly encouaged to read the background information at the “learn more” link below.

What are we trying to accomplish again in this lab?

If you just did a deep dive into the article Web Pages vs. Web Apps, it may be helpful to again review what we are trying to accomplish in this lab:

Disk Quota

IMPORTANT: if you are working on CSIL, and at some point things just “stop working”:

Then you probably have a disk quota problem.

Step 2: Create a Heroku Account

If you do not already have a Heroku account, navigate to https://www.heroku.com/ and click the “Sign up for Free” link.

Heroku Signup

You’ll be asked for:

Step 3: Fork the tutorial repo

“Fork” the tutorial repo into a public copy under your own github account.

To “fork” a repo in Github:

The repo you are going to fork is here:

You’ll need Maven for this lab.

Assuming you are working on CSIL, you can use mvn to run Maven.

Use mvn compile and mvn exec:java to try to run the code and get a web app running on localhost.

Note that in order to see this web app running, you’ll need to be in a web browser on the same host that you are running your program on.

If you are not sitting in the CSIL or CSTL lab, i.e. you are using ssh on a laptop to access CSIL, then you might need to test your webapp using a command line web client such as curl (curl stands for “C” the “URL”). For example, this command should show you the output from the / route for your webapp:

curl http://localhost:4567

And

curl http://localhost:4567/hello

would show the output from the /hello route.

This is not very satisfying.

To get the web app running on the public internet, we’ll need to use a cloud-computing platform such as Heroku.

Step 4: Create a new Heroku App using the Heroku CLI

Logged into CSIL (or one of the machines in the CSTL, i.e. Phelps 3525), use this command to login to Heroku at the command line:

heroku login

NOTE: If the heroku login command doesn’t work, you can instead create the Heroku App at the Heroku Dashboard by visiting https://dashboard.heroku.com/apps, clicking (at upper right): “New => Create New App” and then creating an app with the name heroku create cs56-f19-githubid-lab02 as explained in > the instructions below.

Then, use this command to create a new web app running on heroku. Substitute your github id in place of githubid.
Note that you should convert your githubid to all lowercase; heroku web-app names do not permit uppercase letters.

heroku create cs56-f19-githubid-lab02

Notes:

Step 5: Login to the Heroku Dashboard

Login to https://dashboard.heroku.com/apps and look for the create cs56-f19-githubid-lab02 app that you created.

You should find a place where you can connect your App to Github.

Click on this, and select your repo to connect the Github Repo to Heroku.

Then, click on “deploy branch”.

What if it doesn’t work?

If it doesn’t work, try these things before asking a mentor, TA, or instructor for help.

  1. Make sure you are logged into Heroku at CLI with heroku login. If you exited your CSIL shell (logged out) and logged back in again, you have to login to Heroku again. Then repeat the commands.
  2. Try, try running heroku apps. Make sure the <appname>app-name-goes-here</appname> element in the heroku-maven-plugin section of your pom.xml matches the name of your heroku app exactly.
  3. If it does, try heroku logs --app appname (substitute the name of your app where you see appname). You’ll see the log output of that app on Heroku.
    • You may find it helpful to open a second Terminal, login to CSIL and the Heroku CLI, and use heroku logs --app appname --tail, which keeps the log output running continously.
    • You can also see your logs in a web browser at: https://dashboard.heroku.com/apps/app-name/logs (note that you need to put your app-name in the URL instead of app-name.
    • You can navigate to this from https://dashboard.heroku.com/ by selecting your app, clicking on it, selecting the More menu at upper right, and the selecting Logs.

Step 6: Changing what is shown on the page

Go into the Java source code under src and locate the file /src/main/java/hello/HelloController.java

In this file, locate the line of code that says:

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

This method returns the contents of the home page ("/") for the webapp.

Change that code to the following. Be sure to replace mygithubid with your own github id:

String html = "<h1>Hello World!</h1>\n" +
    "<p>This web app is powered by \n" +
    "<a href='https://github.com/mygithubid/spring-boot-minimal-webapp'>this github repo</a></p>\n";
return html;

Then:

If it works, then the words “this github repo” should become clickable links.

Ok, so far, we haven’t really done anything we couldn’t have done with a static web page. But we have gotten a working Java web app running on Heroku, so it’s start we can build on.

Step 7: The test cases

You’ll see that when you run “mvn test” that there are test cases, some of which are now failing.

The test cases are in these files:

Run the tests and see them fail.

Then modify them so that they pass. Note that we are doing TDD “wrong” this time; to do it “the right way”, we should have modified the tests first, and then modified the code so that the tests pass. We’ll pivot to this style of working once we have a better grasp on all the moving parts here.

Step 8: Submitting your work for grading

When you have a running web app, visit https://gauchospace.ucsb.edu/courses/mod/assign/view.php?id=2672849 and make a submission.

In the text area, enter something like this, substituting your repo name and your Heroku app name:

repo name: https://github.com/chrislee123/spring-boot-minimal-webapp
on heroku: https://cs56-f19-chrislee123-lab02.herokuapp.com

Then, and this is super important, please make both of those URLs clickable urls.

The instructions for doing so are here: https://ucsb-cs56.github.io/topics/gauchospace_clickable_urls/

Grading Rubric: