Base api works
This commit is contained in:
12
src/main/java/tk/draganczuk/url/App.java
Normal file
12
src/main/java/tk/draganczuk/url/App.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package tk.draganczuk.url;
|
||||
|
||||
import static spark.Spark.*;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
get("/all", Routes::getAll);
|
||||
post("/new", Routes::addUrl);
|
||||
get("/:shortUrl", Routes::goToLongUrl);
|
||||
}
|
||||
}
|
||||
36
src/main/java/tk/draganczuk/url/Pair.java
Normal file
36
src/main/java/tk/draganczuk/url/Pair.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package tk.draganczuk.url;
|
||||
|
||||
/**
|
||||
* Pair
|
||||
*/
|
||||
public class Pair<T, U> {
|
||||
|
||||
private T left;
|
||||
private U right;
|
||||
|
||||
|
||||
public Pair(T left, U right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public static <T, U> Pair<T, U> of(T left, U right){
|
||||
return new Pair<T,U>(left, right);
|
||||
}
|
||||
|
||||
public T getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(T left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public U getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(U right) {
|
||||
this.right = right;
|
||||
}
|
||||
}
|
||||
54
src/main/java/tk/draganczuk/url/Routes.java
Normal file
54
src/main/java/tk/draganczuk/url/Routes.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package tk.draganczuk.url;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
|
||||
import spark.Request;
|
||||
import spark.Response;
|
||||
|
||||
public class Routes {
|
||||
|
||||
private static UrlFile urlFile;
|
||||
|
||||
static{
|
||||
try {
|
||||
urlFile = new UrlFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> getAll(Request req, Response res) throws IOException{
|
||||
return urlFile.getAll();
|
||||
}
|
||||
|
||||
public static String addUrl(Request req, Response res) {
|
||||
String longUrl = req.queryParams("long");
|
||||
String shortUrl = req.queryParams("short");
|
||||
|
||||
if (shortUrl == null) {
|
||||
shortUrl = Utils.randomString();
|
||||
}
|
||||
|
||||
return urlFile.addUrl(longUrl, shortUrl);
|
||||
}
|
||||
|
||||
public static String goToLongUrl(Request req, Response res){
|
||||
String shortUrl = req.params("shortUrl");
|
||||
var longUrlOpt = urlFile
|
||||
.findForShortUrl(shortUrl);
|
||||
|
||||
if(longUrlOpt.isEmpty()){
|
||||
res.status(404);
|
||||
return "";
|
||||
}
|
||||
|
||||
res.redirect(longUrlOpt.get());
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
60
src/main/java/tk/draganczuk/url/UrlFile.java
Normal file
60
src/main/java/tk/draganczuk/url/UrlFile.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package tk.draganczuk.url;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
|
||||
|
||||
public class UrlFile {
|
||||
|
||||
private File file;
|
||||
|
||||
public UrlFile() throws IOException{
|
||||
String path = System.getProperty("file.location", "./urls.csv");
|
||||
this.file = new File(path);
|
||||
if(! file.exists()){
|
||||
file.createNewFile();
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getAll() throws IOException{
|
||||
return Files.readAllLines(file.toPath());
|
||||
}
|
||||
|
||||
public String addUrl(String longURL, String shortUrl){
|
||||
String entry = String.format("%s,%s",shortUrl,longURL);
|
||||
try {
|
||||
var lineOpt = Files.lines(file.toPath())
|
||||
.filter(line -> line.equals(entry))
|
||||
.findAny();
|
||||
if(lineOpt.isEmpty()){
|
||||
Files.writeString(file.toPath(), entry + System.lineSeparator(), StandardOpenOption.APPEND);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
public Optional<String> findForShortUrl(String shortUrl){
|
||||
try {
|
||||
return Files.lines(file.toPath())
|
||||
.map(this::splitLine)
|
||||
.filter(pair -> pair.getLeft().equals(shortUrl))
|
||||
.map(Pair::getRight)
|
||||
.findAny();
|
||||
} catch (IOException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public Pair<String, String> splitLine(String line){
|
||||
var split = line.split(",");
|
||||
return new Pair<>(split[0], split[1]);
|
||||
}
|
||||
}
|
||||
23
src/main/java/tk/draganczuk/url/Utils.java
Normal file
23
src/main/java/tk/draganczuk/url/Utils.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package tk.draganczuk.url;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Utils {
|
||||
private static final Random random = new Random(System.currentTimeMillis());
|
||||
|
||||
public static String randomString() {
|
||||
int leftLimit = 48; // numeral '0'
|
||||
int rightLimit = 122; // letter 'z'
|
||||
int targetStringLength = 10;
|
||||
|
||||
String generatedString = random.ints(leftLimit, rightLimit + 1)
|
||||
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
|
||||
.limit(targetStringLength)
|
||||
.collect(StringBuilder::new,
|
||||
StringBuilder::appendCodePoint,
|
||||
StringBuilder::append)
|
||||
.toString();
|
||||
|
||||
return generatedString;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user