Skip to content

Commit 9c1e143

Browse files
authored
Merge pull request #11 from FOSWLY/dev
v2.1.3
2 parents 7db2963 + 0ff5614 commit 9c1e143

File tree

9 files changed

+112
-140
lines changed

9 files changed

+112
-140
lines changed

.github/workflows/deploy.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
jobs:
9+
deploy:
10+
name: Deploy
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Configure SSH 🔧
14+
run: |
15+
mkdir -p ~/.ssh
16+
echo -e "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
17+
chmod 600 ~/.ssh/id_rsa
18+
ssh-keyscan -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts
19+
- name: Connect and deploy 🔧
20+
run: |
21+
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "
22+
sudo ./translate-backend-deploy.sh GITHUB_REPOSITORY=${{ github.repository }} LOKI_HOST=${{ secrets.LOKI_HOST }} LOKI_USER=${{ secrets.LOKI_USER }} LOKI_PASSWORD=${{ secrets.LOKI_PASSWORD }} SERVICE_PORT=${{ secrets.SERVICE_PORT }} ALLOW_UNSAFE_EVAL=${{ secrets.ALLOW_UNSAFE_EVAL }} &&
23+
exit
24+
"

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@
2828

2929
Если вы хотите использовать несколько версий сервера переводов, то вам необходимо реализовать версионирование с помощью путей через ваш веб-сервер.
3030

