move to src

This commit is contained in:
Alex Holliday committed 2025-07-25 15:33:48 -07:00
1 parent acc910cbc0
commit 1c47c8ce2c
127 files changed
+71 -19

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
const SERVICE_NAME = "ServiceRegistry";
import logger from "../../utils/logger.js";
class ServiceRegistry {
static SERVICE_NAME = SERVICE_NAME;
constructor() {
this.services = {};
}
get serviceName() {
return ServiceRegistry.SERVICE_NAME;
}
register(name, service) {
logger.info({
message: `Registering service ${name}`,
service: SERVICE_NAME,
method: "register",
});
this.services[name] = service;
}
get(name) {
if (!this.services[name]) {
logger.error({
message: `Service ${name} is not registered`,
service: SERVICE_NAME,
method: "get",
});
throw new Error(`Service ${name} is not registered`);
}
return this.services[name];
}
listServices() {
return Object.keys(this.services);
}
}
export default new ServiceRegistry();
+81
View File
@@ -0,0 +1,81 @@
const SERVICE_NAME = "SettingsService";
const envConfig = {
nodeEnv: process.env.NODE_ENV,
logLevel: process.env.LOG_LEVEL,
systemEmailHost: process.env.SYSTEM_EMAIL_HOST,
systemEmailPort: process.env.SYSTEM_EMAIL_PORT,
systemEmailUser: process.env.SYSTEM_EMAIL_USER,
systemEmailAddress: process.env.SYSTEM_EMAIL_ADDRESS,
systemEmailPassword: process.env.SYSTEM_EMAIL_PASSWORD,
jwtSecret: process.env.JWT_SECRET,
jwtTTL: process.env.TOKEN_TTL,
clientHost: process.env.CLIENT_HOST,
dbConnectionString: process.env.DB_CONNECTION_STRING,
redisUrl: process.env.REDIS_URL,
callbackUrl: process.env.CALLBACK_URL,
port: process.env.PORT,
pagespeedApiKey: process.env.PAGESPEED_API_KEY,
uprockApiKey: process.env.UPROCK_API_KEY,
};
/**
* SettingsService
*
* This service is responsible for loading and managing the application settings.
*/
class SettingsService {
static SERVICE_NAME = "SettingsService";
/**
* Constructs a new SettingsService
* @constructor
* @throws {Error}
*/ constructor(AppSettings) {
this.AppSettings = AppSettings;
this.settings = { ...envConfig };
}
get serviceName() {
return SettingsService.SERVICE_NAME;
}
/**
* Load settings from env settings
* @returns {Object>} The settings.
*/
loadSettings() {
return this.settings;
}
/**
* Reload settings by calling loadSettings.
* @returns {Promise<Object>} The reloaded settings.
*/
reloadSettings() {
return this.loadSettings();
}
/**
* Get the current settings.
* @returns {Object} The current settings.
* @throws Will throw an error if settings have not been loaded.
*/
getSettings() {
if (!this.settings) {
throw new Error("Settings have not been loaded");
}
return this.settings;
}
async getDBSettings() {
// Remove any old settings
await this.AppSettings.deleteMany({ version: { $exists: false } });
let settings = await this.AppSettings.findOne({ singleton: true }).select("-__v -_id -createdAt -updatedAt -singleton").lean();
if (settings === null) {
await this.AppSettings.create({});
settings = await this.AppSettings.findOne({ singleton: true }).select("-__v -_id -createdAt -updatedAt -singleton").lean();
}
return settings;
}
}
export default SettingsService;
+448
View File
@@ -0,0 +1,448 @@
const SERVICE_NAME = "StringService";
class StringService {
static SERVICE_NAME = SERVICE_NAME;
constructor(translationService) {
if (StringService.instance) {
return StringService.instance;
}
this.translationService = translationService;
this._language = "en"; // default language
StringService.instance = this;
}
get serviceName() {
return StringService.SERVICE_NAME;
}
setLanguage(language) {
this._language = language;
}
get language() {
return this._language;
}
// Auth Messages
get dontHaveAccount() {
return this.translationService.getTranslation("dontHaveAccount");
}
get email() {
return this.translationService.getTranslation("email");
}
get forgotPassword() {
return this.translationService.getTranslation("forgotPassword");
}
get password() {
return this.translationService.getTranslation("password");
}
get signUp() {
return this.translationService.getTranslation("signUp");
}
get submit() {
return this.translationService.getTranslation("submit");
}
get title() {
return this.translationService.getTranslation("title");
}
get continue() {
return this.translationService.getTranslation("continue");
}
get enterEmail() {
return this.translationService.getTranslation("enterEmail");
}
get authLoginTitle() {
return this.translationService.getTranslation("authLoginTitle");
}
get authLoginEnterPassword() {
return this.translationService.getTranslation("authLoginEnterPassword");
}
get commonPassword() {
return this.translationService.getTranslation("commonPassword");
}
get commonBack() {
return this.translationService.getTranslation("commonBack");
}
get authForgotPasswordTitle() {
return this.translationService.getTranslation("authForgotPasswordTitle");
}
get authForgotPasswordResetPassword() {
return this.translationService.getTranslation("authForgotPasswordResetPassword");
}
get createPassword() {
return this.translationService.getTranslation("createPassword");
}
get createAPassword() {
return this.translationService.getTranslation("createAPassword");
}
get authRegisterAlreadyHaveAccount() {
return this.translationService.getTranslation("authRegisterAlreadyHaveAccount");
}
get commonAppName() {
return this.translationService.getTranslation("commonAppName");
}
get authLoginEnterEmail() {
return this.translationService.getTranslation("authLoginEnterEmail");
}
get authRegisterTitle() {
return this.translationService.getTranslation("authRegisterTitle");
}
get monitorGetAll() {
return this.translationService.getTranslation("monitorGetAll");
}
get monitorGetById() {
return this.translationService.getTranslation("monitorGetById");
}
get monitorGetByIdSuccess() {
return this.translationService.getTranslation("monitorGetByIdSuccess");
}
get monitorCreate() {
return this.translationService.getTranslation("monitorCreate");
}
get bulkMonitorsCreate() {
return this.translationService.getTranslation("bulkMonitorsCreate");
}
get monitorEdit() {
return this.translationService.getTranslation("monitorEdit");
}
get monitorDelete() {
return this.translationService.getTranslation("monitorDelete");
}
get monitorPause() {
return this.translationService.getTranslation("monitorPause");
}
get monitorResume() {
return this.translationService.getTranslation("monitorResume");
}
get monitorDemoAdded() {
return this.translationService.getTranslation("monitorDemoAdded");
}
get monitorStatsById() {
return this.translationService.getTranslation("monitorStatsById");
}
get monitorCertificate() {
return this.translationService.getTranslation("monitorCertificate");
}
// Maintenance Window Messages
get maintenanceWindowCreate() {
return this.translationService.getTranslation("maintenanceWindowCreate");
}
get maintenanceWindowGetById() {
return this.translationService.getTranslation("maintenanceWindowGetById");
}
get maintenanceWindowGetByTeam() {
return this.translationService.getTranslation("maintenanceWindowGetByTeam");
}
get maintenanceWindowDelete() {
return this.translationService.getTranslation("maintenanceWindowDelete");
}
get maintenanceWindowEdit() {
return this.translationService.getTranslation("maintenanceWindowEdit");
}
// Webhook Messages
get webhookUnsupportedPlatform() {
return this.translationService.getTranslation("webhookUnsupportedPlatform");
}
get webhookSendError() {
return this.translationService.getTranslation("webhookSendError");
}
get webhookSendSuccess() {
return this.translationService.getTranslation("webhookSendSuccess");
}
get telegramRequiresBotTokenAndChatId() {
return this.translationService.getTranslation("telegramRequiresBotTokenAndChatId");
}
get webhookUrlRequired() {
return this.translationService.getTranslation("webhookUrlRequired");
}
get platformRequired() {
return this.translationService.getTranslation("platformRequired");
}
get testNotificationFailed() {
return this.translationService.getTranslation("testNotificationFailed");
}
get monitorUpAlert() {
return this.translationService.getTranslation("monitorUpAlert");
}
get monitorDownAlert() {
return this.translationService.getTranslation("monitorDownAlert");
}
getWebhookUnsupportedPlatform(platform) {
return this.translationService.getTranslation("webhookUnsupportedPlatform").replace("{platform}", platform);
}
getWebhookSendError(platform) {
return this.translationService.getTranslation("webhookSendError").replace("{platform}", platform);
}
getMonitorStatus(name, status, url) {
const translationKey = status === true ? "monitorStatusUp" : "monitorStatusDown";
return this.translationService.getTranslation(translationKey).replace("{name}", name).replace("{url}", url);
}
// Error Messages
get unknownError() {
return this.translationService.getTranslation("unknownError");
}
get friendlyError() {
return this.translationService.getTranslation("friendlyError");
}
get authIncorrectPassword() {
return this.translationService.getTranslation("authIncorrectPassword");
}
get unauthorized() {
return this.translationService.getTranslation("unauthorized");
}
get authAdminExists() {
return this.translationService.getTranslation("authAdminExists");
}
get authInviteNotFound() {
return this.translationService.getTranslation("authInviteNotFound");
}
get unknownService() {
return this.translationService.getTranslation("unknownService");
}
get noAuthToken() {
return this.translationService.getTranslation("noAuthToken");
}
get invalidAuthToken() {
return this.translationService.getTranslation("invalidAuthToken");
}
get expiredAuthToken() {
return this.translationService.getTranslation("expiredAuthToken");
}
// Queue Messages
get queueGetMetrics() {
return this.translationService.getTranslation("queueGetMetrics");
}
get queueGetJobs() {
return this.translationService.getTranslation("queueGetJobs");
}
get queueAddJob() {
return this.translationService.getTranslation("queueAddJob");
}
get queueObliterate() {
return this.translationService.getTranslation("queueObliterate");
}
// Job Queue Messages
get jobQueueDeleteJobSuccess() {
return this.translationService.getTranslation("jobQueueDeleteJobSuccess");
}
get jobQueuePauseJob() {
return this.translationService.getTranslation("jobQueuePauseJob");
}
get jobQueueResumeJob() {
return this.translationService.getTranslation("jobQueueResumeJob");
}
// Status Page Messages
get statusPageByUrl() {
return this.translationService.getTranslation("statusPageByUrl");
}
get statusPageCreate() {
return this.translationService.getTranslation("statusPageCreate");
}
get statusPageDelete() {
return this.translationService.getTranslation("statusPageDelete");
}
get statusPageUpdate() {
return this.translationService.getTranslation("statusPageUpdate");
}
get statusPageNotFound() {
return this.translationService.getTranslation("statusPageNotFound");
}
get statusPageByTeamId() {
return this.translationService.getTranslation("statusPageByTeamId");
}
get statusPageUrlNotUnique() {
return this.translationService.getTranslation("statusPageUrlNotUnique");
}
// Docker Messages
get dockerFail() {
return this.translationService.getTranslation("dockerFail");
}
get dockerNotFound() {
return this.translationService.getTranslation("dockerNotFound");
}
get dockerSuccess() {
return this.translationService.getTranslation("dockerSuccess");
}
// Port Messages
get portFail() {
return this.translationService.getTranslation("portFail");
}
get portSuccess() {
return this.translationService.getTranslation("portSuccess");
}
// Alert Messages
get alertCreate() {
return this.translationService.getTranslation("alertCreate");
}
get alertGetByUser() {
return this.translationService.getTranslation("alertGetByUser");
}
get alertGetByMonitor() {
return this.translationService.getTranslation("alertGetByMonitor");
}
get alertGetById() {
return this.translationService.getTranslation("alertGetById");
}
get alertEdit() {
return this.translationService.getTranslation("alertEdit");
}
get alertDelete() {
return this.translationService.getTranslation("alertDelete");
}
getDeletedCount(count) {
return this.translationService.getTranslation("deletedCount").replace("{count}", count);
}
get pingSuccess() {
return this.translationService.getTranslation("pingSuccess");
}
get getAppSettings() {
return this.translationService.getTranslation("getAppSettings");
}
get httpNetworkError() {
return this.translationService.getTranslation("httpNetworkError");
}
get httpNotJson() {
return this.translationService.getTranslation("httpNotJson");
}
get httpJsonPathError() {
return this.translationService.getTranslation("httpJsonPathError");
}
get httpEmptyResult() {
return this.translationService.getTranslation("httpEmptyResult");
}
get httpMatchSuccess() {
return this.translationService.getTranslation("httpMatchSuccess");
}
get httpMatchFail() {
return this.translationService.getTranslation("httpMatchFail");
}
get updateAppSettings() {
return this.translationService.getTranslation("updateAppSettings");
}
get insufficientPermissions() {
return this.translationService.getTranslation("insufficientPermissions");
}
getDbFindMonitorById(monitorId) {
return this.translationService.getTranslation("dbFindMonitorById").replace("${monitorId}", monitorId);
}
get dbUserExists() {
return this.translationService.getTranslation("dbUserExists");
}
get testEmailSubject() {
return this.translationService.getTranslation("testEmailSubject");
}
get verifyOwnerNotFound() {
return this.translationService.getTranslation("verifyOwnerNotFound");
}
get verifyOwnerUnauthorized() {
return this.translationService.getTranslation("verifyOwnerUnauthorized");
}
get dbUserNotFound() {
return this.translationService.getTranslation("dbUserNotFound");
}
}
export default StringService;
+92
View File
@@ -0,0 +1,92 @@
import fs from "fs";
import path from "path";
class TranslationService {
static SERVICE_NAME = "TranslationService";
constructor(logger) {
this.logger = logger;
this.translations = {};
this._language = "en";
this.localesDir = path.join(process.cwd(), "locales");
}
get serviceName() {
return TranslationService.SERVICE_NAME;
}
setLanguage(language) {
this._language = language;
}
get language() {
return this._language;
}
async initialize() {
try {
await this.loadFromFiles();
} catch (error) {
this.logger.error({
message: error.message,
service: "TranslationService",
method: "initialize",
stack: error.stack,
});
}
}
async loadFromFiles() {
try {
if (!fs.existsSync(this.localesDir)) {
return false;
}
const files = fs.readdirSync(this.localesDir).filter((file) => file.endsWith(".json"));
if (files.length === 0) {
return false;
}
for (const file of files) {
const language = file.replace(".json", "");
const filePath = path.join(this.localesDir, file);
const content = fs.readFileSync(filePath, "utf8");
this.translations[language] = JSON.parse(content);
}
this.logger.info({
message: "Translations loaded from files successfully",
service: "TranslationService",
method: "loadFromFiles",
});
return true;
} catch (error) {
this.logger.error({
message: error.message,
service: "TranslationService",
method: "loadFromFiles",
stack: error.stack,
});
return false;
}
}
getTranslation(key) {
let language = this._language;
try {
return this.translations[language]?.[key] || this.translations["en"]?.[key] || key;
} catch (error) {
this.logger.error({
message: error.message,
service: "TranslationService",
method: "getTranslation",
stack: error.stack,
});
return key;
}
}
}
export default TranslationService;