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

Commit dea20e7

Browse files
[DEVEDSB-98] Add custom error view and dotenv configuration
1 parent 5dd972f commit dea20e7

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
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=

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
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>
3742
</dependencies>
3843

3944
<build>

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,24 @@
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;
1216

1317
public class Webapp {
18+
private static Dotenv env = Dotenv.configure().ignoreIfMissing().load();
1419

1520
public static void main(String[] args) throws Exception {
1621
// Load the .env file into environment
17-
dotenv();
22+
//dotenv();
23+
staticFileLocation("/public");
1824

1925
// Log all requests and responses
2026
afterAfter(new LoggingFilter());
27+
// Using string/html to handle errors
28+
internalServerError("<html><body><h1>Something went wrong!</h1><h2>Our Engineers are on it</h2><a href='/'>Go Home</a></body></html>");
29+
notFound("<html><body><h1>Page not found</h1><a href='/'>Go Home</a></body></html>");
2130

2231
// Create an access token using our Twilio credentials
2332
get("/", (request, response) -> {
@@ -32,9 +41,9 @@ public static void main(String[] args) throws Exception {
3241

3342
// Create access token
3443
final AccessToken token = new AccessToken.Builder(
35-
System.getProperty("TWILIO_ACCOUNT_SID"),
36-
System.getProperty("TWILIO_API_KEY"),
37-
System.getProperty("TWILIO_API_SECRET")
44+
env.get("TWILIO_ACCOUNT_SID"),
45+
env.get("TWILIO_API_KEY"),
46+
env.get("TWILIO_API_SECRET")
3847
).identity(identity).grant(grant).build();
3948

4049
return token.toJwt();

0 commit comments

Comments
 (0)