31-
Наш сервер [translate.toil.cc](https://translate.toil.cc) поддерживает версионирование с помощью нескольких путей. Доступные версии API:
32-
33-
1. `/v2` - (поддерживается) - использовать версию, которая совместима с новыми ответами из 2.1.0
34-
2. без указания версии (устаревшая) - использовать устаревшую версию 2.0, которая будет удалена через N время
31+
Наш сервер [translate.toil.cc](https://translate.toil.cc) поддерживает только самую последнюю версию.
3532

3633
## 📦 Деплой
3734

@@ -48,8 +45,8 @@ git clone https://github.com/FOSWLY/translate-backend
4845
bun install
4946
```
5047

51-
1. Переименуйте `.example.env` в `.env` и установите необходимые поля
52-
2. Запустите сервер
48+
4. Переименуйте `.example.env` в `.env` и установите необходимые поля
49+
5. Запустите сервер
5350

5451
```bash
5552
bun start

bun.lock

Lines changed: 45 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2.1.3
2+
3+
- Изменен метод установки cors на установку с помощью библиотеки `@elysiajs/cors`
4+
- Библиотека `pino` и ее модули заменены на `@vaylo/pino`
5+
- Обновлены зависимости
6+
17
# 2.1.2
28

39
- Добавлена проверка на allowUnsafeEval при создание клиента перевода

package.json

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "translate-backend",
3-
"version": "2.1.2",
3+
"version": "2.1.3",
44
"author": "Toil",
55
"repository": {
66
"type": "git",
@@ -13,29 +13,28 @@
1313
"prepare": "husky"
1414
},
1515
"dependencies": {
16+
"@elysiajs/cors": "^1.2.0",
1617
"@elysiajs/swagger": "^1.2.2",
17-
"@toil/translate": "^1.0.6",
18-
"elysia": "^1.2.21",
19-
"elysia-http-status-code": "^1.0.9",
20-
"pino": "^9.6.0",
21-
"pino-loki": "^2.5.0"
18+
"@toil/translate": "^1.0.7",
19+
"@vaylo/pino": "^1.0.1",
20+
"elysia": "^1.2.25",
21+
"elysia-http-status-code": "^1.0.9"
2222
},
2323
"devDependencies": {
24-
"@sinclair/typebox": "^0.34.28",
25-
"bun-types": "^1.2.3",
26-
"eslint": "^9.21.0",
27-
"eslint-plugin-oxlint": "^0.15.11",
24+
"@sinclair/typebox": "^0.34.33",
25+
"bun-types": "^1.2.9",
26+
"eslint": "^9.24.0",
27+
"eslint-plugin-oxlint": "^0.16.6",
2828
"husky": "^9.1.7",
29-
"oxlint": "^0.15.11",
30-
"pino-pretty": "^13.0.0",
31-
"typescript-eslint": "^8.24.1"
29+
"oxlint": "^0.16.6",
30+
"typescript-eslint": "^8.29.1"
3231
},
3332
"module": "src/index.js",
3433
"bun-create": {
3534
"start": "bun run src/index.ts"
3635
},
3736
"type": "module",
3837
"peerDependencies": {
39-
"typescript": "^5.6.3"
38+
"typescript": "^5.7.3"
4039
}
4140
}

src/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ export default Value.Parse(ConfigSchema, {
2424
logging: {
2525
level: Bun.env.NODE_ENV === "production" ? "info" : "debug",
2626
logPath: path.join(__dirname, "..", "logs"),
27+
logToFile: Bun.env.LOG_TO_FILE === "true",
2728
loki: {
2829
host: Bun.env.LOKI_HOST,
2930
user: Bun.env.LOKI_USER,
3031
password: Bun.env.LOKI_PASSWORD,
31-
label: Bun.env.LOKI_LABEL,
32+
label: Bun.env.LOKI_LABEL ?? "translate-backend",
3233
},
3334
},
3435
});

src/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import fs from "node:fs/promises";
22

33
import { Elysia } from "elysia";
44
import { swagger } from "@elysiajs/swagger";
5+
import { cors } from "@elysiajs/cors";
6+
import { HttpStatusCode } from "elysia-http-status-code";
57

68
import config from "./config";
79
import { log } from "./logging";
@@ -11,7 +13,6 @@ import translate from "./controllers/translate";
1113
import detect from "./controllers/detect";
1214
import getLangs from "./controllers/getLangs";
1315
import { LibreTransalteDisabledError } from "./errors";
14-
import { HttpStatusCode } from "elysia-http-status-code";
1516

1617
if (!(await fs.exists(config.logging.logPath))) {
1718
await fs.mkdir(config.logging.logPath, { recursive: true });
@@ -48,11 +49,7 @@ const app = new Elysia({
4849
}),
4950
)
5051
.use(HttpStatusCode())
51-
.onRequest(({ set }) => {
52-
for (const [key, val] of Object.entries(config.cors)) {
53-
set.headers[key] = val;
54-
}
55-
})
52+
.use(cors(config.cors))
5653
.error({
5754
LIBRE_TRANSLATE_DISABLED: LibreTransalteDisabledError,
5855
})

src/logging.ts

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,15 @@
1-
import * as path from "node:path";
2-
import { pino, type TransportMultiOptions, type TransportTargetOptions } from "pino";
3-
41
import config from "@/config";
2+
import { PinoClient } from "@vaylo/pino";
53

6-
const { loki } = config.logging;
7-
const startingDate = new Date().toISOString().split("T")[0];
8-
9-
type PinoOpts = Parameters<typeof pino>[0] & {
10-
transport: TransportMultiOptions & { targets: TransportTargetOptions[] };
11-
};
12-
13-
// https://github.com/pinojs/pino/issues/1791
14-
// if don't take out the options separately, it willn't work
15-
const opts: PinoOpts = {
16-
level: config.logging.level,
17-
redact: {
18-
// these just cause clutter
19-
paths: ["pid", "hostname"],
20-
remove: true,
21-
},
22-
transport: {
23-
targets: [],
24-
},
25-
};
4+
const {
5+
logging: { loki, level, logPath, logToFile },
6+
} = config;
267

27-
// it may be higher level than global, but it cann't be lower
28-
opts.transport.targets.push({
29-
level: config.logging.level,
30-
target: "pino-pretty",
31-
options: {
32-
colorized: true,
33-
},
8+
export const loggerClient = new PinoClient({
9+
loki,
10+
level,
11+
logToFile,
12+
logPath,
3413
});
3514

36-
opts.transport.targets.push({
37-
level: config.logging.level,
38-
target: "pino/file",
39-
options: {
40-
destination: path.join(config.logging.logPath, `${startingDate}.log`),
41-
},
42-
});
43-
44-
if (loki.host) {
45-
opts.transport.targets.push({
46-
level: config.logging.level,
47-
target: "pino-loki",
48-
options: {
49-
batching: true,
50-
interval: 5,
51-
labels: { application: config.logging.loki.label },
52-
host: loki.host,
53-
basicAuth:
54-
loki.user && loki.password
55-
? {
56-
username: loki.user,
57-
password: loki.password,
58-
}
59-
: undefined,
60-
},
61-
});
62-
}
63-
64-
export const log = pino(opts);
15+
export const log = loggerClient.init();

src/schemas/config.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Type as t, type Static } from "@sinclair/typebox";
2+
import { Config as LoggingConfig } from "@vaylo/pino/schema";
23

34
import { version } from "../../package.json";
45

@@ -40,21 +41,12 @@ export const ConfigSchema = t.Object({
4041
allowUnsafeEval: t.Boolean({ default: false }),
4142
}),
4243
cors: t.Object({
43-
"Access-Control-Allow-Origin": t.String({ default: "*" }),
44-
"Access-Control-Allow-Headers": t.String({ default: "*" }),
45-
"Access-Control-Allow-Methods": t.String({ default: "POST, GET, OPTIONS" }),
46-
"Access-Control-Max-Age": t.String({ default: "86400" }),
47-
}),
48-
logging: t.Object({
49-
level: LoggingLevel,
50-
logPath: t.String(),
51-
loki: t.Object({
52-
host: t.String({ default: "" }),
53-
user: t.String({ default: "" }),
54-
password: t.String({ default: "" }),
55-
label: t.String({ default: "translate-backend" }),
56-
}),
44+
allowedHeaders: t.String({ default: "*" }),
45+
origin: t.String({ default: "*" }),
46+
methods: t.String({ default: "GET, POST, OPTIONS" }),
47+
maxAge: t.Number({ default: 86400 }),
5748
}),
49+
logging: LoggingConfig,
5850
});
5951

6052
export type ConfigSchemaType = Static<typeof ConfigSchema>;

0 commit comments

Comments
 (0)