forked from RedHatTraining/DO288-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.git | ||
.npm | ||
.containerignore | ||
node_modules | ||
Containerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
FROM registry.access.redhat.com/ubi9/nodejs-18-minimal:1-51 | ||
FROM registry.access.redhat.com/ubi9/nodejs-18-minimal | ||
|
||
# Use root user to listen on port 80 | ||
USER root | ||
|
||
|
||
# Application listens on this port | ||
ENV PORT=80 | ||
EXPOSE ${PORT} | ||
|
||
# Install application dependencies | ||
COPY package*.json ./ | ||
RUN npm ci --omit=dev | ||
# Add application sources | ||
ADD . $HOME | ||
|
||
# Copy the application source code | ||
COPY . . | ||
# Install dependencies. | ||
# Clean up cache when finished. | ||
RUN npm ci --omit=dev && rm -rf .npm | ||
|
||
# Run the server | ||
CMD npm start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Simulate an external localization service | ||
*/ | ||
const fs = require("fs").promises; | ||
|
||
|
||
const TRANSLATIONS = { | ||
"greeting": { | ||
"de-de": "Guten tag", | ||
"en-us": "Hi!", | ||
"es-es": "Hola, ¿que tal?", | ||
"fr-be": "Bonjour, ça va bien?", | ||
"it-it": "Ciao, come stai?", | ||
"ca": "Bon dia", | ||
"pt-pt": "Olá, tudo bem?", | ||
"hi": "Namaste", | ||
"nl-nl": "Hallo", | ||
"no-no": "Hei!", | ||
"el": "Yassou", | ||
"cs": "Ahoj!", | ||
"pl": "Cześć!", | ||
"sv-se": "Hej", | ||
"ru": "Privet", | ||
"tr": "Selam" | ||
} | ||
} | ||
|
||
|
||
async function translate(key, locale) { | ||
let translated; | ||
|
||
try { | ||
translated = await readFromFileCache(key, locale); | ||
console.info(`${locale} '${key}' found in cache`); | ||
} catch (error) { | ||
// Only handle the not found case (ENOENT) | ||
if (error.code !== "ENOENT") { | ||
throw err; | ||
} | ||
|
||
translated = await getTranslationFromExternalService(key, locale); | ||
|
||
await writeToFileCache(key, locale, translated); | ||
} | ||
|
||
return translated; | ||
} | ||
|
||
|
||
async function readFromFileCache(key, locale) { | ||
const buffer = await fs.readFile(generateCacheFilename(key, locale)); | ||
return buffer.toString(); | ||
} | ||
|
||
|
||
function writeToFileCache(key, locale, value) { | ||
return fs.writeFile(generateCacheFilename(key, locale), value); | ||
} | ||
|
||
|
||
function generateCacheFilename(key, locale) { | ||
return `/var/cache/translation_${key}_${locale}`; | ||
} | ||
|
||
|
||
function getTranslationFromExternalService(key, locale) { | ||
// Simulate a network delay of 500 millis | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
const translated = TRANSLATIONS[key][locale]; | ||
resolve(translated); | ||
}, 500); | ||
}); | ||
} | ||
|
||
|
||
function getSupportedLocaleIDs() { | ||
return Object.keys(TRANSLATIONS.greeting); | ||
} | ||
|
||
|
||
function getRandomLocaleID() { | ||
const locales = getSupportedLocaleIDs(); | ||
const localeIndex = Math.floor(Math.random() * locales.length); | ||
return locales[localeIndex]; | ||
} | ||
|
||
|
||
module.exports = { | ||
translate, | ||
getRandomLocaleID, | ||
getSupportedLocaleIDs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.