Skip to content

Commit cc61174

Browse files
committed
readme
1 parent 583b6fe commit cc61174

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

README.md

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Use the procedures in the following sections to run the project on AWS Elastic B
88
## Create an AWS Elastic Beanstalk environment
99
Create a Java 8 SE environment in Elastic Beanstalk to host the application.
1010

11+
*To create an Elastic Beanstalk environment running the Java 8 SE platform*
1112
1. Open the AWS Elastic Beanstalk Management Console with this preconfigured link: [console.aws.amazon.com/elasticbeanstalk/#/newApplication?applicationName=scorekeep...](https://console.aws.amazon.com/elasticbeanstalk/#/newApplication?applicationName=scorekeep&solutionStackName=Java)
1213
2. Click **Create application** to create an application with an environment running the Java 8 SE platform.
1314
3. When your environment is ready, the console redirects you to the environment Dashboard.
@@ -16,13 +17,15 @@ Create a Java 8 SE environment in Elastic Beanstalk to host the application.
1617
## Give the application permission to use DynamoDB
1718
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.
1819

20+
*To add DynamoDB permissions to the instances in your Elastic Beanstalk environment*
1921
1. Open the Elastic Beanstalk instance profile in the IAM console: [aws-elasticbeanstalk-ec2-role](https://console.aws.amazon.com/iam/home#roles/aws-elasticbeanstalk-ec2-role)
2022
2. Click **Attach Policy**.
2123
3. Select [AmazonDynamoDBFullAccess](https://console.aws.amazon.com/iam/home#policies/arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess) and click **Attach Policy**.
2224

2325
## Deploy the application
24-
Deploy the source bundle that you created in the previous section to your environment.
26+
Deploy the source code for the project to your Elastic Beanstalk environment.
2527

28+
*To deploy the source code*
2629
1. Download the source bundle: [eb-java-scorekeep-v1.zip](https://github.com/awslabs/eb-java-scorekeep/releases/download/v1.1/eb-java-scorekeep-v1.zip)
2730
2. Open the [Elastic Beanstalk Management Console](console.aws.amazon.com/elasticbeanstalk/home).
2831
3. Click your environment's name to open the Dashboard.
@@ -37,48 +40,49 @@ Click through the app to explore its functionality. Use the network console in y
3740
# How it works
3841
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.
3942

40-
## Frontend
41-
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.
42-
43-
The frontend is served statically by the nginx proxy at the root path. The [nginx.conf](https://github.com/awslabs/eb-java-scorekeep/blob/master/.ebextensions/nginx/nginx.conf) file in the source code overwrites the default configuration provided by the Elastic Beanstalk Java platform to serve the frontend statically, and forward requests to paths starting with /api to the API running on port 5000.
44-
4543
## Backend
4644
The API runs at paths under /api that provide access to user, session, game, state, and move resources stored as JSON documents in DynamoDB. The API is RESTful, so you can create resources by sending HTTP POST requests to the resource path, for example /api/session. See the [test script](https://github.com/awslabs/eb-java-scorekeep/blob/master/bin/test-api.sh) for example requests with cURL.
4745

4846
The application includes a [configuration file](https://github.com/awslabs/eb-java-scorekeep/blob/master/.ebextensions/dynamodb-tables.config) that creates a DynamoDB table for each resource type.
4947

5048
The [Buildfile](https://github.com/awslabs/eb-java-scorekeep/blob/master/Buildfile) tells Elastic Beanstalk to run `gradle build` during deployment to create an executable JAR file. The [Procfile](https://github.com/awslabs/eb-java-scorekeep/blob/master/Procfile) tells Elastic Beanstalk to run that JAR on port 5000.
5149

52-
# Local testing
50+
## Frontend
51+
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.
52+
53+
The frontend is served statically by the nginx proxy at the root path. The [nginx.conf](https://github.com/awslabs/eb-java-scorekeep/blob/master/.ebextensions/nginx/nginx.conf) file in the source code overwrites the default configuration provided by the Elastic Beanstalk Java platform to serve the frontend statically, and forward requests to paths starting with /api to the API running on port 5000.
54+
55+
# Running the project locally
5356
You can run both the API and frontend locally with Gradle and the Spring Boot CLI.
5457
Clone this repository or extract the contents of the source bundle that you downloaded earlier to get started.
5558

56-
## API
59+
## Run the Scorekeep API with Gradle
5760
Use [Gradle](https://gradle.org/) to build the API and run it locally on port 5000.
58-
```
59-
eb-java-scorekeep$ gradle bootrun
60-
```
6161

62-
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+
~/eb-java-scorekeep$ gradle bootrun
63+
64+
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. If you terminate the environment, the tables are deleted. To create the tables without a running environment, you can use the configuration file as a template to [create a CloudFormation stack](console.aws.amazon.com/cloudformation/home).
6365

64-
**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.
66+
The application needs AWS credentials in order to communicate with Amazon DynamoDB. In AWS Elastic Beanstalk, Scorekeep gets credentials from the instance profile, the IAM role that is attached to the EC2 instance that runs the code. When you run the application locally, the AWS SDK for Java can retrieve credentials from files in ~/.aws/ or environment variables.
67+
68+
Follow the instructions in the AWS SDK for Java developer guide to provide access keys to the application: [Set up AWS Credentials for Development](http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html)
6569

6670
Use the test script to verify that the API works.
67-
```
68-
eb-java-scorekeep$ ./bin/test-api.sh
69-
```
71+
72+
~/eb-java-scorekeep$ ./bin/test-api.sh
73+
74+
The script targets `localhost:5000`, but can be pointed at the API running on any host by modifying the API variable at the top of the file.
7075

7176
## Run the frontend and configure it for local testing
7277
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
73-
```
74-
eb-java-scorekeep$ spring run app.groovy
75-
```
76-
Open the app in a browser: [localhost:8080](http://localhost:8080)
7778

78-
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.
79+
~/eb-java-scorekeep$ spring run app.groovy
80+
81+
Open the app in a browser: [localhost:8080](http://localhost:8080)
7982

80-
When you run the frontend locally, it needs to know the full URL of the API in order to send requests to the backend.
83+
The app loads but can't hit the API, because it's 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.
8184

85+
*To configure the web app with an absolute path to the API*
8286
1. Open [eb-java-scorekeep/public/app/scorekeep.js](https://github.com/awslabs/eb-java-scorekeep/blob/master/public/app/scorekeep.js).
8387
2. Set the value of the api module to the full URL of the API.
8488
* Use the domain name of your environment to test changes to the frontend without running the backend locally
@@ -92,7 +96,8 @@ When you run the frontend locally, it needs to know the full URL of the API in o
9296
3. Refresh the app in your browser to load the updated script.
9397

9498
## Configure the API for CORS
95-
The API includes a CORS (cross-origin resource sharing) filter that allows traffic from localhost:8080.
99+
The API includes a CORS (cross-origin resource sharing) filter that allows traffic from `localhost:8080`.
100+
96101
```java
97102
private static UrlBasedCorsConfigurationSource configurationSource() {
98103
CorsConfiguration config = new CorsConfiguration();
@@ -106,10 +111,12 @@ The API includes a CORS (cross-origin resource sharing) filter that allows traff
106111
return source;
107112
}
108113
```
109-
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.
114+
115+
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. When you make changes to the HTML or CSS in the app, simply refresh the browser.
110116

111117
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.
112118

119+
*To extend the CORS configuration to allow cross-origin requests from specific domains*
113120
1. Open [src/main/java/scorekeepSimpleCORSFilter.java](https://github.com/awslabs/eb-java-scorekeep/blob/master/src/main/java/scorekeep/SimpleCORSFilter.java)
114121
2. Add an allowed origin with the URL serving the frontend.
115122

@@ -118,10 +125,10 @@ When you run both the API and frontend in the same Elastic Beanstalk environment
118125

119126
3. Save the files and commit your changes.
120127

121-
eb-java-scorekeep$ git commit -am "Update API domain"
128+
~/eb-java-scorekeep$ git commit -am "Update API domain"
122129

123130
4. Create an archive of the updated source code.
124131

125-
eb-java-scorekeep$ git archive -o scorekeep-v1.zip HEAD
132+
~/eb-java-scorekeep$ git archive -o scorekeep-v1.zip HEAD
126133

127134
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)