Skip to content

Commit 8f6898c

Browse files
author
Michael Wunderlich
committed
readme
1 parent fce7247 commit 8f6898c

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

README.md

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Scorekeep is a RESTful web API implemented in Java that uses Spring to provide a
33

44
The project shows the use of Spring, Angular, nginx, the AWS SDK for Java, DynamoDB, Gradle, CORS, and Elastic Beanstalk features that let you run both components on the same EC2 instance, create required DynamoDB tables as part of the Elastic Beanstalk environment, and build the API from source on-instance during deployment.
55

6+
Use the procedures in the following sections to run the project on AWS Elastic Beanstalk and configure it for local testing and development.
7+
68
## Create an AWS Elastic Beanstalk environment
79
Create a Java 8 SE environment in Elastic Beanstalk to host the application.
810

@@ -11,22 +13,12 @@ Create a Java 8 SE environment in Elastic Beanstalk to host the application.
1113
3. When your environment is ready, the console redirects you to the environment Dashboard.
1214
4. Click the URL at the top of the page to open the site.
1315

14-
## Configure the application and create a source bundle
15-
The application includes a frontend web app and a backend web API. In order for the frontend to send requests to the backend, it needs to know the URL of your environment.
16-
17-
1. Clone this repository.
18-
2. Open [eb-java-scorekeep/public/app/scorekeep.js](https://github.com/awslabs/eb-java-scorekeep/blob/master/public/app/scorekeep.js).
19-
3. Replace the placeholder value with the domain name of your environment.
20-
21-
module.value('api', 'http://scorekeep.XXXXXXXX.elasticbeanstalk.com/api/');
22-
23-
4. Save the file and commit your changes.
16+
## Give the application permission to use DynamoDB
17+
When the Scorekeep API runs in AWS Elastic Beanstalk, it uses the permissions of its EC2 instance to call AWS. Elastic Beanstalk provides a default instance profile that you can extend to grant the application the permissions it needs to read from and write to resource tables in DynamoDB.
2418

25-
eb-java-scorekeep$ git commit -am "Update API domain"
26-
27-
5. Create an archive of the updated source code.
28-
29-
eb-java-scorekeep$ git archive -o scorekeep-v1.zip HEAD
19+
1. Open the Elastic Beanstalk instance profile in the DynamoDB console: [aws-elasticbeanstalk-ec2-role](https://console.aws.amazon.com/iam/home#roles/aws-elasticbeanstalk-ec2-role)
20+
2. Click **Attach Policy**.
21+
3. Select [AmazonDynamoDBFullAccess](https://console.aws.amazon.com/iam/home#policies/arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess) and click **Attach Policy**.
3022

3123
## Deploy the application
3224
Deploy the source bundle that you created in the previous section to your environment.
@@ -39,9 +31,10 @@ Deploy the source bundle that you created in the previous section to your enviro
3931

4032
![Scorekeep front page](/img/scorekeep-frontpage.png)
4133

42-
Click through the app to explore its functionality
34+
Click through the app to explore its functionality. Use the network console in your browser to see the HTTP requests that it sends to the API to read and write users, sessions, games, moves and game state to DynamoDB via the API.
4335

4436
# How it works
37+
The project includes two independent components, an HTML and JavaScript frontend in Angular 1.5 and a Java backend that uses Spring to provide a public API.
4538

4639
## Frontend
4740
The frontend is an Angular 1.5 web app that uses `$resource` objects to perform CRUD operations on resources defined by the API. Users first encounter the [main view](https://github.com/awslabs/eb-java-scorekeep/blob/master/public/main.html) and [controller](https://github.com/awslabs/eb-java-scorekeep/blob/master/public/app/mainController.js) and progress through session and game views at routes that include the IDs of resources that the user creates.
@@ -57,28 +50,44 @@ The [Buildfile](https://github.com/awslabs/eb-java-scorekeep/blob/master/Buildfi
5750

5851
# Local testing
5952
You can run both the API and frontend locally with Gradle and the Spring Boot CLI.
53+
Clone this repository or extract the contents of the source bundle that you downloaded earlier to get started.
6054

6155
## API
6256
Use [Gradle](https://gradle.org/) to build the API and run it locally on port 5000.
6357
```
6458
eb-java-scorekeep$ gradle bootrun
6559
```
6660

67-
**Note:** The API requires DynamoDB tables to exist in AWS to run locally. These tables are created when you launch the application in Elastic Beanstalk.
61+
The API need's credentials in order to communicate with Amazon DynamoDB. Follow the instructions in the AWS SDK for Java developer guide to provide access keys in a local file or with environment variables: [Set up AWS Credentials for Development](http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html)
62+
63+
**Note:** The API requires DynamoDB tables to exist in AWS to run locally. These tables are created by [this configuration file](https://github.com/awslabs/eb-java-scorekeep/blob/master/.ebextensions/dynamodb-tables.config) when you launch the application in Elastic Beanstalk.
6864

6965
Use the test script to verify that the API works.
7066
```
7167
eb-java-scorekeep$ ./bin/test-api.sh
7268
```
7369

74-
## Frontend
75-
Use the [Spring Boot CLI](http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-installing-spring-boot.html) to run the frontend at [localhost:8080](http://localhost:8080)
70+
## Run the frontend and configure it for local testing
71+
Use the [Spring Boot CLI](http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-installing-spring-boot.html) to run the frontend on port 8080
7672
```
7773
eb-java-scorekeep$ spring run app.groovy
7874
```
75+
Open the app in a browser: [localhost:8080](http://localhost:8080)
7976

80-
## CORS
81-
The API includes a [CORS (cross-origin resource sharing) filter](https://github.com/awslabs/eb-java-scorekeep/blob/master/src/main/java/scorekeep/SimpleCORSFilter.java) that allows traffic from localhost:8080.
77+
It's not working. The app is trying to call paths relative to it's own root, localhost:8080, but the API is running on localhost:5000 or in Elastic Beanstalk. To fix this, configure the app with the full URL of the API.
78+
79+
When you run the frontend locally, it needs to know the full URL of the API in order to send requests to the backend.
80+
81+
1. Open [eb-java-scorekeep/public/app/scorekeep.js](https://github.com/awslabs/eb-java-scorekeep/blob/master/public/app/scorekeep.js).
82+
2. Set the value of the api module to the full URL of the API.
83+
a. Use the domain name of your environment to test changes to the frontend without running the backend locally
84+
module.value('api', 'http://scorekeep.XXXXXXXX.elasticbeanstalk.com/api/');
85+
b. Use localhost:5000 to test both frontend and backend changes when running both locally.
86+
module.value('api', 'http://localhost:5000/api/');
87+
3. Refresh the app in your browser to load the updated script.
88+
89+
## Configure the API for CORS
90+
The API includes a CORS (cross-origin resource sharing) filter that allows traffic from localhost:8080.
8291
```java
8392
private static UrlBasedCorsConfigurationSource configurationSource() {
8493
CorsConfiguration config = new CorsConfiguration();
@@ -94,6 +103,20 @@ The API includes a [CORS (cross-origin resource sharing) filter](https://github.
94103
```
95104
This lets requests originating from a frontend hosted locally on port 8080 to send requests to the API hosted on a different local port (i.e. localhost:5000) or on Elastic Beanstalk. This lets you work on the frontend locally and see changes immediately without needing to deploy the source to your environment.
96105

97-
When you run both the API and frontend in Elastic Beanstalk, CORS is not required because the scripts that contact the API and the API itself are hosted on the same domain. If you want to run the frontend on a different port locally, or even host it on a completely different domain, add an allowed origin to the filter to whitelist it in the API.
106+
When you run both the API and frontend in the same Elastic Beanstalk environment, CORS is not required because the scripts that contact the API and the API itself are hosted on the same domain. If you want to run the frontend on a different port locally, or even host it on a completely different domain, add an allowed origin to the filter to whitelist it in the API.
107+
108+
1. Open [src/main/java/scorekeepSimpleCORSFilter.java](https://github.com/awslabs/eb-java-scorekeep/blob/master/src/main/java/scorekeep/SimpleCORSFilter.java)
109+
2. Add an allowed origin with the URL serving the frontend.
110+
111+
config.addAllowedOrigin("http://localhost:8080");
112+
config.addAllowedOrigin("http://scorekeep.XXXXXXXX.elasticbeanstalk.com");
98113

114+
3. Save the files and commit your changes.
115+
116+
eb-java-scorekeep$ git commit -am "Update API domain"
117+
118+
4. Create an archive of the updated source code.
119+
120+
eb-java-scorekeep$ git archive -o scorekeep-v1.zip HEAD
99121

122+
5. Open your environment's Dashboard in the [Elastic Beanstalk Management Console](console.aws.amazon.com/elasticbeanstalk/home) and deploy the updated code.

0 commit comments

Comments
 (0)