diff --git a/.env.example b/.env.example index fb62506..decbc0f 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,3 @@ -export TWILIO_ACCOUNT_SID= -export TWILIO_API_KEY= -export TWILIO_API_SECRET= +TWILIO_ACCOUNT_SID= +TWILIO_API_KEY= +TWILIO_API_SECRET= diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..67c49bb --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,15 @@ +version: 2 +updates: +- package-ecosystem: maven + directory: "/." + schedule: + interval: daily + open-pull-requests-limit: 10 + ignore: + - dependency-name: com.twilio.sdk:twilio + versions: + - 8.10.0 + - 8.6.1 + - 8.7.0 + - 8.8.0 + - 8.9.0 diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..4a78c1e --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,32 @@ +# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven + +name: Java CI with Maven + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ${{ matrix.platform }} + strategy: + max-parallel: 3 + matrix: + platform: [windows-latest, macos-latest, ubuntu-latest] + + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 11 + uses: actions/setup-java@v2 + with: + java-version: '11' + distribution: 'adopt' + cache: maven + - name: Build with Maven + run: mvn package + - name: Run tests + run: mvn clean test diff --git a/README.md b/README.md index 738a7b9..5617eb2 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ all the config values we need to run the application: | Config Value | Description | | :------------- |:------------- | Account SID | Your primary Twilio account identifier - find this [in the console here](https://www.twilio.com/console). -API Key | Used to authenticate - [generate one here](https://www.twilio.com/console/video/runtime/api-keys). -API Secret | Used to authenticate - [just like the above, you'll get one here](https://www.twilio.com/console/video/runtime/api-keys). +API Key | Used to authenticate - [generate one here](https://www.twilio.com/console/runtime/api-keys/create). +API Secret | Used to authenticate - [just like the above, you'll get one here](https://www.twilio.com/console/runtime/api-keys/create). ## A Note on API Keys diff --git a/pom.xml b/pom.xml index 1c88d33..2fe6265 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,16 @@ logback-classic 1.2.2 + + io.github.cdimascio + dotenv-java + 2.2.0 + + + com.sparkjava + spark-template-thymeleaf + 2.7.1 + diff --git a/src/main/java/com/twilio/Webapp.java b/src/main/java/com/twilio/Webapp.java index df5dede..3b5171e 100644 --- a/src/main/java/com/twilio/Webapp.java +++ b/src/main/java/com/twilio/Webapp.java @@ -9,16 +9,36 @@ import static spark.Spark.afterAfter; import static spark.Spark.get; +import static spark.Spark.staticFileLocation; +import static spark.Spark.internalServerError; +import static spark.Spark.notFound; +import io.github.cdimascio.dotenv.Dotenv; +import spark.ModelAndView; +import spark.template.thymeleaf.ThymeleafTemplateEngine; +import java.util.HashMap; +import java.util.Map; public class Webapp { + private static Dotenv env = Dotenv.configure().ignoreIfMissing().load(); public static void main(String[] args) throws Exception { - // Load the .env file into environment - dotenv(); - // Log all requests and responses afterAfter(new LoggingFilter()); + // Handle errors + internalServerError((request, response) -> { + Map model = new HashMap(); + return new ThymeleafTemplateEngine().render( + new ModelAndView(model, "error") + ); + }); + notFound((request, response) -> { + Map model = new HashMap(); + return new ThymeleafTemplateEngine().render( + new ModelAndView(model, "error") + ); + }); + // Create an access token using our Twilio credentials get("/", (request, response) -> { // Generate a random username for the connecting client @@ -32,24 +52,12 @@ public static void main(String[] args) throws Exception { // Create access token final AccessToken token = new AccessToken.Builder( - System.getProperty("TWILIO_ACCOUNT_SID"), - System.getProperty("TWILIO_API_KEY"), - System.getProperty("TWILIO_API_SECRET") + env.get("TWILIO_ACCOUNT_SID"), + env.get("TWILIO_API_KEY"), + env.get("TWILIO_API_SECRET") ).identity(identity).grant(grant).build(); return token.toJwt(); }); } - - private static void dotenv() throws Exception { - final File env = new File(".env"); - if (!env.exists()) { - return; - } - - final Properties props = new Properties(); - props.load(new FileInputStream(env)); - props.putAll(System.getenv()); - props.entrySet().forEach(p -> System.setProperty(p.getKey().toString(), p.getValue().toString())); - } } diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html new file mode 100644 index 0000000..8999c95 --- /dev/null +++ b/src/main/resources/templates/error.html @@ -0,0 +1,9 @@ + + + + +

Something went wrong!

+

Our Engineers are on it

+ Go Home + +