mirror of
https://github.com/SinTan1729/chhoto-url
synced 2024-12-26 23:58:35 -06:00
Allow setting site_url
This commit is contained in:
parent
e2656ff94e
commit
deeba64e74
5 changed files with 45 additions and 10 deletions
16
README.md
16
README.md
|
@ -28,6 +28,8 @@ unnecessary features, or they didn't have all the features I wanted.
|
||||||
- Provides a simple API for adding new short links
|
- Provides a simple API for adding new short links
|
||||||
- Counts number of hits for each short link in a privacy respecting way
|
- Counts number of hits for each short link in a privacy respecting way
|
||||||
i.e. only the hit is recorded, and nothing else
|
i.e. only the hit is recorded, and nothing else
|
||||||
|
- Allows setting the URL of your website, in case you want to conveniently generate
|
||||||
|
short links locally
|
||||||
- Links are stored in an SQLite database
|
- Links are stored in an SQLite database
|
||||||
- Available as a Docker container
|
- Available as a Docker container
|
||||||
- Backend written in Java using [Spark Java](http://sparkjava.com/), frontend
|
- Backend written in Java using [Spark Java](http://sparkjava.com/), frontend
|
||||||
|
@ -80,6 +82,9 @@ export username=<api username>
|
||||||
export password=<api password>
|
export password=<api password>
|
||||||
# Sets where the database exists. Can be local or remote (optional)
|
# Sets where the database exists. Can be local or remote (optional)
|
||||||
export db_url=<url> # Default: './urls.sqlite'
|
export db_url=<url> # Default: './urls.sqlite'
|
||||||
|
# Sets the url of website, so that it displays that even when accessed
|
||||||
|
# locally (optional, defaults to hostname you're accessing it on)
|
||||||
|
export site_url=<url>
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3. Run it
|
### 3. Run it
|
||||||
|
@ -113,6 +118,17 @@ docker run -p 4567:4567 \
|
||||||
-e db_url=/urls.sqlite \
|
-e db_url=/urls.sqlite \
|
||||||
-d simply-shorten:latest
|
-d simply-shorten:latest
|
||||||
```
|
```
|
||||||
|
1.b Further, set the URL of your website (optional)
|
||||||
|
```
|
||||||
|
touch ./urls.sqlite
|
||||||
|
docker run -p 4567:4567 \
|
||||||
|
-e username="username" \
|
||||||
|
-e password="password" \
|
||||||
|
-v ./urls.sqlite:/urls.sqlite \
|
||||||
|
-e db_url=/urls.sqlite \
|
||||||
|
-e site_url="https://www.example.com" \
|
||||||
|
-d simply-shorten:latest
|
||||||
|
```
|
||||||
|
|
||||||
## Disable authentication
|
## Disable authentication
|
||||||
As requested in #5, it is possible to completely disable the authentication.
|
As requested in #5, it is possible to completely disable the authentication.
|
||||||
|
|
|
@ -10,6 +10,10 @@ services:
|
||||||
# Change if you want to mount the database somewhere else
|
# Change if you want to mount the database somewhere else
|
||||||
# In this case, you can get rid of the db volume below
|
# In this case, you can get rid of the db volume below
|
||||||
# - db_url=/urls.sqlite
|
# - db_url=/urls.sqlite
|
||||||
|
# Change it in case you want to set the website name
|
||||||
|
# displayed in front of the shorturls, defaults to
|
||||||
|
# the hostname you're accessing it from
|
||||||
|
# - site_url=https://www.example.com
|
||||||
- username=admin
|
- username=admin
|
||||||
- password=$3CuReP4S$W0rD
|
- password=$3CuReP4S$W0rD
|
||||||
volumes:
|
volumes:
|
||||||
|
|
|
@ -36,6 +36,7 @@ public class App {
|
||||||
get("/all", Routes::getAll);
|
get("/all", Routes::getAll);
|
||||||
post("/new", Routes::addUrl);
|
post("/new", Routes::addUrl);
|
||||||
delete("/:shortUrl", Routes::delete);
|
delete("/:shortUrl", Routes::delete);
|
||||||
|
get("/siteUrl", Routes::siteUrl);
|
||||||
});
|
});
|
||||||
|
|
||||||
get("/:shortUrl", Routes::goToLongUrl);
|
get("/:shortUrl", Routes::goToLongUrl);
|
||||||
|
|
|
@ -42,6 +42,10 @@ public class Routes {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String siteUrl(Request req, Response res) {
|
||||||
|
return System.getenv().getOrDefault("site_url", "unset");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String goToLongUrl(Request req, Response res) {
|
public static String goToLongUrl(Request req, Response res) {
|
||||||
String shortUrl = req.params("shortUrl");
|
String shortUrl = req.params("shortUrl");
|
||||||
|
|
|
@ -1,3 +1,12 @@
|
||||||
|
const siteName = async () => await fetch("/api/siteUrl").then(res => res.text()).then(text => {
|
||||||
|
if (text == "unset") {
|
||||||
|
return window.location.host;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const refreshData = async () => {
|
const refreshData = async () => {
|
||||||
let data = await fetch("/api/all").then(res => res.text());
|
let data = await fetch("/api/all").then(res => res.text());
|
||||||
data = data
|
data = data
|
||||||
|
@ -13,7 +22,8 @@ const refreshData = async () => {
|
||||||
displayData(data);
|
displayData(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
const displayData = (data) => {
|
const displayData = async (data) => {
|
||||||
|
site = await siteName();
|
||||||
table_box = document.querySelector(".pure-table");
|
table_box = document.querySelector(".pure-table");
|
||||||
if (data.length == 0) {
|
if (data.length == 0) {
|
||||||
table_box.style.visibility = "hidden";
|
table_box.style.visibility = "hidden";
|
||||||
|
@ -22,8 +32,7 @@ const displayData = (data) => {
|
||||||
const table = document.querySelector("#url-table");
|
const table = document.querySelector("#url-table");
|
||||||
table_box.style.visibility = "visible";
|
table_box.style.visibility = "visible";
|
||||||
table.innerHTML = ''; // Clear
|
table.innerHTML = ''; // Clear
|
||||||
data.map(TR)
|
data.forEach(tr => table.appendChild(TR(tr, site)));
|
||||||
.forEach(tr => table.appendChild(tr));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -37,10 +46,10 @@ const addAlertBox = async (s, t) => {
|
||||||
controls.appendChild(alertBox);
|
controls.appendChild(alertBox);
|
||||||
};
|
};
|
||||||
|
|
||||||
const TR = (row) => {
|
const TR = (row, site) => {
|
||||||
const tr = document.createElement("tr");
|
const tr = document.createElement("tr");
|
||||||
const longTD = TD(A(row.long));
|
const longTD = TD(A(row.long));
|
||||||
const shortTD = TD(A_INT(row.short));
|
const shortTD = TD(A_INT(row.short, site));
|
||||||
const hitsTD = TD(row.hits);
|
const hitsTD = TD(row.hits);
|
||||||
const btn = deleteButton(row.short);
|
const btn = deleteButton(row.short);
|
||||||
|
|
||||||
|
@ -52,13 +61,14 @@ const TR = (row) => {
|
||||||
return tr;
|
return tr;
|
||||||
};
|
};
|
||||||
|
|
||||||
const copyShortUrl = (s) => {
|
const copyShortUrl = async (s) => {
|
||||||
navigator.clipboard.writeText(`${window.location.host}/${s}`);
|
site = await siteName();
|
||||||
|
navigator.clipboard.writeText(`${site}/${s}`);
|
||||||
addAlertBox(`Short URL ${s} copied to clipboard!`, "green");
|
addAlertBox(`Short URL ${s} copied to clipboard!`, "green");
|
||||||
};
|
};
|
||||||
|
|
||||||
const A = (s) => `<a href='${s}'>${s}</a>`;
|
const A = (s) => `<a href='${s}'>${s}</a>`;
|
||||||
const A_INT = (s) => `<a href="javascript:copyShortUrl('${s}');">${window.location.host}/${s}</a>`;
|
const A_INT = (s, t) => `<a href="javascript:copyShortUrl('${s}');">${t}/${s}</a>`;
|
||||||
|
|
||||||
const deleteButton = (shortUrl) => {
|
const deleteButton = (shortUrl) => {
|
||||||
const td = document.createElement("td");
|
const td = document.createElement("td");
|
||||||
|
@ -99,7 +109,7 @@ const submitForm = () => {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: `${longUrl.value};${shortUrl.value}`
|
body: `${longUrl.value};${shortUrl.value}`
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then(res => {
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
addAlertBox("Short URL not valid or already in use!", "red");
|
addAlertBox("Short URL not valid or already in use!", "red");
|
||||||
return "error";
|
return "error";
|
||||||
|
@ -107,7 +117,7 @@ const submitForm = () => {
|
||||||
else {
|
else {
|
||||||
return res.text();
|
return res.text();
|
||||||
}
|
}
|
||||||
}).then((text) => {
|
}).then(text => {
|
||||||
if (text != "error") {
|
if (text != "error") {
|
||||||
copyShortUrl(text);
|
copyShortUrl(text);
|
||||||
longUrl.value = "";
|
longUrl.value = "";
|
||||||
|
|
Loading…
Reference in a new issue