-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.cjs
138 lines (119 loc) · 3.31 KB
/
index.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const app = express();
const PORT = process.env.PORT || 80;
const LANGUAGE_TOOL = process.env.LANGUAGE_TOOL;
const LIBRETRANSLATE = process.env.LIBRETRANSLATE;
const OLLAMA = process.env.OLLAMA;
const OLLAMA_MODEL = process.env.OLLAMA_MODEL;
const THEME = process.env.THEME;
const LIBRETRANSLATE_API_KEY = process.env.LIBRETRANSLATE_API_KEY;
const maskString = (str) => {
if (!str || str.length <= 3) {
return str;
}
const visibleChars = str.substring(0, 3);
const stars = "*".repeat(str.length - 3);
return visibleChars + stars;
};
console.log(`
==== services setup ====
LANGUAGE_TOOL: ${LANGUAGE_TOOL}
LIBRETRANSLATE: ${LIBRETRANSLATE}
OLLAMA: ${OLLAMA}
OLLAMA_MODEL: ${OLLAMA_MODEL}
THEME: ${THEME}
API_KEY: ${maskString(LIBRETRANSLATE_API_KEY)} // masked
========================
`);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const handleProxyGET = (url, res) => {
fetch(url)
.then((data) => data.json())
.then((data) => res.send(data))
.catch((error) => res.send(error));
};
const handleProxyPost = (url, req, res) => {
fetch(url, {
method: "POST",
body: JSON.stringify(req.body),
headers: { "Content-Type": "application/json" },
})
.then((data) => {
return data.json();
})
.then((data) => {
res.send(data);
})
.catch((error) => {
console.log({ error, url });
res.send(error);
});
};
const handleFormDataPost = (url, req, res) => {
const headers = {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
};
const formData = new URLSearchParams();
const keys = Object.keys(req.body);
keys.forEach((key) => {
formData.append(key, req.body[key]);
});
return fetch(url, {
method: "POST",
headers: headers,
body: formData.toString(),
})
.then((data) => data.json())
.then((data) => {
res.send(data);
})
.catch((error) => {
console.log({ error, url });
res.send(error);
});
};
app.get("/api/status", (req, res) => {
res.send({
LANGUAGE_TOOL,
LIBRETRANSLATE,
OLLAMA,
OLLAMA_MODEL,
THEME,
});
});
app.get("/api/libretranslate/languages", (req, res) => {
handleProxyGET(`${LIBRETRANSLATE}/languages`, res);
});
app.post("/api/libretranslate/translate", (req, res) => {
handleProxyPost(
`${LIBRETRANSLATE}/translate`,
{ ...req, body: { ...req.body, api_key: LIBRETRANSLATE_API_KEY ?? "" } },
res
);
});
app.post("/api/ollama/generate", (req, res) => {
handleProxyPost(
`${OLLAMA}/api/generate`,
{ ...req, body: { ...req.body, model: OLLAMA_MODEL } },
res
);
});
app.post("/api/languagetool/check", (req, res) => {
handleFormDataPost(`${LANGUAGE_TOOL}/v2/check`, req, res);
});
app.get("/api/languagetool/languages", (req, res) => {
handleProxyGET(`${LANGUAGE_TOOL}/v2/languages`, res);
});
// Serve static files from the React build directory
app.use(express.static(path.join(__dirname, "dist")));
// Handle any requests that don't match the ones above by sending them the index.html file.
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "index.html"));
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});