Skip to content

Commit 7f83505

Browse files
committed
use environment variable to set email address for notifications.
1 parent cad614b commit 7f83505

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ Deploy the source code for the project to your Elastic Beanstalk environment.
4949
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.
5050

5151
## Configure Notifications
52-
The API uses Amazon SNS to send a notification email whenever a game ends. Configure your email address in the source code and redeploy to enable notifications.
52+
The API uses Amazon SNS to send a notification email when a game ends. To enable notifications, configure your email address in an environment variable.
5353

5454
*To enable notifications*
55-
1. Clone this repository
56-
2. Add your email address to the `env.config` configuration file in the `.ebextensions` folder.
57-
3. [Create a source bundle](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/applications-sourcebundle.html).
58-
4. [Deploy the source bundle](http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.deploy-existing-version.html#deployments-newversion).
55+
1. Open your environment's page in the [environment management console](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environments-console.html).
56+
2. Choose **Configuration**.
57+
3. Choose **Software Configuration**
58+
4. Under **Environment Properties**, set **NOTIFICATION_TOPIC** to your email address.
5959
5. Check your email for a subscription confirmation.
6060
6. Complete a game to trigger a notification.
6161

src/main/java/scorekeep/Utils.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
66
import com.amazonaws.services.sns.model.PublishRequest;
77
import com.amazonaws.services.sns.model.PublishResult;
8-
8+
import com.amazonaws.services.sns.model.SubscribeRequest;
99
import org.slf4j.Logger;
1010
import org.slf4j.LoggerFactory;
1111

@@ -23,4 +23,15 @@ public static void sendNotification(String subject, String body) {
2323
PublishResult publishResult = snsclient.publish(publishRequest);
2424
logger.info("Email sent: " + publishResult.getMessageId());
2525
}
26+
27+
/*
28+
* Create an SNS subscription.
29+
*/
30+
public static void createSubscription() {
31+
String topicarn = System.getenv("NOTIFICATION_TOPIC");
32+
String emailaddress = System.getenv("NOTIFICATION_EMAIL");
33+
SubscribeRequest subRequest = new SubscribeRequest(topicarn, "email", emailaddress);
34+
snsclient.subscribe(subRequest);
35+
}
36+
2637
}

src/main/java/scorekeep/WebConfig.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@
33
import org.springframework.context.annotation.Bean;
44
import javax.servlet.Filter;
55

6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
69
@Configuration
710
public class WebConfig {
11+
private static final Logger logger = LoggerFactory.getLogger(WebConfig.class);
812

913
@Bean
1014
public Filter SimpleCORSFilter() {
1115
return new SimpleCORSFilter();
1216
}
13-
}
17+
18+
static {
19+
if ( System.getenv("NOTIFICATION_EMAIL") != null ){
20+
Utils.createSubscription();
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)