refactor(context): Extract context modules from god object
- Create src/context/docker.js - Docker operations - Create src/context/caddy.js - Caddyfile manipulation - Create src/context/dns.js - DNS token management and API calls - Create src/context/session.js - Session wrapper - Create src/context/index.js - Context assembly (DI container) Breaks up the 50+ property ctx god object into domain-specific modules
This commit is contained in:
175
dashcaddy-api/src/context/index.js
Normal file
175
dashcaddy-api/src/context/index.js
Normal file
@@ -0,0 +1,175 @@
|
||||
/**
|
||||
* Context assembly - Dependency injection container
|
||||
* Assembles all context objects needed by routes
|
||||
*/
|
||||
const { createDockerContext } = require('./docker');
|
||||
const { createCaddyContext } = require('./caddy');
|
||||
const { createDnsContext } = require('./dns');
|
||||
const { createSessionContext } = require('./session');
|
||||
|
||||
/**
|
||||
* Assemble the full application context
|
||||
* This replaces the old "god object" ctx with explicit construction
|
||||
*/
|
||||
function assembleContext({
|
||||
// Config
|
||||
siteConfig,
|
||||
buildDomain,
|
||||
buildServiceUrl,
|
||||
SERVICES_FILE,
|
||||
CONFIG_FILE,
|
||||
TOTP_CONFIG_FILE,
|
||||
TAILSCALE_CONFIG_FILE,
|
||||
NOTIFICATIONS_FILE,
|
||||
ERROR_LOG_FILE,
|
||||
DNS_CREDENTIALS_FILE,
|
||||
CADDYFILE_PATH,
|
||||
CADDY_ADMIN_URL,
|
||||
|
||||
// State managers
|
||||
servicesStateManager,
|
||||
configStateManager,
|
||||
|
||||
// Managers
|
||||
credentialManager,
|
||||
authManager,
|
||||
licenseManager,
|
||||
healthChecker,
|
||||
updateManager,
|
||||
backupManager,
|
||||
resourceMonitor,
|
||||
auditLogger,
|
||||
portLockManager,
|
||||
selfUpdater,
|
||||
dockerMaintenance,
|
||||
logDigest,
|
||||
dockerSecurity,
|
||||
|
||||
// Templates
|
||||
APP_TEMPLATES,
|
||||
TEMPLATE_CATEGORIES,
|
||||
DIFFICULTY_LEVELS,
|
||||
RECIPE_TEMPLATES,
|
||||
RECIPE_CATEGORIES,
|
||||
|
||||
// Helpers
|
||||
asyncHandler,
|
||||
errorResponse,
|
||||
ok,
|
||||
fetchT,
|
||||
httpsAgent,
|
||||
log,
|
||||
logError,
|
||||
safeErrorMessage,
|
||||
getServiceById,
|
||||
readConfig,
|
||||
saveConfig,
|
||||
addServiceToConfig,
|
||||
validateURL,
|
||||
strictLimiter,
|
||||
totpConfig,
|
||||
saveTotpConfig,
|
||||
loadSiteConfig,
|
||||
loadNotificationConfig,
|
||||
resyncHealthChecker,
|
||||
|
||||
// Middleware result
|
||||
middlewareResult,
|
||||
|
||||
// App
|
||||
app,
|
||||
}) {
|
||||
// Create domain-specific contexts
|
||||
const docker = createDockerContext(dockerSecurity);
|
||||
const caddy = createCaddyContext(CADDYFILE_PATH, CADDY_ADMIN_URL, fetchT, httpsAgent, log, siteConfig, buildDomain);
|
||||
const dns = createDnsContext(siteConfig, buildDomain, credentialManager, fetchT, httpsAgent, log, DNS_CREDENTIALS_FILE);
|
||||
const session = createSessionContext(middlewareResult);
|
||||
|
||||
// Notification context (inline for now - could be extracted)
|
||||
const notification = {
|
||||
// These will be populated by server.js for now
|
||||
// TODO: Extract notification module
|
||||
};
|
||||
|
||||
// Tailscale context (inline for now - could be extracted)
|
||||
const tailscale = {
|
||||
// These will be populated by server.js for now
|
||||
// TODO: Extract tailscale module
|
||||
};
|
||||
|
||||
// Assemble flat context (temporary - routes still expect this)
|
||||
const ctx = {
|
||||
// Namespaced contexts
|
||||
docker,
|
||||
caddy,
|
||||
dns,
|
||||
session,
|
||||
notification,
|
||||
tailscale,
|
||||
|
||||
// App and config
|
||||
app,
|
||||
siteConfig,
|
||||
|
||||
// State managers
|
||||
servicesStateManager,
|
||||
configStateManager,
|
||||
|
||||
// Managers
|
||||
credentialManager,
|
||||
authManager,
|
||||
licenseManager,
|
||||
healthChecker,
|
||||
updateManager,
|
||||
backupManager,
|
||||
resourceMonitor,
|
||||
auditLogger,
|
||||
portLockManager,
|
||||
selfUpdater,
|
||||
dockerMaintenance,
|
||||
logDigest,
|
||||
|
||||
// Templates
|
||||
APP_TEMPLATES,
|
||||
TEMPLATE_CATEGORIES,
|
||||
DIFFICULTY_LEVELS,
|
||||
RECIPE_TEMPLATES,
|
||||
RECIPE_CATEGORIES,
|
||||
|
||||
// Helpers
|
||||
asyncHandler,
|
||||
errorResponse,
|
||||
ok,
|
||||
fetchT,
|
||||
log,
|
||||
logError,
|
||||
safeErrorMessage,
|
||||
buildDomain,
|
||||
buildServiceUrl,
|
||||
getServiceById,
|
||||
readConfig,
|
||||
saveConfig,
|
||||
addServiceToConfig,
|
||||
validateURL,
|
||||
strictLimiter,
|
||||
|
||||
// Config helpers
|
||||
totpConfig,
|
||||
saveTotpConfig,
|
||||
loadSiteConfig,
|
||||
loadNotificationConfig,
|
||||
resyncHealthChecker,
|
||||
|
||||
// File paths
|
||||
SERVICES_FILE,
|
||||
CONFIG_FILE,
|
||||
TOTP_CONFIG_FILE,
|
||||
TAILSCALE_CONFIG_FILE,
|
||||
NOTIFICATIONS_FILE,
|
||||
ERROR_LOG_FILE,
|
||||
};
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
module.exports = { assembleContext };
|
||||
Reference in New Issue
Block a user