Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .example.env
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ LINK_LENGTH=6
# Default value omits o, O, 0, i, I, l, 1, and j to avoid confusion when reading the URL
LINK_CUSTOM_ALPHABET=abcdefghkmnpqrstuvwxyzABCDEFGHKLMNPQRSTUVWXYZ23456789

# Optional - Allowed locales and translations Supported locales:
# en (English), zh_CN (Simplified Chinese), zh_TW (Traditional Chinese)
LOCALES=en

# Optional - Tells the app that it's running behind a proxy server
# and that it should get the IP address from that proxy server
# if you're not using a proxy server then set this to false, otherwise users can override their IP address
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ docs/api/static
**/.DS_Store
db/*
!db/.gitkeep
.history/**
**.bak
tags
116 changes: 111 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"geoip-lite": "1.4.10",
"hbs": "4.2.0",
"helmet": "7.1.0",
"i18n": "0.15.1",
"ioredis": "5.4.2",
"isbot": "5.1.19",
"jsonwebtoken": "9.0.2",
Expand Down
24 changes: 23 additions & 1 deletion server/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require("dotenv").config();
const { cleanEnv, num, str, bool } = require("envalid");
const { cleanEnv, num, str, bool, makeValidator } = require("envalid");
const { readFileSync } = require("node:fs");

const supportedDBClients = [
Expand All @@ -26,12 +26,34 @@ if (process.argv.includes("--production")) {
process.env.NODE_ENV = "production";
}

// custom locales validator
const allowedLocales = ["en", "zh_CN","zh_TW"];
const localesValidator = makeValidator(function (locales) {
if (typeof locales !== "string") return "en";
return locales
.split(",")
.map(l => l.trim().toLowerCase())
.map(function (locale) {
const index = allowedLocales.findIndex(l => l.toLowerCase() === locale);
if (index !== -1) {
return allowedLocales[index];
} else {
throw new Error(
`Locale is not supported: ${locale}.` +
`Available locales: ${allowedLocales.join(", ")}`
);
}
})
.join(",");
});

const spec = {
PORT: num({ default: 3000 }),
SITE_NAME: str({ example: "Kutt", default: "Kutt" }),
DEFAULT_DOMAIN: str({ example: "kutt.it", default: "localhost:3000" }),
LINK_LENGTH: num({ default: 6 }),
LINK_CUSTOM_ALPHABET: str({ default: "abcdefghkmnpqrstuvwxyzABCDEFGHKLMNPQRSTUVWXYZ23456789" }),
LOCALES: localesValidator({ default: "en,zh_CN,zh_TW" }),
TRUST_PROXY: bool({ default: true }),
DB_CLIENT: str({ choices: supportedDBClients, default: "better-sqlite3" }),
DB_FILENAME: str({ default: "db/data" }),
Expand Down
5 changes: 3 additions & 2 deletions server/handlers/helpers.handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { RedisStore: RateLimitRedisStore } = require("rate-limit-redis");
const { rateLimit: expressRateLimit } = require("express-rate-limit");
const { validationResult } = require("express-validator");
const i18n = require("i18n");

const { CustomError } = require("../utils");
const query = require("../queries");
Expand All @@ -14,7 +15,7 @@ function error(error, req, res, _next) {
console.error(error.message);
}

const message = error instanceof CustomError ? error.message : "An error occurred.";
const message = error instanceof CustomError ? error.message : i18n.__("backbone.errorMsg");
const statusCode = error.statusCode ?? 500;

if (req.isHTML && req.viewTemplate) {
Expand All @@ -25,7 +26,7 @@ function error(error, req, res, _next) {

if (req.isHTML) {
res.render("error", {
message: "An error occurred. Please try again later."
message: i18n.__("backbone.errorAndTryAgainMsg"),
});
return;
}
Expand Down
Loading