Sqlite as storage backend (#1)

Some platforms has some problems with file locking, so I was forced to use an alternative. SQLite seems be the best option currently available


* Migrated to an sqlite database

* Removed unnecessary IOExceptions

* Removed an util class not needed anymore

* Updated README.md and docker-compose.yml to reflect new storage mechanism
This commit is contained in:
Przemek Dragańczuk
2020-03-24 09:07:25 +01:00
committed by GitHub
parent 25adf04903
commit 7f275bf6af
8 changed files with 137 additions and 148 deletions

View File

@@ -8,18 +8,14 @@ import java.io.IOException;
public class Routes {
private static UrlFile urlFile;
private static UrlRepository urlRepository;
static {
try {
urlFile = new UrlFile();
} catch (IOException e) {
e.printStackTrace();
}
urlRepository = new UrlRepository();
}
public static String getAll(Request req, Response res) throws IOException {
return String.join("\n", urlFile.getAll());
public static String getAll(Request req, Response res) {
return String.join("\n", urlRepository.getAll());
}
public static String addUrl(Request req, Response res) {
@@ -31,7 +27,7 @@ public class Routes {
}
if (Utils.validate(shortUrl)) {
return urlFile.addUrl(longUrl, shortUrl);
return urlRepository.addUrl(longUrl, shortUrl);
} else {
res.status(HttpStatus.BAD_REQUEST_400);
return "shortUrl not valid ([a-z0-9]+)";
@@ -41,7 +37,7 @@ public class Routes {
public static String goToLongUrl(Request req, Response res) {
String shortUrl = req.params("shortUrl");
var longUrlOpt = urlFile
var longUrlOpt = urlRepository
.findForShortUrl(shortUrl);
if (longUrlOpt.isEmpty()) {
@@ -56,15 +52,8 @@ public class Routes {
public static String delete(Request req, Response res) {
String shortUrl = req.params("shortUrl");
var longUrlOpt = urlFile
.findForShortUrl(shortUrl);
if (longUrlOpt.isEmpty()) {
res.status(404);
return "";
}
urlFile.deleteEntry(String.format("%s,%s", shortUrl, longUrlOpt.get()));
urlRepository.deleteEntry(shortUrl);
return "";
}
}