mirror of
https://github.com/minoplhy/chhoto-url.git
synced 2024-11-23 01:26:46 +00:00
Added Basic auth and GZIP compression
This commit is contained in:
parent
eba16d518e
commit
59b6d43aea
@ -23,6 +23,7 @@ jar {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile "com.sparkjava:spark-core:2.8.0"
|
compile "com.sparkjava:spark-core:2.8.0"
|
||||||
|
implementation 'com.qmetric:spark-authentication:1.4'
|
||||||
}
|
}
|
||||||
|
|
||||||
application {
|
application {
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package tk.draganczuk.url;
|
package tk.draganczuk.url;
|
||||||
|
|
||||||
|
import spark.Filter;
|
||||||
|
|
||||||
import static spark.Spark.*;
|
import static spark.Spark.*;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
@ -17,13 +19,25 @@ public class App {
|
|||||||
|
|
||||||
port(Integer.parseInt(System.getProperty("port", "4567")));
|
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) -> {
|
get("/", (req, res) -> {
|
||||||
res.redirect("/index.html");
|
res.redirect("/index.html");
|
||||||
return "Redirect";
|
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);
|
get("/:shortUrl", Routes::goToLongUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
20
src/main/java/tk/draganczuk/url/Filters.java
Normal file
20
src/main/java/tk/draganczuk/url/Filters.java
Normal file
@ -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));
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
const refreshData = async () => {
|
const refreshData = async () => {
|
||||||
let data = await fetch("/all").then(res => res.text());
|
let data = await fetch("/api/all").then(res => res.text());
|
||||||
data = data
|
data = data
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.filter(line => line !== "")
|
.filter(line => line !== "")
|
||||||
@ -44,7 +44,7 @@ const submitForm = () => {
|
|||||||
const longUrl = form.elements["longUrl"];
|
const longUrl = form.elements["longUrl"];
|
||||||
const shortUrl = form.elements["shortUrl"];
|
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, {
|
fetch(url, {
|
||||||
method: "POST"
|
method: "POST"
|
||||||
|
Loading…
Reference in New Issue
Block a user