diff --git a/.env.example b/.env.example index b4755fd..c6c9b79 100644 --- a/.env.example +++ b/.env.example @@ -100,6 +100,10 @@ MICROSITE_API_BASE_URL=http://localhost:3001/api # Dev fallback: open published HTML via CloudFront URL of index.html key directly. # Seed bundled microsite templates: npm run seed:global-microsite-v2-templates # Or one template: npm run seed:eventify-template / npm run seed:eventre-template +# Dev: read template.json + template.html from disk instead of MongoDB/S3 (by template slug). +# MICROSITE_V2_READ_FROM_LOCAL=true +# Optional override for the templates folder (default: backend/microsite-v2-templates) +# MICROSITE_V2_TEMPLATES_DIR=/path/to/microsite-v2-templates # # Published-site target: s3 (default) | ec2. When ec2, rendered index.html + # config.json are uploaded via SFTP to {REMOTE_ROOT}/{subdomain}/ on the EC2 box. diff --git a/src/controllers/staticMicrosite.controller.js b/src/controllers/staticMicrosite.controller.js index 174b965..1650acd 100644 --- a/src/controllers/staticMicrosite.controller.js +++ b/src/controllers/staticMicrosite.controller.js @@ -8,7 +8,6 @@ const { uploadTemplateFile, uploadTemplateAsset, templateAssetsBaseUrl, - getS3ObjectBuffer, templateKey, cloudfrontUrlForKey, replaceSlugInS3Key, @@ -201,14 +200,11 @@ const getTemplate = async (request, h) => { return h.response(error('FORBIDDEN', access.message)).code(access.status); } - let schema = template.schema; - if (!schema && template.schemaS3Key) { - try { - const buf = await getS3ObjectBuffer(template.schemaS3Key); - schema = JSON.parse(buf.toString('utf8')); - } catch { - schema = null; - } + let schema; + try { + schema = await loadTemplateSchema(template); + } catch { + schema = template.schema || null; } return h.response(success({ ...template, schema })); diff --git a/src/utils/localMicrositeV2Templates.js b/src/utils/localMicrositeV2Templates.js new file mode 100644 index 0000000..5d41380 --- /dev/null +++ b/src/utils/localMicrositeV2Templates.js @@ -0,0 +1,47 @@ +const fs = require('fs'); +const path = require('path'); +const { parseTemplateSchema } = require('./templateSchema'); + +const BACKEND_ROOT = path.join(__dirname, '..', '..'); + +function isLocalMicrositeV2TemplatesEnabled() { + const v = String(process.env.MICROSITE_V2_READ_FROM_LOCAL || '').trim().toLowerCase(); + return v === '1' || v === 'true' || v === 'yes'; +} + +function getTemplatesRoot() { + const custom = String(process.env.MICROSITE_V2_TEMPLATES_DIR || '').trim(); + if (custom) return path.resolve(custom); + return path.join(BACKEND_ROOT, 'microsite-v2-templates'); +} + +function resolveTemplateDir(slug) { + const normalized = String(slug || '').trim().toLowerCase(); + if (!normalized) return null; + const dir = path.join(getTemplatesRoot(), normalized); + if (!fs.existsSync(dir)) return null; + return dir; +} + +function readLocalTemplateSchema(slug) { + const dir = resolveTemplateDir(slug); + if (!dir) return null; + const jsonPath = path.join(dir, 'template.json'); + if (!fs.existsSync(jsonPath)) return null; + return parseTemplateSchema(fs.readFileSync(jsonPath)); +} + +function readLocalTemplateHtml(slug) { + const dir = resolveTemplateDir(slug); + if (!dir) return null; + const htmlPath = path.join(dir, 'template.html'); + if (!fs.existsSync(htmlPath)) return null; + return fs.readFileSync(htmlPath, 'utf8'); +} + +module.exports = { + isLocalMicrositeV2TemplatesEnabled, + getTemplatesRoot, + readLocalTemplateSchema, + readLocalTemplateHtml, +}; diff --git a/src/utils/publishStaticMicrosite.js b/src/utils/publishStaticMicrosite.js index d6fc46f..74d0999 100644 --- a/src/utils/publishStaticMicrosite.js +++ b/src/utils/publishStaticMicrosite.js @@ -4,6 +4,11 @@ const { uploadSiteToEc2 } = require('./ec2MicrositePublish'); const { mergeConfigWithDefaults } = require('./templateSchema'); const { renderHandlebarsTemplate, buildRenderContext } = require('./handlebarsRender'); const { resolveConfigAssetUrls } = require('./micrositeConfigAssets'); +const { + isLocalMicrositeV2TemplatesEnabled, + readLocalTemplateSchema, + readLocalTemplateHtml, +} = require('./localMicrositeV2Templates'); function preparePublishedConfig(schema, userConfig, assetsBaseUrl) { const merged = mergeConfigWithDefaults(schema, userConfig); @@ -14,6 +19,10 @@ function preparePublishedConfig(schema, userConfig, assetsBaseUrl) { } async function loadTemplateHtml(template) { + if (isLocalMicrositeV2TemplatesEnabled() && template.slug) { + const local = readLocalTemplateHtml(template.slug); + if (local) return local; + } if (!template.htmlS3Key) { throw new Error('Template HTML is not uploaded yet'); } @@ -22,6 +31,10 @@ async function loadTemplateHtml(template) { } async function loadTemplateSchema(template) { + if (isLocalMicrositeV2TemplatesEnabled() && template.slug) { + const local = readLocalTemplateSchema(template.slug); + if (local) return local; + } if (template.schema) return template.schema; if (!template.schemaS3Key) { throw new Error('Template schema is not available');