From 59b6d43aea61863a18d87e4e6572917e2da0ec7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemek=20Draga=C5=84czuk?= Date: Sun, 16 Feb 2020 15:46:29 +0100 Subject: [PATCH] Added Basic auth and GZIP compression --- build.gradle | 1 + src/main/java/tk/draganczuk/url/App.java | 18 ++++++++++++++++-- src/main/java/tk/draganczuk/url/Filters.java | 20 ++++++++++++++++++++ src/main/resources/public/js/main.js | 4 ++-- 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 src/main/java/tk/draganczuk/url/Filters.java diff --git a/build.gradle b/build.gradle index 609d499..58e0dae 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,7 @@ jar { dependencies { compile "com.sparkjava:spark-core:2.8.0" + implementation 'com.qmetric:spark-authentication:1.4' } application { diff --git a/src/main/java/tk/draganczuk/url/App.java b/src/main/java/tk/draganczuk/url/App.java index 24bff3e..93a220a 100644 --- a/src/main/java/tk/draganczuk/url/App.java +++ b/src/main/java/tk/draganczuk/url/App.java @@ -1,5 +1,7 @@ package tk.draganczuk.url; +import spark.Filter; + import static spark.Spark.*; public class App { @@ -17,13 +19,25 @@ public class App { port(Integer.parseInt(System.getProperty("port", "4567"))); + // Add GZIP compression + after(Filters::addGZIP); + + // Authenticate + Filter authFilter = Filters.createAuthFilter(); + before("/index.html", authFilter); + get("/", (req, res) -> { res.redirect("/index.html"); return "Redirect"; }); - get("/all", Routes::getAll); - post("/new", Routes::addUrl); + + path("/api", () -> { + before("/*", authFilter); + get("/all", Routes::getAll); + post("/new", Routes::addUrl); + }); + get("/:shortUrl", Routes::goToLongUrl); } } diff --git a/src/main/java/tk/draganczuk/url/Filters.java b/src/main/java/tk/draganczuk/url/Filters.java new file mode 100644 index 0000000..83ad996 --- /dev/null +++ b/src/main/java/tk/draganczuk/url/Filters.java @@ -0,0 +1,20 @@ +package tk.draganczuk.url; + +import com.qmetric.spark.authentication.AuthenticationDetails; +import com.qmetric.spark.authentication.BasicAuthenticationFilter; +import spark.Filter; +import spark.Request; +import spark.Response; + +public class Filters { + public static void addGZIP(Request request, Response response) { + response.header("Content-Encoding", "gzip"); + } + + public static Filter createAuthFilter() { + String username = System.getenv("username"); + String password = System.getenv("password"); + + return new BasicAuthenticationFilter(new AuthenticationDetails(username, password)); + } +} diff --git a/src/main/resources/public/js/main.js b/src/main/resources/public/js/main.js index 0a9acbf..850ff4e 100644 --- a/src/main/resources/public/js/main.js +++ b/src/main/resources/public/js/main.js @@ -1,5 +1,5 @@ const refreshData = async () => { - let data = await fetch("/all").then(res => res.text()); + let data = await fetch("/api/all").then(res => res.text()); data = data .split("\n") .filter(line => line !== "") @@ -44,7 +44,7 @@ const submitForm = () => { const longUrl = form.elements["longUrl"]; const shortUrl = form.elements["shortUrl"]; - const url = `/new?long=${longUrl.value}&short=${shortUrl.value}`; + const url = `/api/new?long=${longUrl.value}&short=${shortUrl.value}`; fetch(url, { method: "POST"