0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-03-22 06:48:11 +00:00

bring admin.jsx

This commit is contained in:
Víctor Losada Hernández
2026-02-02 17:17:49 +01:00
parent 74733a4cca
commit 09171acc6e
6 changed files with 62 additions and 100 deletions

View File

@@ -0,0 +1,6 @@
import { createRoot } from "react-dom/client";
import Admin from "./admin.jsx";
const props = window.__INITIAL_PROPS__ || {};
createRoot(document.getElementById("reactRoot")).render(<Admin {...props} />);

View File

@@ -1,66 +0,0 @@
import fs from "fs";
const isProd = process.env.NODE_ENV === "production";
const template = async function ({ vite, url }, name, title = "", props = {}) {
const ogTags = [];
const ogMeta = props.ogMeta ?? {};
Object.entries(ogMeta).forEach(([key, value]) => {
if (!value) return;
ogTags.push(`<meta property="og:${key}" content="${value}">`);
});
const ogMetaTags = ogTags.join("\n");
// ----------------
// PROD
// ----------------
if (isProd) {
const ssrModule = await import(`../build/entry-server-${name}/bundle.js`);
return `<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, height=device-height, interactive-widget=resizes-visual" />
<link href="//fonts.googleapis.com/css?family=Open+Sans:400,300,600,700" rel="stylesheet" type="text/css" />
<link href="/${name}/bundle.css" rel="stylesheet" />
<link rel="icon" href="/assets/favicon.ico" type="image/x-icon" />
${ogMetaTags}
<meta name="twitter:card" content="summary">
<title>${title.length ? `${title} - The Homebrewery` : "The Homebrewery - NaturalCrit"}</title>
</head>
<body>
<main id="reactRoot">${ssrModule.default(props)}</main>
<script src="/${name}/bundle.js"></script>
<script>start_app(${JSON.stringify(props)})</script>
</body>
</html>`;
}
// ----------------
// DEV
// ----------------
const { default: render } = await vite.ssrLoadModule(`/client/entry-server-${name}.jsx`);
let html = `<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, height=device-height, interactive-widget=resizes-visual" />
${ogMetaTags}
<title>${title.length ? `${title} - The Homebrewery` : "The Homebrewery - NaturalCrit"}</title>
</head>
<body>
<main id="reactRoot">${render(props)}</main>
<script type="module" src="/@vite/client"></script>
<script type="module" src="/client/entry-client-${name}.jsx"></script>
</body>
</html>`;
return vite.transformIndexHtml(url, html);
};
export default template;

View File

@@ -1,23 +1,29 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta <meta
name="viewport" name="viewport"
content="width=device-width, initial-scale=1, height=device-height, interactive-widget=resizes-visual" /> content="width=device-width, initial-scale=1, height=device-height, interactive-widget=resizes-visual" />
<link <link
href="//fonts.googleapis.com/css?family=Open+Sans:400,300,600,700" href="//fonts.googleapis.com/css?family=Open+Sans:400,300,600,700"
rel="stylesheet" rel="stylesheet"
type="text/css" /> type="text/css" />
<link rel="icon" href="/assets/favicon.ico" type="image/x-icon" /> <link rel="icon" href="/assets/favicon.ico" type="image/x-icon" />
<meta name="twitter:card" content="summary" /> <meta name="twitter:card" content="summary" />
<title>The Homebrewery - NaturalCrit</title> <title>The Homebrewery - NaturalCrit</title>
</head> </head>
<body> <body>
<main id="reactRoot"></main> <main id="reactRoot"></main>
<script type="module" src="/client/homebrew/main.jsx"></script> <script type="module">
</body> if (window.location.pathname.startsWith('/admin')) {
import('/client/admin/main.jsx');
} else {
import('/client/homebrew/main.jsx');
}
</script>
</body>
</html> </html>

View File

@@ -13,7 +13,6 @@ async function start() {
server: { middlewareMode: true }, server: { middlewareMode: true },
appType: "custom", appType: "custom",
}); });
} }
await DB.connect(config).catch((err) => { await DB.connect(config).catch((err) => {

View File

@@ -4,18 +4,23 @@ import { model as NotificationModel } from './notifications.model.js';
import express from 'express'; import express from 'express';
import Moment from 'moment'; import Moment from 'moment';
import zlib from 'zlib'; import zlib from 'zlib';
import templateFn from '../client/template.js'; import config from './config.js';
import path from 'path';
import fs from 'fs-extra';
const nodeEnv = config.get('node_env');
const isProd = nodeEnv === 'production';
import HomebrewAPI from './homebrew.api.js'; import HomebrewAPI from './homebrew.api.js';
import asyncHandler from 'express-async-handler'; import asyncHandler from 'express-async-handler';
import { splitTextStyleAndMetadata } from '../shared/helpers.js'; import { splitTextStyleAndMetadata } from '../shared/helpers.js';
const router = express.Router();
process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin'; process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin';
process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password3'; process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password3';
export default function createAdminApi(vite) {
const router = express.Router();
const mw = { const mw = {
adminOnly : (req, res, next)=>{ adminOnly : (req, res, next)=>{
if(!req.get('authorization')){ if(!req.get('authorization')){
@@ -371,15 +376,28 @@ router.delete('/admin/notification/delete/:id', mw.adminOnly, async (req, res, n
} }
}); });
router.get('/admin', mw.adminOnly, (req, res)=>{ router.get('/admin', mw.adminOnly, asyncHandler(async (req, res) => {
templateFn('admin', { const props = {
url : req.originalUrl url : req.originalUrl
}) };
.then((page)=>res.send(page))
.catch((err)=>{ const htmlPath = isProd
console.log(err); ? path.resolve('build', 'index.html')
res.sendStatus(500); : path.resolve('index.html');
});
}); let html = fs.readFileSync(htmlPath, 'utf-8');
if (!isProd && vite?.transformIndexHtml) {
html = await vite.transformIndexHtml(req.originalUrl, html);
}
res.send(html.replace(
'<head>',
`<head>\n<script id="props">window.__INITIAL_PROPS__ = ${JSON.stringify(props)}</script>`
));
}));
return router;
}
export default router;

View File

@@ -44,7 +44,6 @@ export default async function createApp(vite) {
const isProd = nodeEnv === 'production'; const isProd = nodeEnv === 'production';
const isLocalEnvironment = config.get('local_environments').includes(nodeEnv); const isLocalEnvironment = config.get('local_environments').includes(nodeEnv);
const sanitizeBrew = (brew, accessType)=>{ const sanitizeBrew = (brew, accessType)=>{
brew._id = undefined; brew._id = undefined;
brew.__v = undefined; brew.__v = undefined;
@@ -114,7 +113,7 @@ export default async function createApp(vite) {
}); });
app.use(homebrewApi); app.use(homebrewApi);
app.use(adminApi); app.use(adminApi(vite));
app.use(vaultApi); app.use(vaultApi);
const welcomeText = fs.readFileSync('./client/homebrew/pages/homePage/welcome_msg.md', 'utf8'); const welcomeText = fs.readFileSync('./client/homebrew/pages/homePage/welcome_msg.md', 'utf8');