mirror of
https://github.com/minoplhy/chhoto-url.git
synced 2024-12-22 19:57:57 +00:00
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:
parent
25adf04903
commit
7f275bf6af
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,3 +10,4 @@ url.iml
|
||||
.settings/
|
||||
urls.csv
|
||||
.env
|
||||
urls.sqlite
|
||||
|
26
README.md
26
README.md
@ -13,7 +13,7 @@ unnecessary features, or they didn't have all the features I wanted.
|
||||
to the correct long URL (you'd think that's a standard feature, but
|
||||
apparently it's not)
|
||||
- Provides a simple API for adding new short links
|
||||
- Links are stored in a plaintext CSV file
|
||||
- Links are stored in an SQLite database
|
||||
- Available as a Docker container (there is no image on docker hub _yet_)
|
||||
- Backend written in Java using [Spark Java](http://sparkjava.com/), frontend
|
||||
written in plain HTML and vanilla JS, using [Pure CSS](https://purecss.io/)
|
||||
@ -45,9 +45,11 @@ in order to speed up future builds.
|
||||
|
||||
### 2. Set environment variables
|
||||
```bash
|
||||
# Required for authentication
|
||||
export username=<api username>
|
||||
export password=<api password>
|
||||
export file.location=<file location> # opitonal
|
||||
# Sets where the database exists. Can be local or remote (optional)
|
||||
export db.url=<url> # Default: './urls.sqlite'
|
||||
```
|
||||
|
||||
### 3. Run it
|
||||
@ -64,20 +66,24 @@ docker build . -t url:latest
|
||||
```
|
||||
2. Run the image
|
||||
```
|
||||
docker run -p 4567:4567 -d url:latest
|
||||
docker run -p 4567:4567
|
||||
-d url:latest
|
||||
-e username="username"
|
||||
-e password="password"
|
||||
-d url:latest
|
||||
```
|
||||
2.a Make the CSV file available to host
|
||||
2.a Make the database file available to host (optional)
|
||||
```
|
||||
touch ./urls.csv
|
||||
docker run -p 4567:4567 \
|
||||
-e file.location=/urls.csv \
|
||||
-e username="username"
|
||||
-e password="password"
|
||||
-v ./urls.csv:/urls.csv \
|
||||
-d url:1.0
|
||||
-e username="username" \
|
||||
-e password="password" \
|
||||
-v ./urls.sqlite:/urls.csv \
|
||||
-e db.url=/urls.csv \
|
||||
-d url:latest
|
||||
```
|
||||
## `docker-compose`
|
||||
There is a sample `docker-compose.yml` file in this repository. You can use it
|
||||
There is a sample `docker-compose.yml` file in this repository configured for Traefik. You can use it
|
||||
as a base, modifying it as needed. Run it with
|
||||
```
|
||||
docker-compose up -d --build
|
||||
|
@ -24,6 +24,8 @@ jar {
|
||||
dependencies {
|
||||
compile "com.sparkjava:spark-core:2.8.0"
|
||||
compile 'com.qmetric:spark-authentication:1.4'
|
||||
compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.30.1'
|
||||
|
||||
}
|
||||
|
||||
application {
|
||||
|
@ -6,11 +6,11 @@ services:
|
||||
context: .
|
||||
container_name: url
|
||||
environment:
|
||||
- file.location=/urls.csv
|
||||
- db.url=/urls.sqlite
|
||||
- username=${URL_LOGIN}
|
||||
- password=${URL_PASSWORD}
|
||||
volumes:
|
||||
- ./urls.csv:/urls.csv
|
||||
- ./urls.sqlite:/urls.sqlite
|
||||
networks:
|
||||
- ${NETWORK}
|
||||
labels:
|
||||
|
@ -1,36 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -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 "";
|
||||
}
|
||||
}
|
||||
|
@ -1,82 +0,0 @@
|
||||
package tk.draganczuk.url;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
public class UrlFile {
|
||||
|
||||
private File file;
|
||||
|
||||
public UrlFile() throws IOException{
|
||||
String path = System.getenv().getOrDefault("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]);
|
||||
}
|
||||
|
||||
public void deleteEntry(String entry) {
|
||||
try {
|
||||
File tmp = File.createTempFile(file.getName(), ".tmp");
|
||||
if (!tmp.exists()) {
|
||||
tmp.createNewFile();
|
||||
}
|
||||
|
||||
Files.lines(file.toPath())
|
||||
.filter(line -> !line.equals(entry))
|
||||
.forEach(line -> {
|
||||
try {
|
||||
Files.writeString(tmp.toPath(), line + "\n", StandardOpenOption.APPEND);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
Files.move(tmp.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
109
src/main/java/tk/draganczuk/url/UrlRepository.java
Normal file
109
src/main/java/tk/draganczuk/url/UrlRepository.java
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user