Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Commit 9980556

Browse files
authored
Merge pull request #98 from TwilioDevEd/DEVEDSB-98-update-tutorials
Devedsb 98 update tutorials
2 parents 5dd972f + 606d1cc commit 9980556

File tree

5 files changed

+80
-21
lines changed

5 files changed

+80
-21
lines changed

.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export TWILIO_ACCOUNT_SID=
2-
export TWILIO_API_KEY=
3-
export TWILIO_API_SECRET=
1+
TWILIO_ACCOUNT_SID=
2+
TWILIO_API_KEY=
3+
TWILIO_API_SECRET=

.github/workflows/maven.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3+
4+
name: Java CI with Maven
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
pull_request:
10+
branches: [ master ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ${{ matrix.platform }}
16+
strategy:
17+
max-parallel: 3
18+
matrix:
19+
platform: [windows-latest, macos-latest, ubuntu-latest]
20+
21+
steps:
22+
- uses: actions/checkout@v2
23+
- name: Set up JDK 11
24+
uses: actions/setup-java@v2
25+
with:
26+
java-version: '11'
27+
distribution: 'adopt'
28+
cache: maven
29+
- name: Build with Maven
30+
run: mvn package
31+
- name: Run tests
32+
run: mvn clean test

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434
<artifactId>logback-classic</artifactId>
3535
<version>1.2.2</version>
3636
</dependency>
37+
<dependency>
38+
<groupId>io.github.cdimascio</groupId>
39+
<artifactId>dotenv-java</artifactId>
40+
<version>2.2.0</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.sparkjava</groupId>
44+
<artifactId>spark-template-thymeleaf</artifactId>
45+
<version>2.7.1</version>
46+
</dependency>
3747
</dependencies>
3848

3949
<build>

src/main/java/com/twilio/Webapp.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,36 @@
99

1010
import static spark.Spark.afterAfter;
1111
import static spark.Spark.get;
12+
import static spark.Spark.staticFileLocation;
13+
import static spark.Spark.internalServerError;
14+
import static spark.Spark.notFound;
15+
import io.github.cdimascio.dotenv.Dotenv;
16+
import spark.ModelAndView;
17+
import spark.template.thymeleaf.ThymeleafTemplateEngine;
18+
import java.util.HashMap;
19+
import java.util.Map;
1220

1321
public class Webapp {
22+
private static Dotenv env = Dotenv.configure().ignoreIfMissing().load();
1423

1524
public static void main(String[] args) throws Exception {
16-
// Load the .env file into environment
17-
dotenv();
18-
1925
// Log all requests and responses
2026
afterAfter(new LoggingFilter());
2127

28+
// Handle errors
29+
internalServerError((request, response) -> {
30+
Map<String, Object> model = new HashMap<String, Object>();
31+
return new ThymeleafTemplateEngine().render(
32+
new ModelAndView(model, "error")
33+
);
34+
});
35+
notFound((request, response) -> {
36+
Map<String, Object> model = new HashMap<String, Object>();
37+
return new ThymeleafTemplateEngine().render(
38+
new ModelAndView(model, "error")
39+
);
40+
});
41+
2242
// Create an access token using our Twilio credentials
2343
get("/", (request, response) -> {
2444
// Generate a random username for the connecting client
@@ -32,24 +52,12 @@ public static void main(String[] args) throws Exception {
3252

3353
// Create access token
3454
final AccessToken token = new AccessToken.Builder(
35-
System.getProperty("TWILIO_ACCOUNT_SID"),
36-
System.getProperty("TWILIO_API_KEY"),
37-
System.getProperty("TWILIO_API_SECRET")
55+
env.get("TWILIO_ACCOUNT_SID"),
56+
env.get("TWILIO_API_KEY"),
57+
env.get("TWILIO_API_SECRET")
3858
).identity(identity).grant(grant).build();
3959

4060
return token.toJwt();
4161
});
4262
}
43-
44-
private static void dotenv() throws Exception {
45-
final File env = new File(".env");
46-
if (!env.exists()) {
47-
return;
48-
}
49-
50-
final Properties props = new Properties();
51-
props.load(new FileInputStream(env));
52-
props.putAll(System.getenv());
53-
props.entrySet().forEach(p -> System.setProperty(p.getKey().toString(), p.getValue().toString()));
54-
}
5563
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<body>
5+
<h1>Something went wrong! </h1>
6+
<h2>Our Engineers are on it</h2>
7+
<a href="/">Go Home</a>
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)